스프링/스프링 시큐리티

회원가입 로직

까마귀! 2024. 3. 23. 22:13
join.mustache
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<form action="/joinProc" method="post" name="joinForm">
    <input type="text" name="username" placeholder="Username"/>
    <input type="password" name="password" placeholder="Password"/>
    <input type="submit" value="Join"/>
</form>
</body>
</html>

 

 

 

 

joinController.java
@Controller
public class joinController {

    @Autowired
    private JoinService joinService;

    @GetMapping("/join")
    public String joinP() {

        return "join";
    }

    @PostMapping("/joinProc")
    public String joinProcess(joinDTO joinDTO) {

        System.out.println(joinDTO.getUsername());

        joinService.joinProcess(joinDTO);

        return "redirect:/login";
    }
}

 

 

 

 

 

joinDTO
@Setter
@Getter
public class joinDTO {

    private String username;
    private String password;
}

 

 

 

 

UserEntity
@Entity
@Getter
@Setter
public class UserEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Column(unique = true)
    private String username;

    @Column
    private String password;

    @Column
    private String role;
}

 

 

 

 

UserRepository
public interface UserRepository extends JpaRepository<UserEntity, Integer> {

    boolean existsByUsername(String username);
    
}

 

 

 

 

JoinService
@Service
public class JoinService {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    public void joinProcess(joinDTO joinDTO) {

        //db에 이미 동일한 username을 가진 회원이 존재하는지?
        Boolean isUser = userRepository.existsByUsername(joinDTO.getUsername());
        if (isUser) {
            return;
        }

        UserEntity data = new UserEntity();

        data.setUsername(joinDTO.getUsername());
        data.setPassword(bCryptPasswordEncoder.encode(joinDTO.getPassword()));
        data.setRole("ROLE_ADMIN");

        userRepository.save(data);
    }
}

 

'스프링 > 스프링 시큐리티' 카테고리의 다른 글

세션 설정  (0) 2024.03.23
로그인 검증 로직  (0) 2024.03.23
BCrypt 암호화 메소드 기본설정  (0) 2024.03.07
커스텀 로그인 설정  (0) 2024.03.07
Security Config 구현하기  (0) 2024.03.07