|
|
|
@ -1,9 +1,11 @@ |
|
|
|
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; |
|
|
|
@ -18,37 +20,8 @@ import java.util.Map; |
|
|
|
|
|
|
|
/** |
|
|
|
* token 生成与验证工具 |
|
|
|
* @outhor dong.jun |
|
|
|
* @create 2022-06-13 16:23 |
|
|
|
* @company 深圳亿起融网络科技有限公司 |
|
|
|
*/ |
|
|
|
public class ApiTokenUtils { |
|
|
|
/** |
|
|
|
* cloudhub_token生成测试 |
|
|
|
* |
|
|
|
* @param args |
|
|
|
* @throws Exception |
|
|
|
*/ |
|
|
|
public static void main(String[] args) throws Exception { |
|
|
|
String issuer = "test"; |
|
|
|
Map<String, String> stringStringMap = new HashMap<>(); |
|
|
|
String cloudhub_token = getToken( |
|
|
|
issuer, //从天朗获取
|
|
|
|
stringStringMap.get("private_key"), //从天朗获取
|
|
|
|
5 * 60 * 1000L |
|
|
|
); |
|
|
|
System.out.printf( |
|
|
|
"issuer: %s\n" + |
|
|
|
"pri_key: %s\n" + |
|
|
|
"cloudhub_token: %s\n" + |
|
|
|
"check: %b\n", |
|
|
|
issuer, |
|
|
|
stringStringMap.get("private_key"), |
|
|
|
cloudhub_token, |
|
|
|
Jwts.parser().setSigningKey(stringToPublickKey(stringStringMap.get("public_key"))).parseClaimsJws(cloudhub_token) != null |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @param iss 从天朗获取 |
|
|
|
* @param pri 从天朗获取 |
|
|
|
@ -79,6 +52,19 @@ public class ApiTokenUtils { |
|
|
|
.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 |
|
|
|
@ -97,7 +83,6 @@ public class ApiTokenUtils { |
|
|
|
new X509EncodedKeySpec(decoded); |
|
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
|
return (RSAPublicKey) kf.generatePublic(spec); |
|
|
|
//return (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(publicKey.getBytes())));
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@ -115,15 +100,6 @@ public class ApiTokenUtils { |
|
|
|
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(decoded); |
|
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
|
return (RSAPrivateKey)kf.generatePrivate(spec); |
|
|
|
|
|
|
|
//return (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(org.apache.commons.codec.binary.Base64.decodeBase64(privateKey.getBytes())));
|
|
|
|
} |
|
|
|
|
|
|
|
public static Key stringToPublickKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException { |
|
|
|
return getPublicKey(key); |
|
|
|
} |
|
|
|
|
|
|
|
public static Key stringToPrivateKey(String key) throws Exception { |
|
|
|
return getPrivateKey(key); |
|
|
|
} |
|
|
|
} |
|
|
|
|