-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
5bd846b
commit 731fa9e
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/com/teamseven/MusicVillain/Aspect/JwtAuthorizationAspect.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,51 @@ | ||
package com.teamseven.MusicVillain.Aspect; | ||
|
||
import com.teamseven.MusicVillain.Dto.ServiceResult; | ||
import com.teamseven.MusicVillain.Exception.JwtAuthorizationFailException; | ||
import com.teamseven.MusicVillain.Security.JWT.JwtManager; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.aspectj.lang.reflect.MethodSignature; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.context.request.RequestContextHolder; | ||
import org.springframework.web.context.request.ServletRequestAttributes; | ||
|
||
@Slf4j | ||
@Component | ||
@Aspect | ||
public class JwtAuthorizationAspect { | ||
|
||
@Before("@annotation(JwtAuthorizationRequired)") | ||
public void verifyAccessToken(JoinPoint joinPoint) throws Throwable { | ||
|
||
// delegate token validation | ||
ServiceResult tokenValidationResult = delegateTokenValidation(); | ||
|
||
// if token validation failed, throw JwtAuthorizationFailException | ||
if (tokenValidationResult.isFailed()) { | ||
log.warn("Authorization Fail - {} ", tokenValidationResult.getMessage()); | ||
throw new JwtAuthorizationFailException(tokenValidationResult.getMessage()); | ||
} | ||
} | ||
|
||
|
||
public ServiceResult delegateTokenValidation() throws JwtAuthorizationFailException { | ||
// get Http Request | ||
HttpServletRequest request = | ||
((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest(); | ||
|
||
// get Authorization Header | ||
String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION); | ||
log.trace("Authorization Header: {}", authorizationHeader); | ||
|
||
// delegate Token Validation to JwtManager | ||
ServiceResult verifyAccessTokenResult = JwtManager.verifyAccessToken(authorizationHeader); | ||
log.trace("verifyAccessTokenResult: {}", verifyAccessTokenResult); | ||
|
||
return JwtManager.verifyAccessToken(authorizationHeader); | ||
} | ||
} |