-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancements for the JPA-related content
Signed-off-by: Michal Maléř <[email protected]>
- Loading branch information
1 parent
5849491
commit c191da7
Showing
4 changed files
with
137 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
[id="security-jpa-concept"] | ||
= Quarkus Security with JPA | ||
include::_attributes.adoc[] | ||
:categories: security,jpa | ||
|
||
JPA is an identity provider, similar to xref:[JDBC], suitable for use with the xref:security-basic-authentication-concept.adoc[Basic] and xref:security-authentication-mechanisms-concept.adoc#form-auth[Form-based] Quarkus security mechanisms, which require a combination of username and password credentials. | ||
|
||
The JPA `IdentityProvider` creates a `SecurityIdentity` instance, which is used during user authentication to verify and authorize access requests making your Quarkus application secure. | ||
|
||
For an example of practical use of Basic authentication and JPA, see the xref:security-basic-authentication-tutorial.adoc[Secure a Quarkus application with Basic authentication and JPA] tutorial. | ||
|
||
|
||
== JPA entity specification | ||
|
||
Quarkus security offers a JPA integration to collect usernames, passwords, and roles, and store them into JPA database entities. | ||
|
||
The following JPA entity specification demonstrates how users' information needs to be stored in a JPA entity and properly mapped so that Quarkus can call this information from a database. | ||
|
||
The following annotated classes match specific rows in a database. | ||
JPA then maps the rows of such a database to Java instances of corresponding object classes. | ||
|
||
* The `@UserDefinition` class must be a JPA entity, no matter if link:https://quarkus.io/guides/hibernate-orm-panache[simplified Hibernate ORM with Panache] is used or not. | ||
|
||
* The `@Username` and `@Password` field types are always `String`. | ||
|
||
* The `@Roles` field must either be `String`, `Collection<String>`, or a `Collection<X>`, where `X` is an entity class with a single `String` field annotated as `@RolesValue`. | ||
|
||
* Each `String` role element type is parsed as a comma-separated list of roles. | ||
|
||
== JPA entity as storage of roles | ||
|
||
Use the following example to store roles inside another JPA entity: | ||
|
||
[source,java] | ||
---- | ||
@UserDefinition | ||
@Table(name = "test_user") | ||
@Entity | ||
public class User extends PanacheEntity { | ||
@Username | ||
public String name; | ||
@Password | ||
public String pass; | ||
@ManyToMany | ||
@Roles | ||
public List<Role> roles = new ArrayList<>(); | ||
} | ||
@Entity | ||
public class Role extends PanacheEntity { | ||
@ManyToMany(mappedBy = "roles") | ||
public List<ExternalRolesUserEntity> users; | ||
@RolesValue | ||
public String role; | ||
} | ||
---- | ||
|
||
== Password storage and hashing | ||
|
||
By default, passwords are stored and hashed with https://en.wikipedia.org/wiki/Bcrypt[bcrypt] under the | ||
https://en.wikipedia.org/wiki/Crypt_(C)[Modular Crypt Format] (MCF). | ||
|
||
To create a hashed password faster, use the `String BcryptUtil.bcryptHash(String password)` function, which defaults to creation of a random salt and hashing in ten iterations. | ||
It is possible to specify a desired amount of iterations and the salt used. | ||
|
||
[NOTE] | ||
==== | ||
* In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase. | ||
* 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 store them. | ||
==== | ||
|
||
You can also store passwords by using a different hashing algorithm, for example, `@Password(value = PasswordType.CUSTOM, provider = CustomPasswordProvider.class)`, as outlined in the following code snippet: | ||
|
||
[source,java] | ||
---- | ||
@UserDefinition | ||
@Table(name = "test_user") | ||
@Entity | ||
public class CustomPasswordUserEntity { | ||
@Id | ||
@GeneratedValue | ||
public Long id; | ||
@Column(name = "username") | ||
@Username | ||
public String name; | ||
@Column(name = "password") | ||
@Password(value = PasswordType.CUSTOM, provider = CustomPasswordProvider.class) | ||
public String pass; | ||
@Roles | ||
public String role; | ||
} | ||
public class CustomPasswordProvider implements PasswordProvider { | ||
@Override | ||
public Password getPassword(String pass) { | ||
byte[] digest = DatatypeConverter.parseHexBinary(pass); | ||
return SimpleDigestPassword.createRaw(SimpleDigestPassword.ALGORITHM_SIMPLE_DIGEST_SHA_256, digest); | ||
} | ||
} | ||
---- | ||
|
||
[WARNING] | ||
==== | ||
Never store passwords as a plain text for applications already running in production. | ||
However, it is possible to store passwords as a plain text with the `@Password(PasswordType.CLEAR)` annotation when operating in a test environment. | ||
==== | ||
|
||
|
||
|
||
|
||
|