package com.common.aspect;
|
|
|
import com.common.core.utils.JwtTokenUtil;
|
import com.common.security.utils.SecurityHolderUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.aspectj.lang.ProceedingJoinPoint;
|
import org.aspectj.lang.annotation.Around;
|
import org.aspectj.lang.annotation.Aspect;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.core.annotation.Order;
|
import org.springframework.stereotype.Component;
|
import org.springframework.web.context.request.RequestContextHolder;
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
import javax.servlet.http.HttpServletRequest;
|
|
/**
|
* @author 廖振钦
|
* @date 2022-01-17
|
*/
|
@Slf4j
|
@Aspect
|
@Component
|
@Order(0) //TOKEN记录切面优先级最高
|
public class TokenAspect {
|
|
@Autowired
|
private JwtTokenUtil jwtTokenUtil;
|
|
|
@Around("execution(* com..controller..*.*(..))" +
|
"&& (@annotation(org.springframework.web.bind.annotation.RequestMapping)" +
|
"|| @annotation(org.springframework.web.bind.annotation.GetMapping)" +
|
"|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
|
"|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" +
|
"|| @annotation(org.springframework.web.bind.annotation.PatchMapping))" +
|
"&& !@annotation(com.common.annotation.NoToken)")
|
public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
|
try {
|
this.checkToken();
|
return pjp.proceed();
|
} catch (Throwable e) {
|
log.error("LogAspect>>>>>>>>", e);
|
throw e;
|
}
|
}
|
|
private void checkToken(){
|
HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
|
String token = request.getHeader("pi-token");
|
Object useridobject = jwtTokenUtil.getClaim(token).get(JwtTokenUtil.CLAIM_KEY_USERID);
|
if(useridobject != null){
|
String userid = useridobject.toString();
|
SecurityHolderUtils.setUserId(userid);
|
}
|
}
|
}
|