ITeye博客

使用RSA密钥生成JWT

GodJohnny · 2016-10-26 18:08 · 编程语言 · oauth · keytool · RSA · Markdown

在内部系统上调用基础服务时,在基础服务上加上OAuth验证,基于Spring boot OAuth2.0实现,采用JsonWebToken的方式。accessToken由调用者自己生成,基于RSA生成私钥签名,基础服务公钥验证。 accessToken的生成采用开源的JJWT实现,基础服务的OAuth由spring-security-oauth2框架来自动实现(见:http://godjohnny.iteye.com/blog/2320220)。 生成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
  参见:<https://tools.ietf.org/html/rfc6750#section-2.1>

基础服务中,启动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) {
			}
		}
	}

评论(1)

  1. #1 楼 · u010011083 · 2018-06-14 19:15

    请问,JKSUtil.getInstance().getKeyPair("test") 这个 test是在哪里指定的,还有password是哪里来的。 我是用puttygen软件生成的密匙和公匙

评论功能维护中,当前仅展示历史评论。