Configure Users With inMemoryUserDetailsManager#
Configure Users With inMemoryUserDetailsManager#
- As in Configure Users With inMemoryAuthentication, we knew how to create users by
inMemoryAuthentication
. Now, we will deep dive a little bit by creating users byinMemoryUserDetailsManager
which by default provided by the spring security when we want to store user details inside memory of our application. - By default we have
inMemoryUserDetailsManager
is an implementation ofUserDetailsManager
which extended fromUserDetailsService
. So we have to ensure that we have to build theUserDetails
that we want to use for our application.
UserDetailsService.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 |
|
UserDetailsManager.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
InMemoryUserDetailsManager.java | |
---|---|
1 2 3 4 5 |
|
- So let's comment out the old configuration of
inMemoryAuthentication
in Configure Users With inMemoryAuthentication and add new configuration as below
ProjectSecurityConfig.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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
|
- As you can see, we will create a new
InMemoryUserDetailsManager
then we create newUserDetails
with username, password and authorities and set it intoInMemoryUserDetailsManager
. Finally, we set theInMemoryUserDetailsManager
intoAuthenticationManagerBuilder
. It's mean we has just provided the customUserDetails Service
(InMemoryUserDetailsManager) forAuthenticationManagerBuilder
. - So the step for
UserDetails Service
byInMemoryUserDetailsManager
has done. Then we will need to configure the defaultPasswordEncoder
. So we will create a bean for thePasswordEncoder
as you can see in the code so the Spring Security will load it as the defaultPasswordEncoder
. - Now we have
InMemoryUserDetailsManager
andPasswordEncoder
so, let's start the application and call api for checking. You should see the result as the image below.