springboot简单集成jwt
参考:https://blog.csdn.net/gjtao1130/article/details/111658060
大佬的源码是可以运行的,我写这个文章的目的是添加一些注释来辅助理解
源码
JwtInterceptor.Java
package com.xxh.jwt1.interceptor;import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.exceptions.JWTVerificationException;import com.xxh.jwt1.annotation.LoginToken;
import com.xxh.jwt1.annotation.PassToken;
import com.xxh.jwt1.entity.User;
import com.xxh.jwt1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;/*** @title: JwtInterceptor* @Author gjt* @Date: 2020-12-21* @Description:*/
public class JwtInterceptor implements HandlerInterceptor {@Autowiredprivate UserService userService;// 拦截器,接收请求后,在执行请求前进行检测@Overridepublic boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object object) throws Exception {System.out.println("object:"+object);System.out.println("object instanceof HandlerMethod:"+(object instanceof HandlerMethod));// 如果是请求方法(比如登录方法),那么object instanceof HandlerMethod的结果为true// 如果是请求资源(global.css),那么object instanceof HandlerMethod的结果为false,但是需要拦截器放行// 如果不是映射到方法直接通过if (!(object instanceof HandlerMethod)) {return true;}// 这一步认为object是请求方法,强转HandlerMethod handlerMethod = (HandlerMethod) object;System.out.println("handlerMethod:"+handlerMethod);// 获取method对象Method method = handlerMethod.getMethod();System.out.println("method:" + method);//检查是否有 PassToken 注释,有则跳过认证,没有就继续验证if (method.isAnnotationPresent(PassToken.class)) {// 判断方法上是否有PassToken注解,有的话就获取,没有就为nullPassToken passToken = method.getAnnotation(PassToken.class);System.out.println("method.getAnnotation:" + method.getAnnotation(PassToken.class));System.out.println("passToken.required():" + passToken.required());// 获取PassToken的required()的值,这是我们自定义的。默认为true,所以使preHandle返回true,通过验证if (passToken.required()) {return true;}}// 从 http 请求头中取出 tokenString token = httpServletRequest.getHeader("token");//检查有没有需要用户权限的注解if (method.isAnnotationPresent(LoginToken.class)) {// 判断方法上是否有 LoginToken 注解,有的话就获取,没有就为nullLoginToken loginToken = method.getAnnotation(LoginToken.class);// 获取LoginToken的required()的值,这是我们自定义的。默认为trueif (loginToken.required()) {// 执行认证// token 已经在上面获取,如果没有就直接返回异常if (token == null) {throw new RuntimeException("无token,请重新登录");}// 获取 token 中的 user idString userId;try {userId = JWT.decode(token).getAudience().get(0);System.out.println("JWT.decode(token).getAudience():"+JWT.decode(token).getAudience());System.out.println("userId:"+userId);} catch (JWTDecodeException j) {throw new RuntimeException("401");}User user = userService.getUser(userId);if (user == null) {throw new RuntimeException("用户不存在,请重新登录");}// 验证 tokenJWTVerifier jwtVerifier = JWT.require(Algorithm.HMAC256(user.getPassWord())).build();System.out.println("jwtVerifier:"+jwtVerifier);System.out.println("jwtVerifier.verify(token):"+jwtVerifier.verify(token));try {jwtVerifier.verify(token);} catch (JWTVerificationException e) {throw new RuntimeException("401");}return true;}}// 如果没有 PassToken 或 LoginToken 的接口,如登录获取token方法,就会直接通过return true;}@Overridepublic void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {System.out.println("hello welcome");}@Overridepublic void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {}
}
我觉得集成jwt最难的就是理解JwtInterceptor的原理了。
JwtInterceptor实现HandlerInterceptor的preHandle方法,目的是为了springboot接收到请求前,先不执行controller的方法,由拦截器的逻辑来判断该条请求是否需要拦截。