이 콘텐츠는 선택한 언어로 제공되지 않습니다.
Chapter 2. Quarkus Security with Jakarta Persistence
You can configure your application to use Jakarta Persistence to store users' identities.
Quarkus provides a Jakarta Persistence identity provider, similar to the JDBC identity provider. Jakarta Persistence is suitable for use with the Basic and Form-based Quarkus Security mechanisms, which require username and password credentials.
The Jakarta Persistence IdentityProvider creates a SecurityIdentity instance. During user authentication, this instance is used to verify and authorize access requests.
For a practical example, see the Getting started with Security using Basic authentication and Jakarta Persistence tutorial.
2.1. Jakarta Persistence entity specification 링크 복사링크가 클립보드에 복사되었습니다!
Quarkus security offers a Jakarta Persistence integration to collect usernames, passwords, and roles and store them into Jakarta Persistence database entities.
The following Jakarta Persistence entity specification demonstrates how users' information needs to be stored in a Jakarta Persistence entity and correctly mapped so that Quarkus can retrieve this information from a database.
-
The
@UserDefinitionannotation must be present on a Jakarta Persistence entity, regardless of whether simplified Hibernate ORM with Panache is used or not. -
The
@Usernameand@Passwordfield types are alwaysString. -
The
@Rolesfield must either beString,Collection<String>, or aCollection<X>, whereXis an entity class with a singleStringfield annotated as@RolesValue. -
Each
Stringrole element type is parsed as a comma-separated list of roles.
The following example demonstrates storing security information by adding annotations to the user entity:
The quarkus-security-jpa extension initializes only if a single entity is annotated with @UserDefinition.
- 1
- Table name. Do not define it as
user, which is a restricted keyword in most databases. - 2
- The
@UserDefinitionannotation must be present on a single entity, either a regular Hibernate ORM entity or a Hibernate ORM with Panache entity. - 3
- Indicates the field used for the username.
- 4
- Indicates the field used for the password. By default,
quarkus-security-jpauses bcrypt-hashed passwords, or you can configure plain text or custom passwords instead. - 5
- This indicates the comma-separated list of roles added to the target principal representation attributes.
- 6
- This method lets you add users while hashing passwords with the proper
bcrypthash.
2.2. Jakarta Persistence entity as storage of roles 링크 복사링크가 클립보드에 복사되었습니다!
Use the following example to store roles inside another Jakarta Persistence entity:
This example demonstrates storing and accessing roles. To update an existing user or create a new one, annotate public List<Role> roles with @Cascade(CascadeType.ALL) or choose a specific CascadeType.
2.3. Password storage and hashing 링크 복사링크가 클립보드에 복사되었습니다!
When developing applications with Quarkus, you can decide how to manage password storage and hashing. You can keep the default password and hashing settings of Quarkus, or you can hash passwords manually.
With the default option, passwords are stored and hashed with bcrypt under the Modular Crypt Format (MCF). While using MCF, the hashing algorithm, iteration count, and salt are stored as a part of the hashed value. As such, we do not need dedicated columns to keep them.
In cryptography, a salt is a name for random data used as an additional input to a one-way function that hashes data, a password, or a passphrase.
To represent passwords stored in the database that were hashed by different algorithms, create a class that implements org.wildfly.security.password.PasswordProvider as shown in the following example.
The following snippet shows how to set a custom password provider that represents a password that was hashed with the SHA256 hashing algorithm.
To quickly create a hashed password, use String BcryptUtil.bcryptHash(String password), which defaults to creating a random salt and hashing in ten iterations. This method also allows specifying the number of iterations and salt used.
For applications running in a production environment, do not store passwords as plain text.
However, it is possible to store passwords as plain text with the @Password(PasswordType.CLEAR) annotation when operating in a test environment.
The Hibernate Multitenancy is supported, and you can store the user entity in a persistence unit with enabled multitenancy. However, if your io.quarkus.hibernate.orm.runtime.tenant.TenantResolver must access the io.vertx.ext.web.RoutingContext to resolve request details, you must disable proactive authentication. For more information about proactive authentication, see the Quarkus Proactive authentication guide.
🔒 Fixed at build time: Configuration property fixed at build time - All other configuration properties are overridable at runtime
| Configuration property | Type | Default |
|
🔒 Fixed at build time Selects the Hibernate ORM persistence unit. Default persistence unit is used when no value is specified.
Environment variable: | string |
|