标签:instanceof 拦截器 false token handler return HandlerMethod true method
handler instanceof HandlerMethod 一直为false 是因为导错了包
应该导入:
import org.springframework.web.method.HandlerMethod;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// 从http请求头中取出token
final String token = request.getHeader(JwtUtil.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;
}
}
// LocalAssert.isStringEmpty(token, "token为空,鉴权失败!");
if (StringUtils.isEmpty(token)){
throw new BaseException("token为空,鉴权失败!");
}
//验证,并获取token内部信息
Claims claims = JwtUtil.parseJWT(token);
String userToken = claims.getSubject();
//将token放入本地缓存
WebContextUtil.setUserToken(userToken);
return true;
}
标签:instanceof,拦截器,false,token,handler,return,HandlerMethod,true,method 来源: https://blog.csdn.net/lx_xing/article/details/117655467
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。