회원가입 로직
JoinDTO
- JoinDTO
package com.example.springjwt.dto;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class JoinDTO {
private String username;
private String password;
}
JoinController
- joinController
package com.example.springjwt.controller;
import com.example.springjwt.dto.JoinDTO;
import com.example.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";
}
}
JoinService
- JoinService
package com.example.springjwt.service;
import com.example.springjwt.dto.JoinDTO;
import com.example.springjwt.entity.UserEntity;
import com.example.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);
}
}
UserRepository
- UserRepository
package com.example.springjwt.repository;
import com.example.springjwt.entity.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UserEntity, Integer> {
Boolean existsByUsername(String username);
}
실행
- 1. http://localhost:8080/join 으로 POST 요청
- 2. DB에 데이터 save 됐는 지 확인.
참고
https://www.devyummi.com/page?id=668d525886d3d643f4c18ba0