Example With Bcrypt In Spring Boot#
Example With Bcrypt In Spring Boot#
- In this example we will use
Bcrypt
which is strong hashing function to encode the password.Bcrypt
is a password-hashing function designed byNiels Provos
andDavid Mazières
, based on theBlowfish cipher
Dependencies#
- In Spring Boot we need to add the
spring-security
dependency to do the Hashing withBcrypt
.
pom.xml | |
---|---|
1 2 3 4 5 |
|
- So, why don't we use other dependencies to do the
Bcrypt
hashing example?If you use the Maven Repository to search other
Bcrypt
dependencies then you can see they are out of update and contain manyvulnerabilities
. So In this example, we should the one from thespring-security
which 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
PasswordEncoderConfig
and 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
passwordEncoder
bean ofspring-security
by aBCryptPasswordEncoder
which is initialized by astrength
and aSecureRandom
. - We will configure the
strength
in theapplication.yml
as 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
BcryptService
with the code as below. In which, we will Inject thepasswordEncoder
bean that we have just configured in the step above. Then we will useencode
andmatches
methods that thePasswordEncoder
provided 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/bcrypt
for 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.