Skip to content

Commit

Permalink
๐Ÿ› ๋ฒ„๊ทธ ์ˆ˜์ • (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jindongleee authored Nov 2, 2024
1 parent e8089dd commit e0f3933
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -18,7 +20,12 @@ public class OpenAiController implements OpenAiControllerApi {
private final OpenAiService openAiService;

@PostMapping("/test")
public SuccessResponse<CustomOpenAiClientResponse> requestToOpenAi(@RequestBody CustomOpenAiClientRequest request) {
public SuccessResponse<CustomOpenAiClientResponse> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -13,5 +14,7 @@ public interface OpenAiControllerApi {

@Operation(summary = "Open AI ํ…Œ์ŠคํŠธ์šฉ")
@PostMapping
SuccessResponse<CustomOpenAiClientResponse> requestToOpenAi(@RequestBody CustomOpenAiClientRequest request);
SuccessResponse<CustomOpenAiClientResponse> requestToOpenAi(Authentication authentication, @RequestBody CustomOpenAiClientRequest request);


}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public SuccessResponse<String> testJwt(Authentication authentication) {
}
else {
CustomOAuth2User customOAuth2User = (CustomOAuth2User) authentication.getPrincipal();
return SuccessResponse.of(customOAuth2User.getNickname());
return SuccessResponse.of(customOAuth2User.getUsername());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public String getName() {
return userDto.getName();
}

public String getNickname() {
return userDto.getNickname();
public String getUsername() {
return userDto.getUsername();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
public class UserDto {
private String role;
private String name;
private String nickname;
private String username;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Iterator<? extends GrantedAuthority> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 ๋งŒ๋ฃŒ์‹œ๊ฐ„
Expand Down

0 comments on commit e0f3933

Please sign in to comment.