FeignAuthenticationInterceptor

/**
 * @ClassName: AuthenticationInterceptor
 * @Description:
 * @Author: QIUJIAQING
 * @Date: 2019-03-29 16:53
 * @Version:1.0
 **/
@Slf4j
public class FeignAuthenticationInterceptor implements HandlerInterceptor {

    @Autowired
    private MircoserviceInfoService mircoserviceInfoService;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {
        String token = httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION);// 从 http 请求头中取出 token
        // 如果不是映射到方法直接通过
        if (!(object instanceof HandlerMethod)) {
            return true;
        }
        HandlerMethod handlerMethod = (HandlerMethod) object;
        Method method = handlerMethod.getMethod();

        if (method.isAnnotationPresent(PassToken.class)) {
            PassToken passToken = method.getAnnotation(PassToken.class);
            if (passToken.required()) {
                return true;
            }
        }

        //检查有没有需要微服务权限的注解
        if (method.isAnnotationPresent(MSToken.class)) {
            MSToken msToken = method.getAnnotation(MSToken.class);
            if (msToken.required()) {
                // 执行认证
                if (token == null) {
                    throw new SystemException(SystemException.DESC000021, SystemException.DESC000021);
                }

                String mircoserviceName;
                try {
                    mircoserviceName = JWT.decode(token).getAudience().get(0);
                } catch (JWTDecodeException j) {
                    throw new SystemException(SystemException.CODE000022, SystemException.DESC000022);
                }
                MircoserviceInfo mircoserviceInfo = new MircoserviceInfo();
                mircoserviceInfo.setMircoserviceName(mircoserviceName);
                MircoserviceInfo resultInfo = mircoserviceInfoService.getMircoserviceInfo(mircoserviceInfo);
                if (resultInfo == null) {
                    throw new SystemException(SystemException.CODE030002, SystemException.DESC030002);
                }
                // 验证 token
                JWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(resultInfo.getMircoserviceName() + resultInfo.getPassword())).build();
                try {
                    jwtVerifier.verify(token);
                } catch (JWTVerificationException e) {
                    throw new SystemException(SystemException.CODE000022, SystemException.DESC000022);
                }
                return true;
            }
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值