Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/oauth redirect #63

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ public class MemberController {

@GetMapping("/oauth/kakao")
public SuccessResponse<TokenResponse> kakaoLogin(@Parameter(name = "code", description = "카카였 μΈμ¦μ„œλ²„μ—μ„œ 받은 인증 μ½”λ“œ", required = true)
@RequestParam String code) throws JsonProcessingException {
@RequestParam String code,
@Parameter(name = "redirect_uri", description = "λ¦¬λ‹€μ΄λ ‰νŠΈ uri", required = true)
@RequestParam String redirect_uri) throws JsonProcessingException {

String accessToken = kakaoMemberService.getAccessToken(code);
String accessToken = kakaoMemberService.getAccessToken(code, redirect_uri);

Pair<TokenResponse, Boolean> pair = kakaoMemberService.kakaoLogin(accessToken);

Expand All @@ -62,7 +64,6 @@ public SuccessResponse<TokenResponse> kakaoLogin(@Parameter(name = "code", descr
} else {
return SuccessResponse.success(pair.getLeft());
}

}

@Operation(summary = "ꡬ글 인증 μ„œλ²„λ₯Ό ν†΅ν•œ 둜그인",
Expand All @@ -85,9 +86,10 @@ public SuccessResponse<TokenResponse> kakaoLogin(@Parameter(name = "code", descr

@GetMapping("/oauth/google")
public SuccessResponse<TokenResponse> googleLogin(@Parameter(name = "code", description = "ꡬ글 μΈμ¦μ„œλ²„μ—μ„œ 받은 인증 μ½”λ“œ", required = true)
@RequestParam String code) throws JsonProcessingException {

String accessToken = googleMemberService.getAccessToken(code);
@RequestParam String code,
@Parameter(name = "redirect_uri", description = "λ¦¬λ‹€μ΄λ ‰νŠΈ uri", required = true)
@RequestParam String redirect_uri) throws JsonProcessingException {
String accessToken = googleMemberService.getAccessToken(code, redirect_uri);
Pair<TokenResponse, Boolean> pair = googleMemberService.googleLogin(accessToken);

if (pair.getRight()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ public class GoogleMemberService {
private String clientId;
@Value("${spring.security.oauth2.client.registration.google.client-secret}")
private String clientSecret;
@Value("${spring.security.oauth2.client.registration.google.redirect-uri}")
private String googleRedirectUri;

// 카카였 μ—‘μ„ΈμŠ€ 토큰 λ°œκΈ‰
public String getAccessToken(String code) throws JsonProcessingException {
public String getAccessToken(String code, String redirectUri) throws JsonProcessingException {
String reqUrl = "https://oauth2.googleapis.com/token";

RestTemplate rt = new RestTemplate();
Expand All @@ -51,7 +49,7 @@ public String getAccessToken(String code) throws JsonProcessingException {
params.add("code", code);
params.add("client_id", clientId);
params.add("client_secret", clientSecret);
params.add("redirect_uri", googleRedirectUri);
params.add("redirect_uri", redirectUri);
params.add("grant_type", "authorization_code");

//http λ°”λ””(params)와 http 헀더(headers)λ₯Ό 가진 μ—”ν‹°ν‹°
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ public class KakaoMemberService {

@Value("${spring.security.oauth2.client.registration.kakao.client-id}")
private String clientId;
@Value("${spring.security.oauth2.client.registration.kakao.client-secret}")
private String clientSecret;
@Value("${spring.security.oauth2.client.registration.kakao.redirect-uri}")
private String kakaoRedirectUri;

public Pair<TokenResponse, Boolean> kakaoLogin(String accessToken) throws JsonProcessingException {

Expand All @@ -49,7 +45,7 @@ public Pair<TokenResponse, Boolean> kakaoLogin(String accessToken) throws JsonPr


// 카카였 μ—‘μ„ΈμŠ€ 토큰 λ°œκΈ‰
public String getAccessToken(String code) throws JsonProcessingException {
public String getAccessToken(String code, String redirectUri) throws JsonProcessingException {
String reqUrl = "https://kauth.kakao.com/oauth/token";

RestTemplate rt = new RestTemplate();
Expand All @@ -62,7 +58,7 @@ public String getAccessToken(String code) throws JsonProcessingException {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "authorization_code");
params.add("client_id", clientId);
params.add("redirect_uri", kakaoRedirectUri);
params.add("redirect_uri", redirectUri);
params.add("code", code);

//http λ°”λ””(params)와 http 헀더(headers)λ₯Ό 가진 μ—”ν‹°ν‹°
Expand Down