token的介绍
- Header:头部,通常头部有两部分内容:
声明类型,这里是JWT 签名算法,自定义 我们会对头部进行base64加密(可解密),得到第一部分数据
- Payload:载荷,就是有效数据,一般包含下面信息:
用户身份信息(注意,这里因为采用base64加密,可解密,因此不要存放敏感信息) tokenID:当前这个JWT的唯一标示 注册声明:如token的签发时间,过期时间,签发人等 这部分也会采用base64加密,得到第二部分数据
- Signature:签名,是整个数据的认证信息。一般根据前两步的数据,再加上服务的的密钥(secret)(不要泄漏,最好周期性更换),通过加密算法生成。用于验证整个数据完整和可靠性
例如:
"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.
eyJhdWQiOiIxIiwiZXhwIjoxNzE2NDYyMTE5fQ.
to4o36ikHcZydjrRrR1SMxfwkekHtZu_2TAAeok-AG000"}
pom依赖的引入
<!-- JWT-->
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.10.3</version>
</dependency>
生成token(TokenUtils)
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.tgs.springboot.entity.User;
import com.tgs.springboot.service.IUserService;
import com.tgs.springboot.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
/**
* 生成token
*
*/
public class TokenUtils {
public static IUserService staticUserService;
@Resource
private IUserService userService;
@PostConstruct
public void getUserService(){
staticUserService = userService;
}
public static String getToken(String userId, String sign) {
return JWT.create().withAudience(userId) //将userId保存到token中 , 作为载荷
.withExpiresAt(DateUtil.offsetHour(new Date(),1)) //1小时后token过期
.sign(Algorithm.HMAC256(sign)); //以password 作为 token的密钥
}
/*
获取当前登录的用户信息
*/
public static User getCurrentUser() {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String token = request.getHeader("token");
if (StrUtil.isNotBlank(token)){
String userId = JWT.decode(token).getAudience().get(0);
return staticUserService.getById(userId);
}
}catch (Exception e){
return null;
}
return null;
}
}
拦截器的代码实现
在可能会出现异常的建议trycatch一下 博主在这就不做演示了
import cn.hutool.core.util.StrUtil;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.tgs.springboot.common.Constants;
import com.tgs.springboot.entity.User;
import com.tgs.springboot.exception.ServiceException;
import com.tgs.springboot.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 拦截器
*
*/
@Component
public class JwtInterceptor implements HandlerInterceptor {
@Autowired
private IUserService userService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
String token = request.getHeader("token");
//如果不是映射方法直接通过
if (!(handler instanceof HandlerMethod)) {
return true;
}
//执行认证
if (StrUtil.isBlank(token)){
//此处可直接抛出异常
}
//获取token中的userId
String userId;
userId = JWT.decode(token).getAudience().get(0);
//根据token中的userId查询数据库,判断是否存在
User user = userService.getById(userId);
//用户密码加签验证token
JWTVerifier jwtVerifier =
JWT.require(Algorithm.HMAC256(user.getPassword())).build();
jwtVerifier.verify(token);
return true;
}
}
放行和拦截的配置代码实现
import com.tgs.springboot.config.interceptor.JwtInterceptor;
import org.springframework.context.annotation.Bean;
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 InterceptorConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(jwtInterceptor()).
addPathPatterns("/**") //拦截所有请求,通过判断token是否合法来判断是否需要重新登录
.excludePathPatterns("/user/login","user/register"); //可以放行的路径
}
@Bean
public JwtInterceptor jwtInterceptor(){
return new JwtInterceptor();
}
}