• JWT


    转载于 : https://blog.csdn.net/qq_42577548/article/details/105924345   https://www.cnblogs.com/niceyoo/p/10964277.html 

    1.

     https://blog.csdn.net/weixin_36342477/article/details/112193512

    这篇文章拦截器有点问题

    拦截器代码

    AuthenticationInterceptor类
    package com.data.config;
    
    import com.data.config.annotation.JwtIgnore;
    import com.data.utils.toke.JwtTokenUtil;
    import com.data.utils.toke.WebContextUtil;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.http.HttpMethod;
    import org.springframework.util.Assert;
    import org.springframework.web.method.HandlerMethod;
    import org.springframework.web.servlet.HandlerInterceptor;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.lang.reflect.Method;
    
    @Slf4j
    public class AuthenticationInterceptor extends HandlerInterceptorAdapter /*implements HandlerInterceptor*/ {
     
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
            // 从http请求头中取出token
            final String token = request.getHeader(JwtTokenUtil.AUTH_HEADER_KEY);
            //如果不是映射到方法,直接通过
            if(!(handler instanceof HandlerMethod)){
                return true;
            }
            //如果是方法探测,直接通过
            if (HttpMethod.OPTIONS.equals(request.getMethod())) {
                response.setStatus(HttpServletResponse.SC_OK);
                return true;
            }
            //如果方法有JwtIgnore注解,直接通过
            HandlerMethod handlerMethod = (HandlerMethod) handler;
            Method method=handlerMethod.getMethod();
            if (method.isAnnotationPresent(JwtIgnore.class)) {
                JwtIgnore jwtIgnore = method.getAnnotation(JwtIgnore.class);
                if(jwtIgnore.value()){
                    return true;
                }
            }
            Assert.notNull(token, "token为空,鉴权失败!");
            //验证,并获取token内部信息
            String userToken = JwtTokenUtil.verifyToken(token);
            
            //将token放入本地缓存
            WebContextUtil.setUserToken(userToken);
            return true;
        }
    
    
    
     
        @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
            //方法结束后,移除缓存的token
            WebContextUtil.removeUserToken();
        }
    }
    WebAppConfigurer类
    package com.data.config;
    
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    @Configuration
    public class WebAppConfigurer implements WebMvcConfigurer {
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            // 可添加多个
            registry.addInterceptor(new AuthenticationInterceptor()).addPathPatterns("/**");
        }
    
    }
  • 相关阅读:
    堆排序优化与几个排序算法时间复杂度
    冒泡排序的实现和优化及其与插入,选择排序的比较
    插入排序的实现与优化并和选择排序进行性能比较
    选择排序的实现以及性能测试
    MD5加密
    低功耗蓝牙BLE [学习笔记]
    反编译[学习笔记]
    Android 混淆[学习笔记]
    北大SQL数据库视频课程笔记
    repo/git Android/CyanogenMod srouce code
  • 原文地址:https://www.cnblogs.com/Hello-TomCat/p/14922702.html
Copyright © 2020-2023  润新知