From 5cc4f48b51d04aa283e63f30a3d1d1fe89763713 Mon Sep 17 00:00:00 2001 From: AWESOMEGUY5 Date: Thu, 25 Jul 2024 00:28:50 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=EB=A6=AC=ED=94=84=EB=A0=88=EC=8B=9C=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EA=B5=AC=C3=A3=ED=98=84=20=C3=AB=EB=B0=8F?= =?UTF-8?q?=20=ED=8C=8C=EC=9D=BC=20=EC=A1=B0=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/auth/config/SecurityConfig.java | 2 +- .../auth/googleAuth/AuthLoginController.java | 26 ------------------- .../auth/googleAuth/AuthLoginService.java | 2 +- .../global/auth/jwt/JwtTokenProvider.java | 24 ++++++++--------- .../user/api/UserController.java | 4 +-- .../api/dto/request/UserSignInReqDto.java | 4 ++- .../api/dto/request/UserSignUpReqDto.java | 2 ++ .../api/dto/response/UserSignInResDto.java | 6 ++--- .../user/application/UserService.java | 9 ++++--- .../user/domain/Role.java | 2 +- .../user/domain/User.java | 8 +++--- 11 files changed, 35 insertions(+), 54 deletions(-) delete mode 100644 src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginController.java diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/config/SecurityConfig.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/config/SecurityConfig.java index cc47280..609b37e 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/config/SecurityConfig.java +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/config/SecurityConfig.java @@ -32,7 +32,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .csrf(AbstractHttpConfigurer::disable) /*.authorizeHttpRequests(authorize -> authorize .requestMatchers("/sign-up/**", "/sign-in/**", "/login/oauth2/**").permitAll() - .requestMatchers("/user/**").hasRole("USER") + .requestMatchers("/user/**").hasRole("ROLE_USER") )*/ .authorizeHttpRequests(authorizeRequests -> authorizeRequests .anyRequest().permitAll()) // 스웨거 안 열림 이슈로 다 허용으로 바꿈 diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginController.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginController.java deleted file mode 100644 index f648bfc..0000000 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginController.java +++ /dev/null @@ -1,26 +0,0 @@ -//package org.likelion.likelion_12th_team05.global.auth.googleAuth; -// -//import lombok.RequiredArgsConstructor; -//import org.springframework.web.bind.annotation.GetMapping; -//import org.springframework.web.bind.annotation.RequestMapping; -//import org.springframework.web.bind.annotation.RequestParam; -//import org.springframework.web.bind.annotation.RestController; -// -//@RestController -//@RequiredArgsConstructor -//@RequestMapping("/login/oauth2") -//public class AuthLoginController { -// private final AuthLoginService authLoginService; -// -// @GetMapping("/code/google") -// public GoogleToken googleCallback(@RequestParam(name = "code") String code) { -// String googleAccessToken = authLoginService.getGoogleAccessToken(code); -// return signUpOrSignIn(googleAccessToken); -// } -// -// public GoogleToken signUpOrSignIn(String googleAccessToken) { -// return authLoginService.signUpOrSignIn(googleAccessToken); -// } -// -//} -// 주석처리를 했는데 머지 하고 통합 시 문제 없을 경우 다음 pr에 제거할 예정. 해당 코드는 userSignUpReqDto로 넘어감. diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginService.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginService.java index 80abce4..8d96332 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/AuthLoginService.java @@ -62,7 +62,7 @@ public GoogleToken signUpOrSignIn(String googleAccessToken) { userRepository.save(User.builder() .email(userInfo.getEmail()) .name(userInfo.getName()) - .role(Role.USER) + .role(Role.ROLE_USER) .build()) ); return googleTokenProvider.generateToken(user); diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java index abe3aaa..20269bf 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java @@ -55,18 +55,18 @@ public String generateToken(String email) { .compact(); } -// public String refreshToken(String email) { -// Date date = new Date(); -// -// Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime) * 24); -// -// return Jwts.builder() -// .setSubject(email) -// .setIssuedAt(date) -// .setExpiration(expireDate) -// .signWith(key, SignatureAlgorithm.HS256) -// .compact(); -// } + public String refreshToken(String email) { + Date date = new Date(); + + Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime) * 24 * 7); + + return Jwts.builder() + .setSubject(email) + .setIssuedAt(date) + .setExpiration(expireDate) + .signWith(key, SignatureAlgorithm.HS256) + .compact(); + } public boolean validateToken(String token) { // 토큰을 검증하는 부분 diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java index d96b7c3..a8eece3 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java @@ -23,9 +23,9 @@ public UserController(UserService userService, AuthLoginService authLoginService // 자체 회원가입 @PostMapping("/sign-up") - public ApiResponseTemplate userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto) { + public ApiResponseTemplate userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto) { userService.userSignUp(userSignUpReqDto); - return ApiResponseTemplate.successWithNoContent(SuccessCode.USER_SIGNUP_SUCCESS); + return ApiResponseTemplate.successResponse(userSignUpReqDto.refreshToken(), SuccessCode.USER_SIGNUP_SUCCESS); } @GetMapping("/code/google") diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignInReqDto.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignInReqDto.java index 7a61f58..440051f 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignInReqDto.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignInReqDto.java @@ -12,6 +12,8 @@ public record UserSignInReqDto( @NotBlank(message = "비밀번호를 필수로 입력해야 합니다.") @Size(min = 8, message = "8자 이상 입력하세요.") - String password + String password, + + String refreshToken ) { } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignUpReqDto.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignUpReqDto.java index 8ee1c3a..8ada2b2 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignUpReqDto.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/request/UserSignUpReqDto.java @@ -19,6 +19,8 @@ public record UserSignUpReqDto( @Size(min = 8, message = "8자 이상 입력하세요.") String password, + String refreshToken, + Role role ) { } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java index 4a9fd3a..ddccecd 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java @@ -4,11 +4,11 @@ import org.likelion.likelion_12th_team05.user.domain.User; @Builder -public record UserSignInResDto(String name, String email, String token) { - public static UserSignInResDto of(User user, String token) { +public record UserSignInResDto(String name, String email, String refreshToken) { + public static UserSignInResDto of(User user, String refreshToken) { return UserSignInResDto.builder() .name(user.getName()) .email(user.getEmail()) - .token(token).build(); + .refreshToken(refreshToken).build(); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java b/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java index 390d846..28deda7 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java @@ -32,11 +32,13 @@ public void userSignUp(UserSignUpReqDto userSignUpReqDto) { if (userRepository.existsByEmail(userSignUpReqDto.email())) { throw new IllegalArgumentException("이미 존재하는 이메일입니다."); } + User user = User.builder() .name(userSignUpReqDto.name()) .email(userSignUpReqDto.email()) .password(passwordEncoder.encode(userSignUpReqDto.password())) - .role(Role.USER) + .refreshToken(tokenProvider.refreshToken(userSignUpReqDto.email())) + .role(Role.ROLE_USER) .build(); userRepository.save(user); @@ -45,13 +47,12 @@ public void userSignUp(UserSignUpReqDto userSignUpReqDto) { public UserSignInResDto userSignIn(UserSignInReqDto userSignUpReqDto) { User user = userRepository.findByEmail(userSignUpReqDto.email()) .orElseThrow(() -> new IllegalArgumentException("이메일이나 패스워드가 일치하지 않습니다.")); - String token = tokenProvider.generateToken(user.getEmail()); -// String refreshToken = tokenProvider.refreshToken(user.getEmail()); + String accessToken = tokenProvider.generateToken(user.getEmail()); if (!passwordEncoder.matches(userSignUpReqDto.password(), user.getPassword())) { throw new IllegalArgumentException("이메일이나 패스워드가 일치하지 않습니다."); } - return UserSignInResDto.of(user, token); + return UserSignInResDto.of(user, accessToken); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/domain/Role.java b/src/main/java/org/likelion/likelion_12th_team05/user/domain/Role.java index a136e12..a52a851 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/domain/Role.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/domain/Role.java @@ -1,5 +1,5 @@ package org.likelion.likelion_12th_team05.user.domain; public enum Role { - USER + ROLE_USER } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java b/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java index 2363bfe..f19ee41 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java @@ -24,9 +24,10 @@ public class User { private String name; private String email; private String password; + private String refreshToken; + + @Enumerated(value = EnumType.STRING) private Role role; -// @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) -// private List curations = new ArrayList<>(); @OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private List curations = new ArrayList<>(); @@ -38,10 +39,11 @@ public class User { private List likes = new ArrayList<>(); @Builder - private User(String name, String email, String password, Role role){ + private User(String name, String email, String password, String refreshToken, Role role){ this.name = name; this.email = email; this.password = password; + this.refreshToken = refreshToken; this.role = role; } } From 4fe14df8c618c26833189cad6b785ac1714d618a Mon Sep 17 00:00:00 2001 From: AWESOMEGUY5 Date: Thu, 25 Jul 2024 17:11:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EB=A6=AC=ED=94=84=EB=A0=88=EC=8B=9C=20?= =?UTF-8?q?=ED=86=A0=ED=81=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/auth/googleAuth/GoogleTokenProvider.java | 2 +- .../global/auth/jwt/JwtTokenProvider.java | 8 ++++++-- .../global/auth/jwt/TokenDto.java | 13 +++++++++++++ .../user/api/UserController.java | 5 +++-- .../user/api/dto/response/UserSignInResDto.java | 5 +++-- .../user/application/UserService.java | 9 ++++++--- .../likelion_12th_team05/user/domain/User.java | 5 ++++- 7 files changed, 36 insertions(+), 11 deletions(-) create mode 100644 src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/TokenDto.java diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/GoogleTokenProvider.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/GoogleTokenProvider.java index b8b1847..bc392ab 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/GoogleTokenProvider.java +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/googleAuth/GoogleTokenProvider.java @@ -45,7 +45,7 @@ public GoogleToken generateToken(User user) { .setSubject(user.getEmail()) .claim(AUTHORITIES_KEY, user.getRole().name()) .setExpiration(tokenExpiredTime) - .signWith(key, SignatureAlgorithm.HS512) + .signWith(key, SignatureAlgorithm.HS256) .compact(); return GoogleToken.builder() diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java index 20269bf..ec34769 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/JwtTokenProvider.java @@ -47,12 +47,14 @@ public String generateToken(String email) { Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime)); - return Jwts.builder() + String accessToken = Jwts.builder() .setSubject(email) .setIssuedAt(date) .setExpiration(expireDate) .signWith(key, SignatureAlgorithm.HS256) .compact(); + + return accessToken; } public String refreshToken(String email) { @@ -60,12 +62,14 @@ public String refreshToken(String email) { Date expireDate = new Date(date.getTime() + Long.parseLong(tokenExpireTime) * 24 * 7); - return Jwts.builder() + String refreshToken = Jwts.builder() .setSubject(email) .setIssuedAt(date) .setExpiration(expireDate) .signWith(key, SignatureAlgorithm.HS256) .compact(); + + return refreshToken; } public boolean validateToken(String token) { diff --git a/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/TokenDto.java b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/TokenDto.java new file mode 100644 index 0000000..510ede2 --- /dev/null +++ b/src/main/java/org/likelion/likelion_12th_team05/global/auth/jwt/TokenDto.java @@ -0,0 +1,13 @@ +package org.likelion.likelion_12th_team05.global.auth.jwt; + +import lombok.Builder; + +@Builder +public record TokenDto( + String grantType, + + String accessToken, + + String refreshToken +) { +} diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java index a8eece3..7417218 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/UserController.java @@ -5,6 +5,7 @@ import org.likelion.likelion_12th_team05.config.ApiResponseTemplate; import org.likelion.likelion_12th_team05.global.auth.googleAuth.AuthLoginService; import org.likelion.likelion_12th_team05.global.auth.googleAuth.GoogleToken; +import org.likelion.likelion_12th_team05.global.auth.jwt.TokenDto; import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignInReqDto; import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignUpReqDto; import org.likelion.likelion_12th_team05.user.api.dto.response.UserSignInResDto; @@ -23,8 +24,8 @@ public UserController(UserService userService, AuthLoginService authLoginService // 자체 회원가입 @PostMapping("/sign-up") - public ApiResponseTemplate userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto) { - userService.userSignUp(userSignUpReqDto); + public ApiResponseTemplate userSignUp(@RequestBody @Valid UserSignUpReqDto userSignUpReqDto, TokenDto tokenDto) { + userService.userSignUp(userSignUpReqDto, tokenDto); return ApiResponseTemplate.successResponse(userSignUpReqDto.refreshToken(), SuccessCode.USER_SIGNUP_SUCCESS); } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java index ddccecd..2e9a4f3 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/api/dto/response/UserSignInResDto.java @@ -4,11 +4,12 @@ import org.likelion.likelion_12th_team05.user.domain.User; @Builder -public record UserSignInResDto(String name, String email, String refreshToken) { - public static UserSignInResDto of(User user, String refreshToken) { +public record UserSignInResDto(String name, String email, String accessToken, String refreshToken) { + public static UserSignInResDto of(User user, String accessToken, String refreshToken) { return UserSignInResDto.builder() .name(user.getName()) .email(user.getEmail()) + .accessToken(accessToken) .refreshToken(refreshToken).build(); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java b/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java index 28deda7..5dfca85 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/application/UserService.java @@ -2,6 +2,7 @@ import lombok.extern.slf4j.Slf4j; import org.likelion.likelion_12th_team05.global.auth.jwt.JwtTokenProvider; +import org.likelion.likelion_12th_team05.global.auth.jwt.TokenDto; import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignInReqDto; import org.likelion.likelion_12th_team05.user.api.dto.request.UserSignUpReqDto; import org.likelion.likelion_12th_team05.user.api.dto.response.UserSignInResDto; @@ -28,7 +29,7 @@ public UserService(UserRepository userRepository, PasswordEncoder passwordEncode // 회원가입 @Transactional - public void userSignUp(UserSignUpReqDto userSignUpReqDto) { + public void userSignUp(UserSignUpReqDto userSignUpReqDto, TokenDto tokenDto) { if (userRepository.existsByEmail(userSignUpReqDto.email())) { throw new IllegalArgumentException("이미 존재하는 이메일입니다."); } @@ -37,7 +38,8 @@ public void userSignUp(UserSignUpReqDto userSignUpReqDto) { .name(userSignUpReqDto.name()) .email(userSignUpReqDto.email()) .password(passwordEncoder.encode(userSignUpReqDto.password())) - .refreshToken(tokenProvider.refreshToken(userSignUpReqDto.email())) + .accessToken(tokenDto.accessToken()) + .refreshToken(tokenDto.refreshToken()) .role(Role.ROLE_USER) .build(); @@ -48,11 +50,12 @@ public UserSignInResDto userSignIn(UserSignInReqDto userSignUpReqDto) { User user = userRepository.findByEmail(userSignUpReqDto.email()) .orElseThrow(() -> new IllegalArgumentException("이메일이나 패스워드가 일치하지 않습니다.")); String accessToken = tokenProvider.generateToken(user.getEmail()); + String refreshToken = tokenProvider.refreshToken(user.getEmail()); if (!passwordEncoder.matches(userSignUpReqDto.password(), user.getPassword())) { throw new IllegalArgumentException("이메일이나 패스워드가 일치하지 않습니다."); } - return UserSignInResDto.of(user, accessToken); + return UserSignInResDto.of(user, accessToken, refreshToken); } } diff --git a/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java b/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java index f19ee41..2fbfd38 100644 --- a/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java +++ b/src/main/java/org/likelion/likelion_12th_team05/user/domain/User.java @@ -24,6 +24,8 @@ public class User { private String name; private String email; private String password; + + private String accessToken; private String refreshToken; @Enumerated(value = EnumType.STRING) @@ -39,10 +41,11 @@ public class User { private List likes = new ArrayList<>(); @Builder - private User(String name, String email, String password, String refreshToken, Role role){ + private User(String name, String email, String password, String accessToken, String refreshToken, Role role){ this.name = name; this.email = email; this.password = password; + this.accessToken = accessToken; this.refreshToken = refreshToken; this.role = role; }