|
|
@ -11,8 +11,10 @@ import com.auth0.jwt.interfaces.DecodedJWT; |
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
import org.apache.commons.codec.binary.Base64; |
|
|
import org.apache.commons.logging.Log; |
|
|
import org.apache.commons.logging.Log; |
|
|
import org.apache.commons.logging.LogFactory; |
|
|
import org.apache.commons.logging.LogFactory; |
|
|
|
|
|
import sun.misc.BASE64Decoder; |
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.BufferedReader; |
|
|
|
|
|
import java.io.FileReader; |
|
|
import java.security.KeyFactory; |
|
|
import java.security.KeyFactory; |
|
|
import java.security.NoSuchAlgorithmException; |
|
|
import java.security.NoSuchAlgorithmException; |
|
|
import java.security.interfaces.RSAPrivateKey; |
|
|
import java.security.interfaces.RSAPrivateKey; |
|
|
@ -78,6 +80,8 @@ public class SignUtils { |
|
|
*/ |
|
|
*/ |
|
|
private static Map getTypeMap(String type) { |
|
|
private static Map getTypeMap(String type) { |
|
|
Map nmap = new HashMap(); |
|
|
Map nmap = new HashMap(); |
|
|
|
|
|
// 清空
|
|
|
|
|
|
nmap.clear(); |
|
|
switch (type) { |
|
|
switch (type) { |
|
|
case "user_real": |
|
|
case "user_real": |
|
|
nmap = userRealMap(); |
|
|
nmap = userRealMap(); |
|
|
@ -157,6 +161,7 @@ public class SignUtils { |
|
|
// 公钥证书路径
|
|
|
// 公钥证书路径
|
|
|
String pubkey=(String) xmap.get("public_key"); |
|
|
String pubkey=(String) xmap.get("public_key"); |
|
|
// 公钥证书
|
|
|
// 公钥证书
|
|
|
|
|
|
// 读取证书内容
|
|
|
RSAPublicKey rsaPublicKey = getPublicKey(pubkey); |
|
|
RSAPublicKey rsaPublicKey = getPublicKey(pubkey); |
|
|
// 解析对象,算法与加密时候一致
|
|
|
// 解析对象,算法与加密时候一致
|
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.RSA256(rsaPublicKey,null)).build(); |
|
|
JWTVerifier jwtVerifier = JWT.require(Algorithm.RSA256(rsaPublicKey,null)).build(); |
|
|
@ -182,28 +187,39 @@ public class SignUtils { |
|
|
* @throws InvalidKeySpecException |
|
|
* @throws InvalidKeySpecException |
|
|
*/ |
|
|
*/ |
|
|
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { |
|
|
public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException { |
|
|
String publicKeyPEM = publicKey.replace("-----BEGIN PUBLIC KEY-----\r\n", ""); |
|
|
// 读取证书文件内容
|
|
|
|
|
|
String keycontent = getKeyContent(publicKey); |
|
|
|
|
|
// 剔除证书中的换行符
|
|
|
|
|
|
String publicKeyPEM = keycontent.replace("-----BEGIN PUBLIC KEY-----\r\n", ""); |
|
|
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); |
|
|
publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); |
|
|
|
|
|
|
|
|
Base64 b64 = new Base64(); |
|
|
try { |
|
|
byte [] decoded = b64.decode(publicKeyPEM); |
|
|
Base64 b64 = new Base64(); |
|
|
|
|
|
byte [] decoded = b64.decode(publicKeyPEM); |
|
|
|
|
|
|
|
|
X509EncodedKeySpec spec = |
|
|
X509EncodedKeySpec spec = |
|
|
new X509EncodedKeySpec(decoded); |
|
|
new X509EncodedKeySpec(decoded); |
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
return (RSAPublicKey) kf.generatePublic(spec); |
|
|
return (RSAPublicKey) kf.generatePublic(spec); |
|
|
|
|
|
}catch (Exception ex){ |
|
|
|
|
|
ex.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 抽取私钥 |
|
|
* 抽取私钥 |
|
|
* @param privateKey |
|
|
* @param privateKey 私钥路径 |
|
|
* @return |
|
|
* @return |
|
|
* @throws Exception |
|
|
* @throws Exception |
|
|
*/ |
|
|
*/ |
|
|
public static RSAPrivateKey getPrivateKey(String privateKey) throws Exception { |
|
|
public static RSAPrivateKey getPrivateKey(String privateKey) throws Exception { |
|
|
String privKeyPEM = privateKey.replace("-----BEGIN PRIVATE KEY-----\r\n", ""); |
|
|
// 读取路径的证书信息
|
|
|
|
|
|
String keycontent = getKeyContent(privateKey); |
|
|
|
|
|
//
|
|
|
|
|
|
String privKeyPEM = keycontent.replace("-----BEGIN PRIVATE KEY-----\r\n", ""); |
|
|
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); |
|
|
privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", ""); |
|
|
Base64 b64 = new Base64(); |
|
|
Base64 b64 = new Base64(); |
|
|
byte [] decoded = b64.decode(privKeyPEM); |
|
|
byte [] decoded = b64.decode(privKeyPEM); |
|
|
@ -212,4 +228,27 @@ public class SignUtils { |
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
KeyFactory kf = KeyFactory.getInstance("RSA"); |
|
|
return (RSAPrivateKey)kf.generatePrivate(spec); |
|
|
return (RSAPrivateKey)kf.generatePrivate(spec); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 读取证书文件内容 |
|
|
|
|
|
* @param path |
|
|
|
|
|
* @return |
|
|
|
|
|
*/ |
|
|
|
|
|
private static String getKeyContent(String path){ |
|
|
|
|
|
StringBuffer sb= new StringBuffer(); |
|
|
|
|
|
try{ |
|
|
|
|
|
BufferedReader br = new BufferedReader(new FileReader(path)); |
|
|
|
|
|
String t; |
|
|
|
|
|
while ((t=br.readLine())!=null){ |
|
|
|
|
|
if(!t.startsWith("-")){ |
|
|
|
|
|
sb.append(t.trim()); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
}catch (Exception ex){ |
|
|
|
|
|
log.error("证书文件不存在:"+path); |
|
|
|
|
|
ex.printStackTrace(); |
|
|
|
|
|
} |
|
|
|
|
|
return sb.toString(); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|