-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] #137 - oauth 에서 어느 컨테이너에서 콜백이 되더라도 인증과정을 처리할 수 있도록 redis에 oaut…
…h request 저장
- Loading branch information
odls
authored and
odls
committed
Nov 27, 2024
1 parent
ae7350f
commit 6eb7ee3
Showing
4 changed files
with
90 additions
and
7 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
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
64 changes: 64 additions & 0 deletions
64
src/main/java/com/wooribound/global/util/RedisOAuth2AuthorizationRequestRepository.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,64 @@ | ||
package com.wooribound.global.util; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.security.oauth2.client.web.AuthorizationRequestRepository; | ||
import org.springframework.security.oauth2.core.endpoint.OAuth2AuthorizationRequest; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RedisOAuth2AuthorizationRequestRepository implements AuthorizationRequestRepository<OAuth2AuthorizationRequest> { | ||
|
||
private final RedisTemplate<String, OAuth2AuthorizationRequest> oauth2AuthorizationRequestRedisTemplate; | ||
private static final String OAUTH2_AUTHORIZATION_REQUEST_PREFIX = "oauth2_auth_request:"; | ||
private static final int TIMEOUT = 10 * 60; | ||
|
||
@Override | ||
public void saveAuthorizationRequest( | ||
OAuth2AuthorizationRequest authorizationRequest, | ||
HttpServletRequest request, | ||
HttpServletResponse response) { | ||
if (authorizationRequest == null) { | ||
removeAuthorizationRequest(request, response); | ||
return; | ||
} | ||
|
||
String state = authorizationRequest.getState(); | ||
oauth2AuthorizationRequestRedisTemplate.opsForValue().set( | ||
OAUTH2_AUTHORIZATION_REQUEST_PREFIX + state, | ||
authorizationRequest, | ||
TIMEOUT, | ||
TimeUnit.SECONDS | ||
); | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest loadAuthorizationRequest(HttpServletRequest request) { | ||
String state = request.getParameter("state"); | ||
if (state == null) return null; | ||
|
||
return oauth2AuthorizationRequestRedisTemplate.opsForValue() | ||
.get(OAUTH2_AUTHORIZATION_REQUEST_PREFIX + state); | ||
} | ||
|
||
@Override | ||
public OAuth2AuthorizationRequest removeAuthorizationRequest( | ||
HttpServletRequest request, | ||
HttpServletResponse response) { | ||
String state = request.getParameter("state"); | ||
if (state == null) return null; | ||
|
||
String key = OAUTH2_AUTHORIZATION_REQUEST_PREFIX + state; | ||
OAuth2AuthorizationRequest auth = oauth2AuthorizationRequestRedisTemplate.opsForValue().get(key); | ||
if (auth != null) { | ||
oauth2AuthorizationRequestRedisTemplate.delete(key); | ||
} | ||
return auth; | ||
} | ||
} | ||
|