Example With Bcrypt In Spring Boot#
Example With Bcrypt In Spring Boot#
- In this example we will use 
Bcryptwhich is strong hashing function to encode the password.Bcryptis a password-hashing function designed byNiels ProvosandDavid Mazières, based on theBlowfish cipher 
Dependencies#
- In  Spring Boot we need to add the 
spring-securitydependency to do the Hashing withBcrypt. 
| pom.xml | |
|---|---|
1 2 3 4 5  |  | 
- So, why don't we use other dependencies to do the 
Bcrypthashing example?If you use the Maven Repository to search other
Bcryptdependencies then you can see they are out of update and contain manyvulnerabilities. So In this example, we should the one from thespring-securitywhich currently contains noVulnerabilities. 
Controller#
- Let's create an controller with some apis as below:
 
| BcryptController.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  |  | 
- We will need to create 2 simple models for request body as below. One is used for hashing data and the other one is used for checking raw data and hashed data.
 
| DataRequest.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  |  | 
| MatchDataRequest.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  |  | 
- Then we also need to create a model that loads environment variables into a spring bean using @ConfigurationProperties as below.
 
| HashConfigProperties.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  |  | 
| Bcrypt.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14  |  | 
Configuration#
- Now, let create a configuration class name 
PasswordEncoderConfigand but the code as below. 
| PasswordEncoderConfig.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25  |  | 
- In which, we will override the default 
passwordEncoderbean ofspring-securityby aBCryptPasswordEncoderwhich is initialized by astrengthand aSecureRandom. - We will configure the 
strengthin theapplication.ymlas below. 
| application.yml | |
|---|---|
1 2 3  |  | 
Bcrypt hashing so we will ignore the default spring-security configuration in the dependency spring-boot-starter-security. So we will add one more configuration as below to disable the default spring-security configuration.
| application.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10  |  | 
Service#
- Now let's create a service with name 
BcryptServicewith the code as below. In which, we will Inject thepasswordEncoderbean that we have just configured in the step above. Then we will useencodeandmatchesmethods that thePasswordEncoderprovided to encode and check the hashed data. 
| BcryptService.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  |  | 
Testing#
- Now, run the Spring Boot project and try to call api 
v1/cipher/hash/bcryptfor testing hasing data. Then you will receive the result as below 
- Then with the hash result above, we will use it to check with the original data by calling api 
v1/cipher/hash/bcrypt/check. Then you will see the original data and hashed data are matched. 
- Now let's try to change a single character in original data and check again with hashed data. Then you will see the api return failed.
 


