๐Ÿ“ GitHub ๋ฐ”๋กœ๊ฐ€๊ธฐ

1 User ์„ค๊ณ„

1๏ธโƒฃ entity > User.java

@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;
    }

}

2๏ธโƒฃ entity > UserRoleEnum.java

public enum UserRoleEnum {

    // ์‚ฌ์šฉ์ž๊ถŒํ•œ
    USER,

    // ๊ด€๋ฆฌ์ž๊ถŒํ•œ
    ADMIN

}

3๏ธโƒฃ repository > UserRepository.java

public interface UserRepository extends JpaRepository<User, Long> {
}

2 UserDto ์„ค๊ณ„

1๏ธโƒฃ dto > SignupRequestDto.java

@Getter
@Setter
public class SignupRequestDto {

    private String username;

    private String password;

    private String email;

    private boolean admin = false;

    private String adminToken = "";

}

2๏ธโƒฃ dto > LoginRequestDto.java

@Getter
@Setter
public class LoginRequestDto {

    private String username;

    private String password;

}

3 ํšŒ์›๊ฐ€์ž… ์„ค๊ณ„

1๏ธโƒฃ controller > UserController.java

@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";

    }

}

2๏ธโƒฃ service > UserService.java

@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);

    }

}

3๏ธโƒฃ repository > UserRepository.java

public interface UserRepository extends JpaRepository<User, Long> {

    // ํšŒ์›์ค‘๋ณตํ™•์ธ
    Optional<User> findByUsername(String username);

}

4 ๋กœ๊ทธ์ธ ์„ค๊ณ„

1๏ธโƒฃ controller > UserController.java

@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";

    }

}

2๏ธโƒฃ service > UserService.java

@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("๋น„๋ฐ€๋ฒˆํ˜ธ๊ฐ€ ์ผ์น˜ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค");
        }

    }

}

5 ํ•œ๊ณ„์ 

  • ํšŒ์›๊ฐ€์ž…, ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์€ ์ •์ƒ์ ์œผ๋กœ ์ž‘๋™ํ•˜๋‚˜, ์ •๋ณด๊ฐ€ ์œ ์ง€๋˜์ง€ ์•Š์Œ ๐Ÿšจ

  • ํšŒ์›๋ณ„๋กœ ๋‹ค๋ฅธ ์ƒํ’ˆ์„ ๋ณด์—ฌ์ค„ ์ˆ˜ ์—†์Œ

  • adminToken โ†’ ๊ณ„์†๋œ ์ „์†ก์œผ๋กœ ๋…ธ์ถœ์ด ์‰ฌ์›Œ, ๋ณด์•ˆ์ด ์•ฝํ•จ

profile
๐ŸฑSunyeon-Jeong, mallang developer๐Ÿฐ

0๊ฐœ์˜ ๋Œ“๊ธ€