Skip to content

Commit

Permalink
fix: 자동 로그인을 위해 access token 만료 확인 로직 제거
Browse files Browse the repository at this point in the history
related to: #11
  • Loading branch information
heejjinkim committed Oct 18, 2024
1 parent 1374da4 commit d688dc7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
5 changes: 3 additions & 2 deletions src/main/java/com/_119/wepro/global/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com._119.wepro.global.config;

import static com._119.wepro.global.security.constant.SecurityConstants.REFRESH_TOKEN_HEADER;

import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
Expand All @@ -8,7 +10,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.security.oauth2.core.AuthorizationGrantType;

@Configuration
public class SwaggerConfig {
Expand Down Expand Up @@ -48,7 +49,7 @@ private SecurityScheme createRefreshTokenSecurityScheme() {
return new SecurityScheme()
.type(SecurityScheme.Type.APIKEY)
.in(SecurityScheme.In.HEADER)
.name(AuthorizationGrantType.REFRESH_TOKEN.getValue());
.name(REFRESH_TOKEN_HEADER);
}

private Info createApiInfo() {
Expand Down
25 changes: 8 additions & 17 deletions src/main/java/com/_119/wepro/member/service/ReissueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Slf4j
@Service
@RequiredArgsConstructor
public class ReissueService {
Expand All @@ -31,15 +34,14 @@ public void reissue(HttpServletRequest request, HttpServletResponse response) {
String refreshToken = request.getHeader(REFRESH_TOKEN_HEADER);
String accessToken = extractToken(request.getHeader(ACCESS_TOKEN_HEADER));

validateAccessTokenExpired(accessToken);
String providerId = jwtTokenProvider.parseExpiredToken(accessToken).getSubject();
String memberId = jwtTokenProvider.parseExpiredToken(accessToken).getSubject();

validateRefreshToken(refreshToken, providerId);
validateRefreshToken(refreshToken, memberId);

Member member = memberRepository.findByProviderId(providerId)
Member member = memberRepository.findById(Long.parseLong(memberId))
.orElseThrow(() -> new RestApiException(UserErrorCode.USER_NOT_FOUND));

TokenInfo newTokenInfo = jwtTokenProvider.generateToken(providerId, member.getRole());
TokenInfo newTokenInfo = jwtTokenProvider.generateToken(memberId, member.getRole());
setTokenPairToResponseHeader(response, newTokenInfo.getAccessToken(),
newTokenInfo.getRefreshToken());
}
Expand All @@ -52,17 +54,6 @@ private String extractToken(String token) {
return token.replace(GRANT_TYPE, "");
}

private void validateAccessTokenExpired(String accessToken) {
try {
jwtTokenProvider.validateToken(accessToken);
throw new RestApiException(REFRESH_DENIED);
} catch (RestApiException e) {
if (e.getErrorCode() != EXPIRED_TOKEN) {
throw e;
}
}
}

private void validateRefreshToken(String refreshToken, String memberId) {
String savedRefreshToken = jwtTokenProvider.getRefreshToken(memberId);
if (!refreshToken.equals(savedRefreshToken)) {
Expand All @@ -73,6 +64,6 @@ private void validateRefreshToken(String refreshToken, String memberId) {
private void setTokenPairToResponseHeader(
HttpServletResponse response, String accessToken, String refreshToken) {
response.setHeader(ACCESS_TOKEN_HEADER, GRANT_TYPE + accessToken);
response.setHeader(REFRESH_TOKEN_HEADER, GRANT_TYPE + refreshToken);
response.setHeader(REFRESH_TOKEN_HEADER, refreshToken);
}
}

0 comments on commit d688dc7

Please sign in to comment.