Skip to content

Commit 4c8777b

Browse files
authored
Merge pull request #74 from checkmo2025/refactor/73/SocialLogin
♻️refactor: 소셜 로그인 성공 시 리다이렉트 URI를 프론트쪽으로 변경
2 parents 12dc7aa + 96129b6 commit 4c8777b

File tree

4 files changed

+24
-30
lines changed

4 files changed

+24
-30
lines changed

src/main/java/checkmo/domain/member/service/security/jwt/JwtCookieUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class JwtCookieUtil {
1111
public void addTokenToCookie(HttpServletResponse response, String cookieName, String token, int maxAge) {
1212
Cookie cookie = new Cookie(cookieName, token);
1313
cookie.setHttpOnly(true); // 클라이언트 스크립트에서 접근 불가
14-
cookie.setAttribute("SameSite", "Lax");
14+
cookie.setAttribute("SameSite", "None");
1515
cookie.setPath("/"); // 모든 경로에서 접근 가능
1616
cookie.setMaxAge(maxAge);
1717
cookie.setSecure(true); // HTTPS에서만 전송하도록 추가
@@ -36,7 +36,7 @@ public void deleteTokenFromCookie(HttpServletResponse response, String cookieNam
3636
cookie.setMaxAge(0);
3737
cookie.setPath("/");
3838
cookie.setHttpOnly(true);
39-
cookie.setAttribute("SameSite", "Lax");
39+
cookie.setAttribute("SameSite", "None");
4040
response.addCookie(cookie);
4141
}
4242
}

src/main/java/checkmo/domain/member/service/security/oauth2/OAuth2AuthenticationSuccessHandler.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package checkmo.domain.member.service.security.oauth2;
22

3+
import checkmo.domain.member.entity.Member;
4+
import checkmo.domain.member.service.security.auth.PrincipalDetails;
35
import checkmo.domain.member.service.security.jwt.JwtLoginProcessor;
46
import jakarta.servlet.http.HttpServletRequest;
57
import jakarta.servlet.http.HttpServletResponse;
68
import java.io.IOException;
79
import lombok.RequiredArgsConstructor;
10+
import org.springframework.beans.factory.annotation.Value;
811
import org.springframework.security.core.Authentication;
912
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
1013
import org.springframework.stereotype.Component;
14+
import org.springframework.web.util.UriComponentsBuilder;
1115

1216
/**
1317
* 소셜 로그인 성공 후의 성공 처리 핸들러
@@ -22,16 +26,27 @@ public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationS
2226

2327
private final JwtLoginProcessor jwtLoginProcessor;
2428

29+
@Value("${app.oauth2.redirect.base-uri}")
30+
private String baseUri;
31+
2532
@Override
2633
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
2734

2835
// JWT 토큰 생성 및 쿠키 설정
2936
jwtLoginProcessor.processLogin(response, authentication);
3037

38+
PrincipalDetails principalDetails = (PrincipalDetails) authentication.getPrincipal();
39+
Member member = principalDetails.getMember();
40+
41+
String path = member.isProfileCompleted() ? "home" : "profile";
42+
3143
// 기본 리다이렉트 URI
32-
String redirectUri = "/api/auth/redirect/oauth2";
44+
String targetUrl = UriComponentsBuilder.fromUriString(baseUri)
45+
.pathSegment(path)
46+
.build().toUriString();
3347

3448
// 성공 후 리다이렉트 URL 설정
35-
getRedirectStrategy().sendRedirect(request, response, redirectUri);
49+
clearAuthenticationAttributes(request);
50+
getRedirectStrategy().sendRedirect(request, response, targetUrl);
3651
}
3752
}

src/main/java/checkmo/domain/member/web/controller/AuthController.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,30 +100,4 @@ public ApiResponse<Void> logout(HttpServletRequest request, HttpServletResponse
100100
memberCommandFacade.logout(request, response);
101101
return ApiResponse.onSuccess(null);
102102
}
103-
104-
// 소셜 로그인 관련
105-
@Operation(summary = "소셜 로그인 성공 후 리다이렉트 (임시 컨트롤러)", description = """
106-
소셜 로그인 성공 후, 리다이렉트 되는 중간 경로입니다.
107-
108-
- 실제 로그인 진입 경로는 `/oauth2/authorization/{provider}` (예: /oauth2/authorization/google) 이며,
109-
이 엔드포인트는 로그인 성공 후 JWT 토큰이 발급된 상태에서 호출됩니다.
110-
111-
- 최초 로그인(회원가입)의 경우: `isProfileCompleted: false` 가 응답되고, 프로필이 이미 완료된 유저의 경우: `nickname` 이 응답됩니다.
112-
113-
- 이 API는 프론트가 직접 호출하는 것이 아니라, 로그인 성공 후 자동으로 리다이렉트되는 경로입니다.
114-
""")
115-
@GetMapping(value = "/redirect/oauth2", produces = MediaType.APPLICATION_JSON_VALUE)
116-
public ApiResponse<Map<String, Object>> handleSocialLoginRedirect(@CurrentMember Member member) {
117-
118-
boolean isProfileCompleted = member.isProfileCompleted();
119-
if (isProfileCompleted) {
120-
return ApiResponse.onSuccess(
121-
Map.of("nickname", member.getNickName()));
122-
} else {
123-
return ApiResponse.onSuccess(
124-
Map.of("email", member.getEmail(), "isProfileCompleted",
125-
false)
126-
);
127-
}
128-
}
129103
}

src/main/resources/application-oauth2.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
app:
2+
oauth2:
3+
redirect:
4+
base-uri: ${FRONTEND_BASE_URI}
5+
16
spring:
27
security:
38
oauth2:

0 commit comments

Comments
 (0)