【发布时间】:2021-01-06 07:04:11
【问题描述】:
我正在尝试解决此处介绍的 RBAC 授权示例:https://quarkus.io/guides/security-jwt#generate-jwt-tokens。 GenerateToken 类的代码与示例中的代码几乎相同,我只是删除了声明:
public class GenerateToken
{
public static void main(String[] args)
{
String token = Jwt.issuer("CN=me, OU=quarkus, O=quarkus, C=FR").upn("me").groups(new HashSet<>(Arrays.asList("User", "Admin"))).sign();
log.info (">>> GenerateToken.main(): Token is {}", token);
}
}
现在我需要生成私钥和公钥 .pem 文件。我写了以下脚本:
keytool -genkey -keyalg RSA -keystore ./jks/selfsigned.jks -keysize 2048 -dname "CN=me,OU=quarkus,O=quarkus,C=FR"
keytool -exportcert -keystore ./jks/selfsigned.jks -rfc -file src/main/resources/META-INF/resources/public-key.pem
keytool -importkeystore -srckeystore ./jks/selfsigned.jks -destkeystore ./jks/selfsigned.p12 -srcstoretype jks -deststoretype pkcs12
openssl pkcs12 -in ./jks/selfsigned.p12 -nodes -nocerts -out src/test/resources/private-key.pem
据我所知,private-key.pem 和 public-key.pem 文件生成正确。但是运行 GenerateToken 类会引发以下异常:
mvn exec:java -Dexec.classpathScope=test -Dsmallrye.jwt.sign.key-location=private-key.pem
[INFO] Scanning for projects...
...
[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ eclipse-microprofile ---
[WARNING]
java.lang.NullPointerException
at io.smallrye.jwt.build.impl.JwtSignatureImpl.keyAlgorithm (JwtSignatureImpl.java:186)
at io.smallrye.jwt.build.impl.JwtSignatureImpl.signInternal (JwtSignatureImpl.java:150)
at io.smallrye.jwt.build.impl.JwtSignatureImpl.sign (JwtSignatureImpl.java:72)
at ... GenerateToken.main (GenerateToken.java:14)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke (Method.java:566)
at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:282)
at java.lang.Thread.run (Thread.java:834)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.572 s
[INFO] Finished at: 2020-09-19T18:10:50+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:java (default-cli) on project eclipse-microprofile: An exception occured while executing the Java class. null: NullPointerException -> [Help 1]
使用提供的 publicKey.pem 和 privateKey.pem 运行原始示例,当然可以正常工作。我在这里做错了什么?
非常感谢。
西摩玻璃
【问题讨论】: