-
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.
Browse files
Browse the repository at this point in the history
Feat/#20 토큰 검증을 위한 ArgumentResolver 생성
- Loading branch information
Showing
9 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
src/main/java/coffeemeet/server/auth/RefreshTokenRepository.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,8 @@ | ||
package coffeemeet.server.auth; | ||
|
||
import coffeemeet.server.auth.domain.RefreshToken; | ||
import org.springframework.data.repository.CrudRepository; | ||
|
||
public interface RefreshTokenRepository extends CrudRepository<RefreshToken, Long> { | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/coffeemeet/server/auth/domain/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,23 @@ | ||
package coffeemeet.server.auth.domain; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.redis.core.RedisHash; | ||
|
||
@Getter | ||
@RedisHash(value = "refresh", timeToLive = 1209600) | ||
public class RefreshToken { | ||
|
||
@Id | ||
private Long userId; | ||
|
||
private String value; | ||
|
||
@Builder | ||
private RefreshToken(Long userId, String value) { | ||
this.userId = userId; | ||
this.value = value; | ||
} | ||
|
||
} |
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
57 changes: 57 additions & 0 deletions
57
src/main/java/coffeemeet/server/common/UserArgumentResolver.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,57 @@ | ||
package coffeemeet.server.common; | ||
|
||
import coffeemeet.server.auth.RefreshTokenRepository; | ||
import coffeemeet.server.auth.domain.RefreshToken; | ||
import coffeemeet.server.auth.utils.JwtTokenProvider; | ||
import coffeemeet.server.common.annotation.Login; | ||
import coffeemeet.server.user.dto.AuthInfo; | ||
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; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class UserArgumentResolver implements HandlerMethodArgumentResolver { | ||
|
||
public static final String USER_AUTHENTICATION_FAILED_MESSAGE = "사용자(%s)의 갱신 토큰이 존재하지 않습니다."; | ||
private static final String HEADER_AUTHENTICATION_FAILED_MESSAGE = "(%s)는 잘못된 권한 헤더입니다."; | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
|
||
@Override | ||
public boolean supportsParameter(MethodParameter parameter) { | ||
return parameter.getParameterType().equals(AuthInfo.class) && parameter.hasParameterAnnotation( | ||
Login.class); | ||
} | ||
|
||
@Override | ||
public AuthInfo resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, | ||
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) { | ||
HttpServletRequest httpServletRequest = (HttpServletRequest) webRequest.getNativeRequest(); | ||
String authHeader = httpServletRequest.getHeader("Authorization"); | ||
|
||
if (authHeader != null && authHeader.startsWith("Bearer ")) { | ||
String token = authHeader.substring(7); | ||
Long userId = jwtTokenProvider.extractUserId(token); | ||
RefreshToken refreshToken = getRefreshToken(userId); | ||
return new AuthInfo(userId, refreshToken.getValue()); | ||
} | ||
throw new IllegalArgumentException( | ||
String.format(HEADER_AUTHENTICATION_FAILED_MESSAGE, authHeader) | ||
); | ||
} | ||
|
||
private RefreshToken getRefreshToken(Long userId) { | ||
return refreshTokenRepository.findById(userId) | ||
.orElseThrow(() -> new IllegalArgumentException(String.format( | ||
USER_AUTHENTICATION_FAILED_MESSAGE, userId))); | ||
} | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/coffeemeet/server/common/annotation/Login.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,12 @@ | ||
package coffeemeet.server.common.annotation; | ||
|
||
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) | ||
public @interface Login { | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/coffeemeet/server/common/config/AuthWebConfig.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 |
---|---|---|
@@ -1,21 +1,31 @@ | ||
package coffeemeet.server.common.config; | ||
|
||
import coffeemeet.server.auth.RefreshTokenRepository; | ||
import coffeemeet.server.auth.utils.JwtTokenProvider; | ||
import coffeemeet.server.auth.utils.converter.OAuthProviderConverter; | ||
import coffeemeet.server.common.UserArgumentResolver; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.format.FormatterRegistry; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class AuthWebConfig implements WebMvcConfigurer { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
|
||
@Override | ||
public void addFormatters(FormatterRegistry registry) { | ||
registry.addConverter(new OAuthProviderConverter()); | ||
} | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(new UserArgumentResolver(jwtTokenProvider, refreshTokenRepository)); | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package coffeemeet.server.user.dto; | ||
|
||
public record AuthInfo(Long userId, String 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