@Entity(name = "users") // ์์ฝ์ด -> users๋ก ๋ณ๊ฒฝ
@Getter
@NoArgsConstructor
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false, unique = true)
private String email;
@Column(nullable = false)
@Enumerated(value = EnumType.STRING)
private UserRoleEnum role;
// ์์ฑ์
public User(String username, String password, String email, UserRoleEnum role) {
this.username = username;
this.password = password;
this.email = email;
this.role = role;
}
}
public enum UserRoleEnum {
// ์ฌ์ฉ์๊ถํ
USER,
// ๊ด๋ฆฌ์๊ถํ
ADMIN
}
public interface UserRepository extends JpaRepository<User, Long> {
}
@Getter
@Setter
public class SignupRequestDto {
private String username;
private String password;
private String email;
private boolean admin = false;
private String adminToken = "";
}
@Getter
@Setter
public class LoginRequestDto {
private String username;
private String password;
}
@Controller
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
// ํ์๊ฐ์
ํ์ด์ง๋ฐํ
@GetMapping("/signup")
public ModelAndView signupPage() {
return new ModelAndView("signup");
}
// ๋ก๊ทธ์ธ ํ์ด์ง๋ฐํ
@GetMapping("/login")
public ModelAndView loginPage() {
return new ModelAndView("login");
}
// ํ์๊ฐ์
@PostMapping("/signup")
public String signup(SignupRequestDto signupRequestDto) {
userService.signup(signupRequestDto);
return "redirect:/api/user/login";
}
}
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
// ADMIN_TOKEN
private static final String ADMIN_TOKEN = "AAABnvxRVklrnYxKZ0aHgTBcXukeZygoC";
// ํ์๊ฐ์
@Transactional
public void signup(SignupRequestDto signupRequestDto) {
// 1. RequestDto -> ID/PW ๊ฐ์ ธ์ด
String username = signupRequestDto.getUsername();
String password = signupRequestDto.getPassword();
// 2. ํ์์ค๋ณตํ์ธ
// Optional<> -> ๊ฒฐ๊ณผ null๊ฐ ํ์ฉ
Optional<User> duplicationTest = userRepository.findByUsername(username);
if (duplicationTest.isPresent()) {
throw new IllegalArgumentException("์ค๋ณต๋ ์ฌ์ฉ์๊ฐ ์กด์ฌํฉ๋๋ค");
}
// 3. RequestDto -> Email ๊ฐ์ ธ์ด
String email = signupRequestDto.getEmail();
// 4. ํ์Roleํ์ธ
UserRoleEnum role = UserRoleEnum.USER;
if (signupRequestDto.isAdmin()) {
// ADMIN_TOKEN ์ ํจ์ฑ๊ฒ์ฌ
if (! signupRequestDto.getAdminToken().equals(ADMIN_TOKEN)) {
throw new IllegalArgumentException("๊ด๋ฆฌ์ ์ํธ๊ฐ ์ผ์นํ์ง ์์ ๋ฑ๋ก์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค");
}
// ๊ด๋ฆฌ์์ํธ์ผ์น -> Role ๋ณ๊ฒฝ
role = UserRoleEnum.ADMIN;
}
// 5. ํ์์ ๋ณด -> Entity ์ด๊ธฐํ(์์ฑ์)
User user = new User(username, password, email, role);
// 6. Entity -> DB table ์ ์ฅ
userRepository.save(user);
}
}
public interface UserRepository extends JpaRepository<User, Long> {
// ํ์์ค๋ณตํ์ธ
Optional<User> findByUsername(String username);
}
@Controller
@RequiredArgsConstructor
@RequestMapping("/api/user")
public class UserController {
private final UserService userService;
// ํ์๊ฐ์
ํ์ด์ง๋ฐํ
@GetMapping("/signup")
public ModelAndView signupPage() {
return new ModelAndView("signup");
}
// ๋ก๊ทธ์ธ ํ์ด์ง๋ฐํ
@GetMapping("/login")
public ModelAndView loginPage() {
return new ModelAndView("login");
}
// ํ์๊ฐ์
@PostMapping("/signup")
public String signup(SignupRequestDto signupRequestDto) {
userService.signup(signupRequestDto);
return "redirect:/api/user/login";
}
// ๋ก๊ทธ์ธ
@PostMapping("/login")
public String login(LoginRequestDto loginRequestDto) {
userService.login(loginRequestDto);
return "redirect:/api/shop";
}
}
@Service
@RequiredArgsConstructor
public class UserService {
private final UserRepository userRepository;
// ADMIN_TOKEN
private static final String ADMIN_TOKEN = "AAABnvxRVklrnYxKZ0aHgTBcXukeZygoC";
// ํ์๊ฐ์
@Transactional
public void signup(SignupRequestDto signupRequestDto) {
// 1. RequestDto -> ID/PW ๊ฐ์ ธ์ด
String username = signupRequestDto.getUsername();
String password = signupRequestDto.getPassword();
// 2. ํ์์ค๋ณตํ์ธ
// Optional<> -> ๊ฒฐ๊ณผ null๊ฐ ํ์ฉ
Optional<User> duplicationTest = userRepository.findByUsername(username);
if (duplicationTest.isPresent()) {
throw new IllegalArgumentException("์ค๋ณต๋ ์ฌ์ฉ์๊ฐ ์กด์ฌํฉ๋๋ค");
}
// 3. RequestDto -> Email ๊ฐ์ ธ์ด
String email = signupRequestDto.getEmail();
// 4. ํ์Roleํ์ธ
UserRoleEnum role = UserRoleEnum.USER;
if (signupRequestDto.isAdmin()) {
// ADMIN_TOKEN ์ ํจ์ฑ๊ฒ์ฌ
if (! signupRequestDto.getAdminToken().equals(ADMIN_TOKEN)) {
throw new IllegalArgumentException("๊ด๋ฆฌ์ ์ํธ๊ฐ ์ผ์นํ์ง ์์ ๋ฑ๋ก์ด ๋ถ๊ฐ๋ฅํฉ๋๋ค");
}
// ๊ด๋ฆฌ์์ํธ์ผ์น -> Role ๋ณ๊ฒฝ
role = UserRoleEnum.ADMIN;
}
// 5. ํ์์ ๋ณด -> Entity ์ด๊ธฐํ(์์ฑ์)
User user = new User(username, password, email, role);
// 6. Entity -> DB table ์ ์ฅ
userRepository.save(user);
}
// ๋ก๊ทธ์ธ
@Transactional(readOnly = true)
public void login(LoginRequestDto loginRequestDto) {
// 1. RequestDto -> ID/PW ๊ฐ์ ธ์ด
String username = loginRequestDto.getUsername();
String password = loginRequestDto.getPassword();
// 2. ํ์์ ํจ์ฑ๊ฒ์ฌ
User user = userRepository.findByUsername(username).orElseThrow(
() -> new IllegalArgumentException("๋ฑ๋ก๋ ์ฌ์ฉ์๊ฐ ์์ต๋๋ค")
);
// 3. ๋น๋ฐ๋ฒํธ์ ํจ์ฑ๊ฒ์ฌ
if (! user.getPassword().equals(password)) {
throw new IllegalArgumentException("๋น๋ฐ๋ฒํธ๊ฐ ์ผ์นํ์ง ์์ต๋๋ค");
}
}
}
ํ์๊ฐ์ , ๋ก๊ทธ์ธ ๊ธฐ๋ฅ์ ์ ์์ ์ผ๋ก ์๋ํ๋, ์ ๋ณด๊ฐ ์ ์ง๋์ง ์์ ๐จ
ํ์๋ณ๋ก ๋ค๋ฅธ ์ํ์ ๋ณด์ฌ์ค ์ ์์
adminToken
โ ๊ณ์๋ ์ ์ก์ผ๋ก ๋
ธ์ถ์ด ์ฌ์, ๋ณด์์ด ์ฝํจ