-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
109 additions
and
11 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
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/corea/auth/annotation/RefreshToken.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,14 @@ | ||
package corea.auth.annotation; | ||
|
||
import io.swagger.v3.oas.annotations.Hidden; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.PARAMETER) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Hidden() | ||
public @interface RefreshToken { | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package corea.auth.domain; | ||
|
||
public record TokenInfo(String accessToken, String refreshToken) { | ||
import org.springframework.http.ResponseCookie; | ||
|
||
public record TokenInfo(String accessToken, ResponseCookie refreshToken) { | ||
} |
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
42 changes: 42 additions & 0 deletions
42
backend/src/main/java/corea/auth/resolver/RefreshTokenArgumentResolver.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,42 @@ | ||
package corea.auth.resolver; | ||
|
||
import corea.auth.annotation.RefreshToken; | ||
import corea.auth.dto.TokenRefreshRequest; | ||
import corea.auth.service.CookieService; | ||
import corea.exception.CoreaException; | ||
import corea.exception.ExceptionType; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.core.MethodParameter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.bind.support.WebDataBinderFactory; | ||
import org.springframework.web.context.request.NativeWebRequest; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.method.support.ModelAndViewContainer; | ||
|
||
import static corea.global.config.Constants.REFRESH_COOKIE; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class RefreshTokenArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
private final CookieService cookieService; | ||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.hasParameterAnnotation(RefreshToken.class); | ||
} | ||
|
||
@Override | ||
public TokenRefreshRequest resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest(); | ||
Cookie[] cookies = request.getCookies(); | ||
if (cookies == null) { | ||
throw new CoreaException(ExceptionType.COOKIE_NOT_EXIST); | ||
} | ||
return new TokenRefreshRequest(cookieService.getCookieValue(cookies, REFRESH_COOKIE) | ||
.orElseThrow(() -> new CoreaException(ExceptionType.COOKIE_NOT_EXIST))); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
backend/src/main/java/corea/auth/service/CookieService.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,29 @@ | ||
package corea.auth.service; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Arrays; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class CookieService { | ||
|
||
public ResponseCookie createCookie(String name, String value, long maxAge) { | ||
return ResponseCookie.from(name, value) | ||
.httpOnly(true) | ||
.secure(true) | ||
.sameSite("Lax") | ||
.path("/") | ||
.maxAge(maxAge) | ||
.build(); | ||
} | ||
|
||
public Optional<String> getCookieValue(Cookie[] cookies, String name) { | ||
return Arrays.stream(cookies) | ||
.filter(cookie -> cookie.getName().equals(name)) | ||
.map(Cookie::getValue) | ||
.findFirst(); | ||
} | ||
} |
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