token 验证注解拦截

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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值