springcloud 项目中,网关层客户端调用api时经常需要验证token的有效性且需要返回值,按照需求需要加上一个拦截器,选用spring aop.
直接上代码
定义注解类
package com.jp.user.aop;
import java.lang.annotation.*;
/**
* @Author: hj
* @Description:
* @Date: Created in 10:35 2019/5/31
* @Modified By:
*/
/**
* 验证token
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface TokenVerification {
}
定义全局返回值类型
package com.jp.user.aop;
import com.jp.user.entity.UserCacheVO;
/**
* @Author: hj
* @Description:
* @Date: Created in 10:44 2019/5/31
* @Modified By:
*/
public class TokenValidHelper {
private final static ThreadLocal<UserCacheVO> threadLocal = new ThreadLocal<>();
public static void set(UserCacheVO UserCacheVO){
threadLocal.set(UserCacheVO);
}
public static UserCacheVO get(){
return threadLocal.get();
}
public static void remove(){
threadLocal.remove();
}
}
验证token 并返回userCacheVo 用户信息
package com.jp.user.aop;
import com.jp.user.entity.UserCacheVO;
/**
* @Author: hj
* @Description:
* @Date: Created in 10:44 2019/5/31
* @Modified By:
*/
public class TokenValidHelper {
private final static ThreadLocal<UserCacheVO> threadLocal = new ThreadLocal<>();
public static void set(UserCacheVO UserCacheVO){
threadLocal.set(UserCacheVO);
}
public static UserCacheVO get(){
return threadLocal.get();
}
public static void remove(){
threadLocal.remove();
}
}
package com.jp.user.aop;
import com.jiu.common.core.dto.ResponseDTO;
import com.jiu.common.core.enums.ErrorEnum;
import com.jp.user.entity.UserCacheVO;
import com.jp.user.utils.JsonHelper;
import com.jp.user.utils.RedisSessionManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
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: hj
* @Description:
* @Date: Created in 14:12 2019/5/31
* @Modified By:
*/
@Slf4j
@Aspect
@Component
public class TokenValidAspect {
private RedisSessionManager redisSessionManager ;
@Around("@annotation(tokenVerification)")
public Object verifyToken(ProceedingJoinPoint joinPoint,TokenVerification tokenVerification) throws Throwable {
if(TokenValidHelper.get()!=null){
log.info("TokenValidAspect={}", JsonHelper.obj2JsonStr(TokenValidHelper.get()));
}
log.info("请求到了aop");
//获取请求
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
//获取请求参数
Object[] object = joinPoint.getArgs();
log.info(object[0].toString());
//获取请求头上携带的token
String token = request.getHeader("uu");
if(StringUtils.isEmpty(token)){
return ResponseDTO.fail(ErrorEnum.ERROR_000103);
}
//验证token
//从redis 中拿取token
UserCacheVO userCacheVO = redisSessionManager.getUserAuthCacheVOByToken(token);
/* if(userCacheVO==null){
return ResponseDTO.fail(ErrorEnum.ERROR_000101);
}*/
//更新token过期时间
// redisSessionManager.updateTokenExpireTime(token,null,null,userCacheVO.getTokenExpireTime());
/* if(!responseDTO.isSuccess()){
throw new CoinException(responseDTO.getRetCode(),responseDTO.getRetMsg());
}
*/
//放入用户信息
TokenValidHelper.set(userCacheVO);
//执行目标方法
Object obj = null;
try {
obj = joinPoint.proceed();
} finally {
//清除用户信息
TokenValidHelper.remove();
}
return obj;
}
}