-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 스프링 인터셉터을 이용하여 토큰 유효성 검증 로직 구현 (#17)
- Loading branch information
1 parent
fc3fe18
commit 65845f7
Showing
5 changed files
with
101 additions
and
8 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/com/darass/darass/auth/oauth/AuthControllerAdvice.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 com.darass.darass.auth.oauth; | ||
|
||
import com.darass.darass.auth.oauth.exception.AuthenticationException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
@RestControllerAdvice | ||
public class AuthControllerAdvice { | ||
|
||
@ExceptionHandler(AuthenticationException.class) | ||
public String authenticationExceptionHandler(AuthenticationException exception) { | ||
return exception.getMessage(); | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
backend/src/main/java/com/darass/darass/auth/oauth/AuthenticationPrincipalConfig.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,31 @@ | ||
package com.darass.darass.auth.oauth; | ||
|
||
import com.darass.darass.auth.oauth.infrastructure.JwtTokenProvider; | ||
import com.darass.darass.auth.oauth.infrastructure.LoginInterceptor; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
public class AuthenticationPrincipalConfig implements WebMvcConfigurer { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public AuthenticationPrincipalConfig(JwtTokenProvider jwtTokenProvider) { | ||
this.jwtTokenProvider = jwtTokenProvider; | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(loginInterceptor()) | ||
.addPathPatterns("/api/v1/**") | ||
.excludePathPatterns("/api/v1/login/oauth"); | ||
} | ||
|
||
@Bean | ||
public HandlerInterceptor loginInterceptor() { | ||
return new LoginInterceptor(jwtTokenProvider); | ||
} | ||
} |
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
29 changes: 29 additions & 0 deletions
29
...end/src/main/java/com/darass/darass/auth/oauth/infrastructure/AuthorizationExtractor.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 com.darass.darass.auth.oauth.infrastructure; | ||
|
||
import java.util.Enumeration; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
public class AuthorizationExtractor { | ||
|
||
public static final String AUTHORIZATION = "Authorization"; | ||
public static final String ACCESS_TOKEN_TYPE = AuthorizationExtractor.class.getSimpleName() + ".ACCESS_TOKEN_TYPE"; | ||
public static String BEARER_TYPE = "Bearer"; | ||
|
||
public static String extract(HttpServletRequest request) { | ||
Enumeration<String> headers = request.getHeaders(AUTHORIZATION); | ||
while (headers.hasMoreElements()) { | ||
String value = headers.nextElement(); | ||
if ((value.toLowerCase().startsWith(BEARER_TYPE.toLowerCase()))) { | ||
String authHeaderValue = value.substring(BEARER_TYPE.length()).trim(); | ||
request.setAttribute(ACCESS_TOKEN_TYPE, value.substring(0, BEARER_TYPE.length()).trim()); | ||
int commaIndex = authHeaderValue.indexOf(','); | ||
if (commaIndex > 0) { | ||
authHeaderValue = authHeaderValue.substring(0, commaIndex); | ||
} | ||
return authHeaderValue; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/src/main/java/com/darass/darass/auth/oauth/infrastructure/LoginInterceptor.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,24 @@ | ||
package com.darass.darass.auth.oauth.infrastructure; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
public class LoginInterceptor implements HandlerInterceptor { | ||
|
||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public LoginInterceptor(JwtTokenProvider jwtTokenProvider) { | ||
this.jwtTokenProvider = jwtTokenProvider; | ||
} | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
if ("OPTIONS".equals(request.getMethod())) { | ||
return true; | ||
} | ||
final String accessToken = AuthorizationExtractor.extract(request); | ||
jwtTokenProvider.validateToken(accessToken); | ||
return true; | ||
} | ||
} |