如何为 Quarkus 配置 JPA 安全性(获取 NullPointerException)?
How to configure JPA security for Quarkus (Getting NullPointerException)?
9.1 根据我的 gradle.properties
文件:
#Gradle properties
#Wed Oct 28 14:48:17 EDT 2020
quarkusPluginVersion=1.9.1.Final
quarkusPlatformArtifactId=quarkus-universe-bom
quarkusPlatformGroupId=io.quarkus
quarkusPlatformVersion=1.9.1.Final
org.gradle.logging.level=INFO
在我的 application.properties
我有:
quarkus.datasource.db-kind=mssql
%dev.quarkus.datasource.username=foo
%dev.quarkus.datasource.password=bar
%dev.quarkus.datasource.jdbc.url=jdbc:sqlserver://xxxxxx;databaseName=xxxxxx
quarkus.hibernate-orm.database.generation=none
我的实体:
import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.security.jpa.Password;
import io.quarkus.security.jpa.UserDefinition;
import io.quarkus.security.jpa.Username;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "my_user")
@UserDefinition
public class MyUser extends PanacheEntity {
@Username
public String username;
@Password
public String password;
/**
* Adds a new user in the database
* @param username the user name
* @param password the unencrypted password (it will be encrypted with bcrypt)
*/
public static void add(String username, String password) {
MyUser myUser = new MyUser();
myUser.username = username;
myUser.password = BcryptUtil.bcryptHash(password);
myUser.persist();
}
}
初始化代码:
import com.xxxxxx.MyUser;
import io.quarkus.runtime.StartupEvent;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;
import javax.transaction.Transactional;
@Singleton
public class Startup {
@Transactional
public void loadUsers(@Observes StartupEvent evt) {
// reset and load all test users
MyUser.deleteAll();
MyUser.add("test", "test");
MyUser.add("user", "user");
}
}
但是当我启动 Quarkus 时,我得到...
2020-11-09 15:59:50,320 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[error]: Build step io.quarkus.security.jpa.deployment.QuarkusSecurityJpaProcessor#configureJpaAuthConfig threw an exception: java.lang.NullPointerException
at io.quarkus.security.jpa.deployment.JpaSecurityDefinition.getFieldOrMethod(JpaSecurityDefinition.java:81)
at io.quarkus.security.jpa.deployment.JpaSecurityDefinition.<init>(JpaSecurityDefinition.java:76)
at io.quarkus.security.jpa.deployment.QuarkusSecurityJpaProcessor.configureJpaAuthConfig(QuarkusSecurityJpaProcessor.java:107)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.quarkus.deployment.ExtensionLoader.execute(ExtensionLoader.java:936)
at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
您的用户实体中缺少角色变量(带有注释@Roles)。
这是来自quarkus.pro的教程,它是第 4 点。
This indicates the comma-separated list of roles added to the target Principal representation attributes.
9.1 根据我的 gradle.properties
文件:
#Gradle properties
#Wed Oct 28 14:48:17 EDT 2020
quarkusPluginVersion=1.9.1.Final
quarkusPlatformArtifactId=quarkus-universe-bom
quarkusPlatformGroupId=io.quarkus
quarkusPlatformVersion=1.9.1.Final
org.gradle.logging.level=INFO
在我的 application.properties
我有:
quarkus.datasource.db-kind=mssql
%dev.quarkus.datasource.username=foo
%dev.quarkus.datasource.password=bar
%dev.quarkus.datasource.jdbc.url=jdbc:sqlserver://xxxxxx;databaseName=xxxxxx
quarkus.hibernate-orm.database.generation=none
我的实体:
import io.quarkus.elytron.security.common.BcryptUtil;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import io.quarkus.security.jpa.Password;
import io.quarkus.security.jpa.UserDefinition;
import io.quarkus.security.jpa.Username;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "my_user")
@UserDefinition
public class MyUser extends PanacheEntity {
@Username
public String username;
@Password
public String password;
/**
* Adds a new user in the database
* @param username the user name
* @param password the unencrypted password (it will be encrypted with bcrypt)
*/
public static void add(String username, String password) {
MyUser myUser = new MyUser();
myUser.username = username;
myUser.password = BcryptUtil.bcryptHash(password);
myUser.persist();
}
}
初始化代码:
import com.xxxxxx.MyUser;
import io.quarkus.runtime.StartupEvent;
import javax.enterprise.event.Observes;
import javax.inject.Singleton;
import javax.transaction.Transactional;
@Singleton
public class Startup {
@Transactional
public void loadUsers(@Observes StartupEvent evt) {
// reset and load all test users
MyUser.deleteAll();
MyUser.add("test", "test");
MyUser.add("user", "user");
}
}
但是当我启动 Quarkus 时,我得到...
2020-11-09 15:59:50,320 ERROR [io.qua.dep.dev.IsolatedDevModeMain] (main) Failed to start quarkus: java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
[error]: Build step io.quarkus.security.jpa.deployment.QuarkusSecurityJpaProcessor#configureJpaAuthConfig threw an exception: java.lang.NullPointerException
at io.quarkus.security.jpa.deployment.JpaSecurityDefinition.getFieldOrMethod(JpaSecurityDefinition.java:81)
at io.quarkus.security.jpa.deployment.JpaSecurityDefinition.<init>(JpaSecurityDefinition.java:76)
at io.quarkus.security.jpa.deployment.QuarkusSecurityJpaProcessor.configureJpaAuthConfig(QuarkusSecurityJpaProcessor.java:107)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at io.quarkus.deployment.ExtensionLoader.execute(ExtensionLoader.java:936)
at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
at org.jboss.threads.ContextClassLoaderSavingRunnable.run(ContextClassLoaderSavingRunnable.java:35)
at org.jboss.threads.EnhancedQueueExecutor.safeRun(EnhancedQueueExecutor.java:2046)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.doRunTask(EnhancedQueueExecutor.java:1578)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1452)
at java.base/java.lang.Thread.run(Thread.java:834)
at org.jboss.threads.JBossThread.run(JBossThread.java:479)
您的用户实体中缺少角色变量(带有注释@Roles)。
这是来自quarkus.pro的教程,它是第 4 点。
This indicates the comma-separated list of roles added to the target Principal representation attributes.