4 changed files with 123 additions and 132 deletions
@ -1,114 +0,0 @@ |
|||
package cn.chjyj.szwh.utils; |
|||
|
|||
import io.jsonwebtoken.Claims; |
|||
import io.jsonwebtoken.Jwts; |
|||
import io.jsonwebtoken.SignatureAlgorithm; |
|||
import org.apache.commons.codec.binary.Base64; |
|||
|
|||
import javax.xml.bind.DatatypeConverter; |
|||
import java.security.Key; |
|||
import java.security.KeyFactory; |
|||
import java.security.NoSuchAlgorithmException; |
|||
import java.security.interfaces.RSAPrivateKey; |
|||
import java.security.interfaces.RSAPublicKey; |
|||
import java.security.spec.InvalidKeySpecException; |
|||
import java.security.spec.PKCS8EncodedKeySpec; |
|||
import java.security.spec.X509EncodedKeySpec; |
|||
import java.util.Date; |
|||
import java.util.HashMap; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* token 生成与验证工具 |
|||
*/ |
|||
public class ApiTokenUtils { |
|||
/** |
|||
* @param iss 从天朗获取 |
|||
* @param pri 从天朗获取 |
|||
* @param exp token有效期 |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static String getToken(String iss, String pri, Long exp) throws Exception { |
|||
Date date = new Date(System.currentTimeMillis()); |
|||
return Jwts |
|||
.builder() |
|||
.signWith(SignatureAlgorithm.RS256, getPrivateKey(pri)) |
|||
.setIssuer(iss) |
|||
.setExpiration(new Date(date.getTime() + exp)) |
|||
.setIssuedAt(date) |
|||
.compact(); |
|||
} |
|||
|
|||
/** |
|||
* 生产token |
|||
* @param iss 发行者 |
|||
* @param pri 私钥 |
|||
* @param exp 过期时间 long型 |
|||
* @param claims 验证参数 |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static String getToken(String iss, String pri, Long exp, Map<String, Object> claims) throws Exception { |
|||
Date date = new Date(System.currentTimeMillis()); |
|||
return Jwts |
|||
.builder() |
|||
.setClaims(claims) |
|||
.signWith(SignatureAlgorithm.RS256, getPrivateKey(pri)) |
|||
.setIssuer(iss) |
|||
.setExpiration(new Date(date.getTime() + exp)) |
|||
.setIssuedAt(date) |
|||
.compact(); |
|||
} |
|||
|
|||
/** |
|||
* 解析jwt token |
|||
* 公钥解密 |
|||
* @param jwt |
|||
* @return |
|||
*/ |
|||
public static Claims parseJWT(String jwt,String pubkey) throws Exception { |
|||
return Jwts.parser() |
|||
.setSigningKey(pubkey) |
|||
.parseClaimsJws(jwt) |
|||
.getBody(); |
|||
} |
|||
|
|||
/** |
|||
* 抽取公钥 |
|||
* @param publicKey |
|||
* @return |
|||
* @throws NoSuchAlgorithmException |
|||
* @throws InvalidKeySpecException |
|||
*/ |
|||
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { |
|||
String publicKeyPEM = publicKey.replace("-----BEGIN PUBLIC KEY-----\r\n", ""); |
|||
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); |
|||
|
|||
Base64 b64 = new Base64(); |
|||
byte [] decoded = b64.decode(publicKeyPEM); |
|||
|
|||
X509EncodedKeySpec spec = |
|||
new X509EncodedKeySpec(decoded); |
|||
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|||
return (RSAPublicKey) kf.generatePublic(spec); |
|||
} |
|||
|
|||
/** |
|||
* 抽取私钥 |
|||
* @param privateKey |
|||
* @return |
|||
* @throws Exception |
|||
*/ |
|||
public static RSAPrivateKey getPrivateKey(String privateKey) throws Exception { |
|||
String privKeyPEM = privateKey.replace("-----BEGIN PRIVATE KEY-----\r\n", ""); |
|||
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); |
|||
Base64 b64 = new Base64(); |
|||
byte [] decoded = b64.decode(privKeyPEM); |
|||
|
|||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded); |
|||
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|||
return (RSAPrivateKey)kf.generatePrivate(spec); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue