회원가입
회원 정보를 통해 인증 / 인가 작업을 진행하기 때문에 사용자로부터 회원 가입을 진행한 뒤 데이터베이스에 회원 정보를 저장한다.
SecurityConfig
package com.gdy.springsecurity.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
@Configuration //confinguration 컨테이너로 등록
@EnableWebSecurity
public class SecurityConfig {
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder(){
return new BCryptPasswordEncoder();
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception{
http.authorizeHttpRequests((auth) -> auth
.requestMatchers("/", "/login", "/loginProce","/join","/joinProc").permitAll() //루트 경로에서 작업 진행
.requestMatchers("/admin").hasRole("ADMIN")
.requestMatchers("/my/**").hasAnyRole("ADMIN","USER")
.anyRequest().authenticated()//위에서 처리하지 못한 경로들 처리하는 메소드
);
http
.formLogin((auth) -> auth.loginPage("/login")
.loginProcessingUrl("/loginProc") //form에서 post로 보내는 action값
.permitAll()
);
http
.csrf((auth)->auth.disable());
return http.build();
}
}
MemberController
package com.gdy.springsecurity.Controller;
import com.gdy.springsecurity.Dto.MemberDTO;
import com.gdy.springsecurity.Service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
@RequiredArgsConstructor
public class MemberController {
private final MemberService memberService;
@GetMapping("/join")
public String joinForm(){
return "join";
}
@PostMapping("/joinProc")
public String join(MemberDTO memberDTO){
System.out.println(memberDTO);
memberService.join(memberDTO);
return "index";
}
}
MemberDTO
package com.gdy.springsecurity.Dto;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class MemberDTO {
private String username;
private String password;
}
MemberEntity
package com.gdy.springsecurity.Entity;
import com.gdy.springsecurity.Dto.MemberDTO;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
@Entity
@Setter
@Getter
@RequiredArgsConstructor
public class MemberEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(unique = true)
private String username;
private String password;
private String role;
public static MemberEntity toJoinmemberEntity(MemberDTO memberDTO, String encode, String Role) {
MemberEntity memberEntity = new MemberEntity();
memberEntity.setUsername(memberDTO.getUsername());
memberEntity.setPassword(encode);
memberEntity.setRole(Role);
return memberEntity;
}
}
MemberRepository
package com.gdy.springsecurity.Repository;
import com.gdy.springsecurity.Entity.MemberEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<MemberEntity,Integer> {
boolean existsByUsername(String username);
}
MemberService
package com.gdy.springsecurity.Service;
import com.gdy.springsecurity.Dto.MemberDTO;
import com.gdy.springsecurity.Entity.MemberEntity;
import com.gdy.springsecurity.Repository.MemberRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class MemberService {
private final MemberRepository memberRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
public void join(MemberDTO memberDTO){
//db에 이미 동일한 username을 가진 회원이 존재하는지?
boolean isUser = memberRepository.existsByUsername(memberDTO.getUsername());
if(isUser){
return;
}
String password = bCryptPasswordEncoder.encode(memberDTO.getPassword());
String Role = "ROLE_USER";
MemberEntity memberEntity = MemberEntity.toJoinmemberEntity(memberDTO,password,Role);
memberRepository.save(memberEntity);
}
}
결과
1. 로그인 페이지
2. 로그인 성공
3. 결과
password가 암호화되어 저장된 모습을 볼 수 있다.
'🌠Development > SpringBoot' 카테고리의 다른 글
[Spring Securitiy] 08 - 세션 정보 확인 (0) | 2024.02.25 |
---|---|
[Spring Security] 07 - 로그인 검증 로직 (0) | 2024.02.22 |
[Spring Security] 05 - BCrypt 암호화 메서드 (0) | 2024.02.21 |
[Spring Security] 04 - 커스텀 로그인 (0) | 2024.02.21 |
[Spring Security] 03 - Security Config 클래스 (0) | 2024.02.21 |