import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
public class JwtTokenUtils {
public static final String TOKEN_HEADER = "TOKEN";
/**
* 密钥key
*/
private static final String SECRET = "ug666ug666ug666";
/**
* JWT的发行人
*/
private static final String ISS = "ug666";
/**
* 自定义用户信息
*/
private static final String ROLE_CLAIMS = "rol";
/**
* 过期时间是3600秒,既是1个小时
*/
public static final long EXPIRATION = 3600L * 1000;
/**
* 选择了记住我之后的过期时间为7天
*/
public static final long EXPIRATION_REMEMBER = 604800L * 1000;
public static String createToken(User user) {
return Jwts.builder().signWith(SignatureAlgorithm.HS512, SECRET) // 加密算法
.setIssuer(ISS) // jwt发行人
.claim("userId", user.getId())
.setSubject(user.getUsername()) // jwt面向的用户
.setIssuedAt(new Date()) // jwt发行人
.setExpiration(new Date(System.currentTimeMillis() + EXPIRATION)) // token过期时间
.compact();
}
/**
* 从token获取用户名
* @param token
* @return
*/
public static String getUsername(String token) {
return getTokenBody(token).getSubject();
}
public static int getUserId(String token) {
return (int)getTokenBody(token).get("userId");
}
/**
* 是否已过期
*
* @param token
* @return
*/
public static boolean isExpiration(String token) {
return getTokenBody(token).getExpiration().before(new Date());
}
private static Claims getTokenBody(String token) {
return Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
}
/**
* 验证token
*
* @param token
* @return
*/
public static boolean validateToken(String token) {
return isExpiration(token) == false;
}
public static void main(String[] args) {
User user = new User();
user.setId(2);
user.setUsername("asd");
String token = JwtTokenUtils.createToken(user);
System.out.println(token);
System.out.println(JwtTokenUtils.getUsername("eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ1ZzY2NiIsInVzZXJJZCI6Miwic3ViIjoiYXNkIiwiaWF0IjoxNjcyOTE2NzM0LCJleHAiOjE2NzI5MjAzMzR9.gTPE0PgoXMOP7y53diapMqItIBMKyhXStMOP3RJ1fseOagFKMFzVcAroSaUGMcHT4hGWYHcZDYcT7yDCRlKkOg"));
System.out.println(JwtTokenUtils.getUserId("eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ1ZzY2NiIsInVzZXJJZCI6Miwic3ViIjoiYXNkIiwiaWF0IjoxNjcyOTE2NzM0LCJleHAiOjE2NzI5MjAzMzR9.gTPE0PgoXMOP7y53diapMqItIBMKyhXStMOP3RJ1fseOagFKMFzVcAroSaUGMcHT4hGWYHcZDYcT7yDCRlKkOg"));
System.out.println(JwtTokenUtils.validateToken("eyJhbGciOiJIUzUxMiJ9.eyJpc3MiOiJ1ZzY2NiIsInVzZXJJZCI6Miwic3ViIjoiYXNkIiwiaWF0IjoxNjcyOTE2NzM0LCJleHAiOjE2NzI5MjAzMzR9.gTPE0PgoXMOP7y53diapMqItIBMKyhXStMOP3RJ1fseOagFKMFzVcAroSaUGMcHT4hGWYHcZDYcT7yDCRlKkOg"));
}
}
需要导入maven依赖
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.0</version>
</dependency>
另外一个版本的TokenUtils工具类
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
//JWT Token工具类
@Component
public class TokenUtils {
//用户名key
private static final String CLAIM_KEY_USERNAME = "username";
//生成时间key
private static final String CLAIM_KEY_CREATED = "created";
//密钥
private String secret = "1234";
//失效时间
private Long expiration = 3600L * 1000;
//根据用户信息生成token
public String generateToken(User user){
Map<String,Object> claims = new HashMap<>();
claims.put(CLAIM_KEY_USERNAME,user.getUsername());
claims.put(CLAIM_KEY_CREATED,new Date());
return generateToken(claims);
}
//根据荷载生成JWT Token
private String generateToken(Map<String,Object> claims){
return Jwts.builder()
.setClaims(claims)
//过期时间
.setExpiration(generateExpirationDate())
//secret:盐值
.signWith(SignatureAlgorithm.HS512,secret)
.compact();
}
//生成token失效时间
private Date generateExpirationDate() {
return new Date(System.currentTimeMillis() + expiration*1000);
}
//从token中获取用户名
public String getUsernameFromToken(String token){
String username;
try{
Claims claims = getClaimsFormToken(token);
username = claims.getSubject();
}catch (Exception e){
username = null;
}
return username;
}
//从jwt中获荷载
private Claims getClaimsFormToken(String token) {
Claims claims = null;
try {
claims = Jwts.parser()
.setSigningKey(secret)
.parseClaimsJws(token)
.getBody();
}catch (Exception e){
e.printStackTrace();
}
return claims;
}
//判断token是否有效
public boolean validateToken(String token,User user){
String username = getUsernameFromToken(token);
return username.equals(user.getUsername()) && ! isTokenExpired(token);
}
//判断token是否失效
private boolean isTokenExpired(String token) {
Date expireDate = getExpiredDtaeFromToken(token);
return expireDate.before(new Date());
}
//从token中获取过期时间
private Date getExpiredDtaeFromToken(String token) {
Claims claims = getClaimsFormToken(token);
return claims.getExpiration();
}
//判断token是否可以被刷新,过期可被刷新
public boolean canRefresh(String token){
return !isTokenExpired(token);
}
//刷新token
public String refershToken(String token){
Claims claims = getClaimsFormToken(token);
claims.put(CLAIM_KEY_CREATED,new Date());
return generateToken(claims);
}
}
拦截器JWTInterceptor将redis中token取出来判断
public class JWTInterceptor extends ApiController implements HandlerInterceptor {
@Autowired
private StringRedisTemplate redisTemplate;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String token = request.getHeader("token");
// 校验token
if (StringUtils.isNotBlank(token)) {
// 根据 token 拿到用户名
String username;
try{
username = JwtTokenUtils.getUsername(token);
}catch (Exception e){
throw new ServiceException("token验证失败");
}
// 再根据获取到的用户名 从redis中获取数据
String sourceToken = redisTemplate.opsForValue().get(username);
// 如果能够获取到数据,说明token未过期
if (StringUtils.isNotBlank(sourceToken)) {
// 如果两个token相等,代表 token有效
if (Objects.equals(token, sourceToken)) {
// 请求放行
return true;
}
}
}
throw new ServiceException("NOTLOGIN");
}
}
service业务层,将token存入redis
@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {
@Autowired
private StringRedisTemplate redisTemplate;
@Resource
private UserDao userDao;
@Override
public String checkUsernameAndPassword(String username, String password) {
User user = userDao.selectUserByUsername(username);
if (user == null || !password.equals(user.getPassword())) {
throw new ServiceException("用户名或密码错误");
}
String token = JwtTokenUtils.createToken(user);
// 存入redis中
redisTemplate.opsForValue().set(user.getUsername(),token, JwtTokenUtils.EXPIRATION, TimeUnit.SECONDS);
return token;
}
}
将数据存入redis用下面语句
@Autowired
private StringRedisTemplate redisTemplate;
redisTemplate.opsForValue().set(user.getUsername(),token, JwtTokenUtils.EXPIRATION, TimeUnit.SECONDS);
set中的第一个参数是redis中的健,第二个参数是值,第三个参数是过期时间,第四个参数是时间单位
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public CacheManager cacheManager(final RedisConnectionFactory redisConnectionFactory)
{
RedisCacheManager.RedisCacheManagerBuilder builder
= RedisCacheManager.RedisCacheManagerBuilder.fromConnectionFactory(redisConnectionFactory);
return builder.build();
}
@Bean(name = "springSessionDefaultRedisSerializer")
public GenericJackson2JsonRedisSerializer getGenericJackson2JsonRedisSerializer()
{
return new GenericJackson2JsonRedisSerializer();
}
@Bean
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> getRedisTemplate(
final RedisConnectionFactory connectionFactory
)
{
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(connectionFactory);
// 配置默认的序列化器
redisTemplate.setDefaultSerializer(getGenericJackson2JsonRedisSerializer());
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 设置 Key 的默认序列化机制
redisTemplate.setKeySerializer(stringRedisSerializer);
redisTemplate.setHashKeySerializer(stringRedisSerializer);
return redisTemplate;
}
}
解决redis序列化问题
评论