使用RSA密钥生成JWT

在内部系统上调用基础服务时,在基础服务上加上OAuth验证,基于Spring boot OAuth2.0实现,采用JsonWebToken的方式。accessToken由调用者自己生成,基于RSA生成私钥签名,基础服务公钥验证。
accessToken的生成采用开源的JJWT实现,基础服务的OAuth由spring-security-oauth2框架来自动实现(见:[url]http://godjohnny.iteye.com/blog/2320220[/url])。
生成accessToken的步骤:
1、生成密钥库文件。
使用keytool。具体过程略。
2、使用该密钥文件生成accessToken
2.1 从密钥库文件加载密钥对
public class JKSUtil {
private String keyStoreFile;
private char[] password;
private KeyStore store;
private Object lock = new Object();

private static JKSUtil instance = null;

public static JKSUtil getInstance() {
synchronized (JKSUtil.class) {
if (instance == null) {
synchronized (JKSUtil.class) {
instance = new JKSUtil("/keystore.jks", "foobar".toCharArray());
}
}
return instance;
}
}

private JKSUtil(String _jksFilePath, char[] password) {
this.keyStoreFile = _jksFilePath;
this.password = password;
}

public KeyPair getKeyPair(String alias) {
return getKeyPair(alias, this.password);
}

public KeyPair getKeyPair(String alias, char[] password) {
try {
synchronized (this.lock) {
if (this.store == null) {
synchronized (this.lock) {
InputStream is = this.getClass().getResourceAsStream(keyStoreFile);
try {
this.store = KeyStore.getInstance("JKS");
this.store.load(is, this.password);
} finally {
if (is != null) {
try {
is.close();
} catch (Exception e) {
}
}
}
}
}
}
RSAPrivateCrtKey key = (RSAPrivateCrtKey) this.store.getKey(alias, password);
RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
return new KeyPair(publicKey, key);
} catch (Exception e) {
throw new IllegalStateException("Cannot load keys from store: " + this.keyStoreFile, e);
}
}
}

2.2 使用该密钥对,使用JJWT生成accessToken
   public static String generateAccessToken() throws Exception {
KeyPair keyPair = JKSUtil.getInstance().getKeyPair("test");
String compactJws = Jwts.builder().setSubject("MyService")
.signWith(SignatureAlgorithm.RS256, keyPair.getPrivate()).compact();
System.out.println(compactJws);
return compactJws;
}

3、在调用基础服务时,HTTP头上加上accessToken
参见:[url]https://tools.ietf.org/html/rfc6750#section-2.1[/url]

基础服务中,启动Oauth2.0验证,并加上密钥库中的公钥(PEM格式)。导出密钥库文件中的公钥,仍然使用keytool命令。

其他:
1、从密钥库中导出DER格式公钥(证书)的命令:
keytool -exportcert -file publie2.crt -keystore keystore.jks -alias test
2、从密钥库中导出PEM格式公钥(证书)的命令:
keytool -exportcert -rfc -file publie2.crt -keystore keystore.jks -alias test
3、JAVA中加载DER格式公钥的方式:
public static PublicKey loadPublicKey(String _pubKeyFile) throws CertificateException, FileNotFoundException {
FileInputStream fis = new FileInputStream(_pubKeyFile);
try {
CertificateFactory certificatefactory = CertificateFactory.getInstance("X.509");
X509Certificate Cert = (X509Certificate) certificatefactory.generateCertificate(fis);
PublicKey pk = Cert.getPublicKey();
return pk;
} finally {
try {
fis.close();
} catch (IOException e) {
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值