转载于 : 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("/**"); } }