Skip to content

Commit

Permalink
[+] #76 Jwt 인증을 수행하는 AspectJ 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
woody35545 committed Oct 8, 2023
1 parent 5bd846b commit 731fa9e
Showing 1 changed file with 51 additions and 0 deletions.
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);
}
}

0 comments on commit 731fa9e

Please sign in to comment.