Many online web platforms require users to enter a strong password during registration. This strategy helps reduce the vulnerability of user data to any hacking.
In this article, we’ll create a simple form with a registration page. Before continuing with this tutorial, you should have a basic understanding of Java with the Spring framework.
Passay is a Java-based password generation and validation library. It builds on the success of vt-password
and provides a comprehensive and extensible feature set.
Use Spring Initializr to generate the spring boot 2 project with the dependencies: web, lombok, spring-boot-starter-validation.
Then add the Passay dependency to manage validation policies.
XML
<dependency>
<groupId>org.passay</groupId>
<artifactId>passay</artifactId>
<version>1.6.0</version>
</dependency>
You can find all versions here.
Use the UserData
class containing the information to verify.
Java
@PasswordValueMatch.List({
@PasswordValueMatch(
field = "password",
fieldMatch = "confirmPassword",
message = "Passwords do not match!"
)
})
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString
public class UserData {
@NonNull
@NotBlank(message = "username is mandatory")
private String username;
@NotNull
@NotEmpty
@Email
private String email;
@ValidPassword
@NonNull
@NotBlank(message = "New password is mandatory")
private String password;
@ValidPassword
@NonNull
@NotBlank(message = "Confirm Password is mandatory")
private String confirmPassword;
}
Two important annotations:
@PasswordValueMatch
: Check if the password and confirmation password match.@ValidPassword
: Contains the password validation policy.#java #spring boot 2.2 #angular 9 #validation policy