package com.terra.proxy.util;
|
|
import com.alibaba.fastjson.JSONObject;
|
import io.jsonwebtoken.Claims;
|
import io.jsonwebtoken.Jwts;
|
import io.jsonwebtoken.SignatureAlgorithm;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.stereotype.Component;
|
|
import java.util.Date;
|
|
/**
|
* @author lanbase
|
* @Description: TODO(jwt工具类)
|
* @date 2017-9-27 14:33
|
*/
|
@ConfigurationProperties(prefix = "sys.jwt")
|
@Component
|
public class JwtUtils {
|
|
private Logger logger = LoggerFactory.getLogger(getClass());
|
|
private String secret;
|
|
private long expire;
|
|
private String header;
|
|
private String appidtokentime;
|
|
|
/**
|
* 生成jwt token
|
*
|
* @param userId
|
* @return
|
*/
|
|
|
public String generateToken(long userId) {
|
Date nowDate = new Date();
|
//过期时间
|
Date expireDate = new Date(nowDate.getTime() + expire * 1000);
|
|
return Jwts.builder()
|
.setHeaderParam("type", "JWT")
|
.setSubject(userId + "")
|
.setIssuedAt(nowDate)
|
.setExpiration(expireDate)
|
.signWith(SignatureAlgorithm.HS512, secret)
|
.compact();
|
}
|
|
public String generateToken(String msg, Long expireSeconds) {
|
Date nowDate = new Date();
|
//过期时间
|
Date expireDate = new Date(nowDate.getTime() + expire * 1000);
|
if (expireSeconds != null) {
|
expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
|
}
|
|
return Jwts.builder()
|
.setHeaderParam("type", "JWT")
|
.setSubject(msg)
|
.setIssuedAt(nowDate)
|
.setExpiration(expireDate)
|
.signWith(SignatureAlgorithm.HS512, secret)
|
.compact();
|
}
|
|
|
public String generateToken(Long expireSeconds, Integer appId) {
|
if (expireSeconds == null) {
|
//根据appid,获取对应的token有效持续时间
|
JSONObject appmap = JSONObject.parseObject(appidtokentime);
|
expireSeconds = appmap.getLongValue(appId.toString());
|
}
|
Date nowDate = new Date();
|
//过期时间
|
Date expireDate = new Date(nowDate.getTime() + expire * 1000);
|
if (expireSeconds != null) {
|
expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
|
}
|
|
return Jwts.builder()
|
.setHeaderParam("type", "JWT")
|
.setSubject(appId.toString())
|
.setIssuedAt(nowDate)
|
.setExpiration(expireDate)
|
.signWith(SignatureAlgorithm.HS512, secret)
|
.compact();
|
}
|
|
|
public String generateToken(Integer userid, Long expireSeconds) {
|
Date nowDate = new Date();
|
//过期时间
|
Date expireDate = new Date(nowDate.getTime() + expire * 1000);
|
if (expireSeconds != null) {
|
expireDate = new Date(nowDate.getTime() + expireSeconds.longValue() * 1000);
|
}
|
|
return Jwts.builder()
|
.setHeaderParam("type", "JWT")
|
.setSubject(userid.toString())
|
.setIssuedAt(nowDate)
|
.setExpiration(expireDate)
|
.signWith(SignatureAlgorithm.HS512, secret)
|
.compact();
|
}
|
|
public Claims getClaimByToken(String token) {
|
try {
|
return Jwts.parser()
|
.setSigningKey(secret)
|
.parseClaimsJws(token)
|
.getBody();
|
} catch (Exception e) {
|
return null;
|
}
|
}
|
|
|
/**
|
* token是否过期
|
*
|
* @param expiration
|
* @return
|
*/
|
public boolean isTokenExpired(Date expiration) {
|
return expiration.before(new Date());
|
}
|
|
public String getSecret() {
|
return secret;
|
}
|
|
public void setSecret(String secret) {
|
this.secret = secret;
|
}
|
|
public long getExpire() {
|
return expire;
|
}
|
|
public void setExpire(long expire) {
|
this.expire = expire;
|
}
|
|
public String getHeader() {
|
return header;
|
}
|
|
public void setHeader(String header) {
|
this.header = header;
|
}
|
|
}
|