컬럼 - PK 아이디, 유저네임, 패스워드, 역할
package com.sample.springjwt.entity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import lombok.Getter;
import lombok.Setter;
@Entity
@Setter
@Getter
public class UserEntity {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String username;
private String password;
private String role;
}
이름과 비밀번호만 입력 받기
package com.sample.springjwt.dto;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class JoinDto {
private String username;
private String password;
}
“/join” url 을 가지는 회원가입 API
package com.sample.springjwt.controller;
import com.sample.springjwt.dto.JoinDto;
import com.sample.springjwt.service.JoinService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@ResponseBody
public class JoinController {
private final JoinService joinService;
public JoinController(JoinService joinService){
this.joinService = joinService;
}
@PostMapping("/join")
public String joinProcess(JoinDto joinDto){
joinService.joinProcess(joinDto);
return "ok";
}
}
유저레포지토리로 입력받은 이메일이 DB에 있는지 중복 확인하고
→ 이미 존재하면 바로 리턴
→ 존재하지 않을 때 회원가입 진행
UserEntity에 이메일과 인코딩한 비밀번호와 역할을 넣고 save() 메서드로 저장
package com.sample.springjwt.service;
import com.sample.springjwt.dto.JoinDto;
import com.sample.springjwt.entity.UserEntity;
import com.sample.springjwt.repository.UserRepository;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class JoinService {
private final UserRepository userRepository;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
public JoinService(UserRepository userRepository, BCryptPasswordEncoder bCryptPasswordEncoder){
this.userRepository = userRepository;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
public void joinProcess(JoinDto joinDto){
String username = joinDto.getUsername();
String password = joinDto.getPassword();
Boolean isExist = userRepository.existsByUsername(username);
if (isExist) {
return;
}
UserEntity data = new UserEntity();
data.setUsername(username);
// 비밀번호는 암호화해서 넣어야함
data.setPassword(bCryptPasswordEncoder.encode(password));
data.setRole("ROLE_ADMIN");
userRepository.save(data);
}
}
BCryptPasswordEncoderBCrypt 해싱 함수를 사용해서 비밀번호를 인코딩해주는 메서드와
사용자에 의해 제출된 비밀번호와 저장소에 저장되어 있는 비밀번호의 일치 여부를 확인해주는 메서드를 제공
PasswordEncoder 인터페이스를 구현한 클래스
생성자의 인자 값(verstion, strength, SecureRandom instance)을 통해서 해시의 강도를 조절 가능
메서드
encode(java.lang.CharSequence rawPassword)
패스워드를 암호화해주는 메서드
SHA-1, 8바이트로 결합된 해쉬, 랜덤 하게 생성된 솔트(salt)를 지원
똑같은 비밀번호를 인코딩하더라도 매번 다른 인코딩 된 문자열을 반환
matchers(java.lang.CharSequence rawPassword, java.lang.String encodePassword)
인코딩 되지 않은 패스워드와 인코딩 된 패스워드의 일치 여부를 확인
반환 타입은 boolean
upgradeEncoding(java.lang.String encodePassword)
더 나은 보안을 위해서 인코딩 된 암호를 다시 한번 더 인코딩해야 하는 경우에 사용
매개변수는 인코딩 필요 여부를 확인하고자 하는 인코딩 된 패스워드(String 타입)를 입력
반환 타입은 인코딩이 필요한 경우 true를, 필요하지 않은 경우는 false