You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.7 KiB
68 lines
1.7 KiB
/**
|
|
* Copyright (c) 2018 人人开源 All rights reserved.
|
|
*
|
|
* https://www.renren.io
|
|
*
|
|
* 版权所有,侵权必究!
|
|
*/
|
|
|
|
package com.epmet.jwt;
|
|
|
|
import io.jsonwebtoken.Claims;
|
|
import io.jsonwebtoken.Jwts;
|
|
import io.jsonwebtoken.SignatureAlgorithm;
|
|
import org.joda.time.DateTime;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.Date;
|
|
|
|
/**
|
|
* Jwt工具类
|
|
*
|
|
* @author Mark sunlightcs@gmail.com
|
|
* @since 1.0.0
|
|
*/
|
|
@Component
|
|
public class JwtUtils {
|
|
private static final Logger logger = LoggerFactory.getLogger(JwtUtils.class);
|
|
|
|
/* @Autowired
|
|
private JwtProperties jwtProperties;*/
|
|
@Autowired
|
|
private JwtTokenProperties jwtProperties;
|
|
/**
|
|
* 生成jwt token
|
|
*/
|
|
public String generateToken(Long userId) {
|
|
return Jwts.builder()
|
|
.setHeaderParam("typ", "JWT")
|
|
.setSubject(userId+"")
|
|
.setIssuedAt(new Date())
|
|
.setExpiration(DateTime.now().plusSeconds(jwtProperties.getExpire()).toDate())
|
|
.signWith(SignatureAlgorithm.HS512, jwtProperties.getSecret())
|
|
.compact();
|
|
}
|
|
|
|
public Claims getClaimByToken(String token) {
|
|
try {
|
|
return Jwts.parser()
|
|
.setSigningKey(jwtProperties.getSecret())
|
|
.parseClaimsJws(token)
|
|
.getBody();
|
|
}catch (Exception e){
|
|
logger.debug("validate is token error, token = " + token, e);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* token是否过期
|
|
* @return true:过期
|
|
*/
|
|
public boolean isTokenExpired(Date expiration) {
|
|
return expiration.before(new Date());
|
|
}
|
|
}
|
|
|