我们在.NET Core中生成了一个Bearer JWT令牌,并使用使用Powershell生成的X509自签名证书的pfx对令牌进行了签名。
我们需要在Springboot Java应用程序中验证令牌签名。 为实现这一目标,我们使用Java中的密钥工具将pfx导入到java密钥库(jks)中。
getFilesWithDetails
我们正在验证资源服务器中的令牌签名,我们在其中配置了如下配置的JwtAccessTokenConverter。
keytool -importkeystore -srckeystore "certificate.pfx" -srcstoretype pkcs12 -destkeystore "clientcert.jks" -deststoretype JKS
应用程序中的Controller使用以下元素进行修饰。
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
InputStream inJksStream = null;
InputStream inputJksStream = null;
inJksStream = new FileInputStream("clientcert.jks");
inputJksStream = new BufferedInputStream(inJksStream);
KeyStore trustStore = KeyStore.getInstance("jks");
trustStore.load(inputJksStream, "password".toCharArray());
Key trustStorePrivateKey = trustStore.getKey("alias-value", "password".toCharArray());
converter.setSigningKey(Base64.getEncoder().encodeToString(trustStorePrivateKey.getEncoded()));
但是,当有效的JWT令牌与Springboot应用程序的请求一起传递时,我们会遇到签名无效的异常。
这是在Springboot中验证Bearer JWT令牌的适当方法吗?
答案 0 :(得分:0)
我在使用IdentiyServer4发布JWT时遇到了类似的问题。如果我能看到错误的输出,我可以提供帮助。
答案 1 :(得分:0)
我建议使用auth0库验证您的JWT。我刚刚在一个项目中实现了它,它比其他库更容易,并且涉及的代码少得多。
maven:com.auth0 / java-jwt / 3.0.1
可以在下面找到验证JWT的简单方法......
public static DecodedJWT decodeJWT(String jwtString) {
try {
String secret = //get your secret key
return JWT.require(Algorithm.HMAC256(secret))
.withIssuer("foo")
.build()
.verify(jwtString);
} catch (IOException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}