升级了@JwtVerify的能力,现在被修饰的方法的任意一个参数是继承于JwtRequest的对象即可,不再强制为第一个参

This commit is contained in:
高子兴 2024-07-03 13:58:09 +08:00
parent 6c36552c2f
commit a5aac1199d

View File

@ -1,5 +1,6 @@
package org.cmh.backend.Utils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@ -7,13 +8,17 @@ import org.springframework.stereotype.Component;
@Aspect
@Component
public class JwtVerifyAspect {
@Before("@annotation(JwtVerify) && args(request,..)")
public void verifyJwtToken(Object request) throws JwtValidationException {
if (request instanceof JwtRequest) {
String token = ((JwtRequest) request).getToken();
if (!JwtUtil.isTokenValid(token)) {
throw new JwtValidationException("JWT token is invalid");
@Before("@annotation(JwtVerify)")
public void verifyJwtToken(JoinPoint joinPoint) throws JwtValidationException {
Object[] args = joinPoint.getArgs();
for (Object arg : args) {
if (arg instanceof JwtRequest jwtRequest) {
String token = jwtRequest.getToken();
if (!JwtUtil.isTokenValid(token)) {
throw new JwtValidationException("JWT token is invalid");
}
}
break;
}
}
}