-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: OAuth 콜백 파라미터로 리다이렉트 위치를 지정하도록 수정 (#685)
* feat: 인가 요청 시 추가 파라미터 설정하는 커스텀 리졸버 구현 * feat: 시큐리티 설정에 커스텀 리졸버 등록 * feat: 리다이렉트 시 파라미터로부터 타깃 위치 로드하도록 설정
- Loading branch information
Showing
4 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...in/java/com/gdschongik/gdsc/global/security/CustomOAuth2AuthorizationRequestResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.gdschongik.gdsc.global.security; | ||
|
||
import static com.gdschongik.gdsc.global.common.constant.SecurityConstant.*; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository; | ||
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver; | ||
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestResolver; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
|
||
public class CustomOAuth2AuthorizationRequestResolver implements OAuth2AuthorizationRequestResolver { | ||
|
||
private final DefaultOAuth2AuthorizationRequestResolver delegate; | ||
|
||
public CustomOAuth2AuthorizationRequestResolver(ClientRegistrationRepository clientRegistrationRepository) { | ||
this.delegate = | ||
new DefaultOAuth2AuthorizationRequestResolver(clientRegistrationRepository, "/oauth2/authorization"); | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest resolve(HttpServletRequest request) { | ||
OAuth2AuthorizationRequest authorizationRequest = delegate.resolve(request); | ||
return authorizationRequest != null ? customizeAuthorizationRequest(request, authorizationRequest) : null; | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest resolve(HttpServletRequest request, String clientRegistrationId) { | ||
OAuth2AuthorizationRequest authorizationRequest = delegate.resolve(request, clientRegistrationId); | ||
return authorizationRequest != null ? customizeAuthorizationRequest(request, authorizationRequest) : null; | ||
} | ||
|
||
private OAuth2AuthorizationRequest customizeAuthorizationRequest( | ||
HttpServletRequest request, OAuth2AuthorizationRequest authorizationRequest) { | ||
|
||
String referer = request.getHeader("Referer"); | ||
if (referer == null || referer.isEmpty()) { | ||
return authorizationRequest; | ||
} | ||
|
||
Map<String, Object> additionalParameters = new HashMap<>(); | ||
additionalParameters.put(OAUTH_TARGET_URL_PARAM_NAME, referer); | ||
|
||
return OAuth2AuthorizationRequest.from(authorizationRequest) | ||
.additionalParameters(additionalParameters) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters