From e0f3933d87fbda5264a987181fe45e45b32e33d4 Mon Sep 17 00:00:00 2001 From: Eddy <149242983+Jindongleee@users.noreply.github.com> Date: Sun, 3 Nov 2024 00:23:21 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=EB=B2=84=EA=B7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20(#27)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openai/controller/OpenAiController.java | 9 ++++++++- .../openai/controller/OpenAiControllerApi.java | 5 ++++- .../gather_back_end/test/controller/TestController.java | 2 +- .../gather_back_end/util/jwt/dto/CustomOAuth2User.java | 4 ++-- .../example/gather_back_end/util/jwt/dto/UserDto.java | 2 +- .../util/jwt/handler/CustomSuccessHandler.java | 4 ++-- .../util/jwt/service/CustomOAuth2UserServiceImpl.java | 4 ++-- .../example/gather_back_end/util/jwt/util/JwtFilter.java | 4 ++-- .../example/gather_back_end/util/jwt/util/JwtUtil.java | 8 ++++---- 9 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/example/gather_back_end/openai/controller/OpenAiController.java b/src/main/java/org/example/gather_back_end/openai/controller/OpenAiController.java index e2c07a5..82f5175 100644 --- a/src/main/java/org/example/gather_back_end/openai/controller/OpenAiController.java +++ b/src/main/java/org/example/gather_back_end/openai/controller/OpenAiController.java @@ -4,7 +4,9 @@ import org.example.gather_back_end.openai.dto.CustomOpenAiClientRequest; import org.example.gather_back_end.openai.dto.CustomOpenAiClientResponse; import org.example.gather_back_end.openai.service.OpenAiService; +import org.example.gather_back_end.util.jwt.dto.CustomOAuth2User; import org.example.gather_back_end.util.response.SuccessResponse; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -18,7 +20,12 @@ public class OpenAiController implements OpenAiControllerApi { private final OpenAiService openAiService; @PostMapping("/test") - public SuccessResponse requestToOpenAi(@RequestBody CustomOpenAiClientRequest request) { + public SuccessResponse requestToOpenAi(Authentication authentication,@RequestBody CustomOpenAiClientRequest request) { + CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal(); + if(customOAuth2User == null) { + return null; + } + CustomOpenAiClientResponse response = openAiService.getOpenAiResponse(request); return SuccessResponse.of(response); } diff --git a/src/main/java/org/example/gather_back_end/openai/controller/OpenAiControllerApi.java b/src/main/java/org/example/gather_back_end/openai/controller/OpenAiControllerApi.java index 64cebfe..03315e7 100644 --- a/src/main/java/org/example/gather_back_end/openai/controller/OpenAiControllerApi.java +++ b/src/main/java/org/example/gather_back_end/openai/controller/OpenAiControllerApi.java @@ -5,6 +5,7 @@ import org.example.gather_back_end.openai.dto.CustomOpenAiClientRequest; import org.example.gather_back_end.openai.dto.CustomOpenAiClientResponse; import org.example.gather_back_end.util.response.SuccessResponse; +import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -13,5 +14,7 @@ public interface OpenAiControllerApi { @Operation(summary = "Open AI 테스트용") @PostMapping - SuccessResponse requestToOpenAi(@RequestBody CustomOpenAiClientRequest request); + SuccessResponse requestToOpenAi(Authentication authentication, @RequestBody CustomOpenAiClientRequest request); + + } diff --git a/src/main/java/org/example/gather_back_end/test/controller/TestController.java b/src/main/java/org/example/gather_back_end/test/controller/TestController.java index 745634c..1aaf0f6 100644 --- a/src/main/java/org/example/gather_back_end/test/controller/TestController.java +++ b/src/main/java/org/example/gather_back_end/test/controller/TestController.java @@ -29,7 +29,7 @@ public SuccessResponse testJwt(Authentication authentication) { } else { CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal(); - return SuccessResponse.of(customOAuth2User.getNickname()); + return SuccessResponse.of(customOAuth2User.getUsername()); } } } diff --git a/src/main/java/org/example/gather_back_end/util/jwt/dto/CustomOAuth2User.java b/src/main/java/org/example/gather_back_end/util/jwt/dto/CustomOAuth2User.java index 0b4347b..57fc501 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/dto/CustomOAuth2User.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/dto/CustomOAuth2User.java @@ -38,7 +38,7 @@ public String getName() { return userDto.getName(); } - public String getNickname() { - return userDto.getNickname(); + public String getUsername() { + return userDto.getUsername(); } } diff --git a/src/main/java/org/example/gather_back_end/util/jwt/dto/UserDto.java b/src/main/java/org/example/gather_back_end/util/jwt/dto/UserDto.java index 58e747e..f4525e3 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/dto/UserDto.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/dto/UserDto.java @@ -10,5 +10,5 @@ public class UserDto { private String role; private String name; - private String nickname; + private String username; } diff --git a/src/main/java/org/example/gather_back_end/util/jwt/handler/CustomSuccessHandler.java b/src/main/java/org/example/gather_back_end/util/jwt/handler/CustomSuccessHandler.java index 0a0e554..78487d6 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/handler/CustomSuccessHandler.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/handler/CustomSuccessHandler.java @@ -28,14 +28,14 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo // OAuth2User CustomOAuth2User customUserDetails = (CustomOAuth2User) authentication.getPrincipal(); - String nickname = customUserDetails.getNickname(); + String username = customUserDetails.getUsername(); Collection authorities = authentication.getAuthorities(); Iterator iterator = authorities.iterator(); GrantedAuthority auth = iterator.next(); String role = auth.getAuthority(); - String token = jwtUtil.createJwt(nickname, role, 60 * 60 * 1000L); + String token = jwtUtil.createJwt(username, role, 60 * 60 * 1000L); response.sendRedirect("https://www.to-gather.info?code=" + "Bearer " + token); } diff --git a/src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserServiceImpl.java b/src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserServiceImpl.java index 7668b40..7ce8f66 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserServiceImpl.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/service/CustomOAuth2UserServiceImpl.java @@ -76,7 +76,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic ); UserDto userDto = UserDto.builder() - .nickname(nickname) + .username(username) .name(oAuth2Response.getName()) .role("ROLE_USER") .build(); @@ -90,7 +90,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) throws OAuth2Authentic userRepository.save(existData); UserDto userDto = UserDto.builder() - .nickname(existData.getNickname()) + .username(existData.getUsername()) .name(existData.getName()) .role(existData.getRole()) .build(); diff --git a/src/main/java/org/example/gather_back_end/util/jwt/util/JwtFilter.java b/src/main/java/org/example/gather_back_end/util/jwt/util/JwtFilter.java index a49a7e2..27424e6 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/util/JwtFilter.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/util/JwtFilter.java @@ -49,12 +49,12 @@ protected void doFilterInternal(HttpServletRequest request,HttpServletResponse r } // 토큰에서 username & role 획득 - String nickname = jwtUtil.getNickname(token); + String username = jwtUtil.getUsername(token); String role = jwtUtil.getRole(token); // userDto 생성하여 값 set UserDto userDto = UserDto.builder() - .nickname(nickname) + .username(username) .role(role) .build(); diff --git a/src/main/java/org/example/gather_back_end/util/jwt/util/JwtUtil.java b/src/main/java/org/example/gather_back_end/util/jwt/util/JwtUtil.java index 06c9fc7..38fb3b3 100644 --- a/src/main/java/org/example/gather_back_end/util/jwt/util/JwtUtil.java +++ b/src/main/java/org/example/gather_back_end/util/jwt/util/JwtUtil.java @@ -21,14 +21,14 @@ public JwtUtil(@Value("${spring.jwt.secret}") String secret) { } // 현재 사용중인 username 받기 - public String getNickname(String token) { + public String getUsername(String token) { return Jwts .parser() .verifyWith(secretKey) .build() .parseSignedClaims(token) .getPayload() - .get("nickname", String.class); + .get("username", String.class); } public String getRole(String token) { @@ -52,9 +52,9 @@ public Boolean isExpired(String token) { .before(new Date()); } - public String createJwt(String nickname, String role, Long expiredMs) { + public String createJwt(String username, String role, Long expiredMs) { return Jwts.builder() - .claim("nickname", nickname) + .claim("username", username) .claim("role", role) .issuedAt(new Date(System.currentTimeMillis())) .expiration(new Date(System.currentTimeMillis() + expiredMs)) // token 만료시간