@PostMapping("/join")
public @Response String join(User user){
System.out.println(user);
return "join";
}
UserRepository
※ 간단하게 하기위해서 따로 service 만들지 않고 repository만 만들었다.
// CRUD 함수를 JpaRepository가 들고 있음
// @Repository라는 어노테이션이 없어도 IoC가 된다. why? JpaRepository을 상속 받았기 때문에
public interface UserRepository extends JpaRepository<User, Integer> {
}
IndexController
userRepository.save(user); 로 하면 회원가입은 잘되지만 시큐리티 로그인 할 수 없다. why? 이유는 비번이 암호화가 안되어있어 비밀번호가 맞지않는다고 나와 로그인이 불가하다.
@Autowired
private UserRepository userRepository;
@PostMapping("/join")
public @Response String join(User user){
System.out.println(user);
user.setRole("ROLE_USER");
userRepository.save(user); // 회원가입 잘됨. but : 1234 -> 시큐리티로 로그인 할 수 x, 이유는 비번이 암호화가 안되어있기 때문에
}
SecurityConfig
pw암호화해줄 코드 추가해준다.
// 해당 메서드의 리턴되는 오브젝트를 IoC로 등록해준다.
@Bean
public BCryptPasswordEncoder encodePwd() {
return new BCryptPasswordEncoder();
}
IndexController
join() 코드추가 및 BCryptPasswordEncoder DI
@PostMapping("/join")
public @Response String join(User user){
System.out.println(user);
user.setRole("ROLE_USER");
String rawPassword = user.getPassword();
String encPassword = bCryptPasswordEncoder.encode(rawPassword);
user.setPassword(encPassword);
userRepository.save(user); // 회원가입 잘됨. but : 1234 -> 시큐리티로 로그인 할 수 x, 이유는 비번이 암호화가 안되어있기 때문에
}