Custom UserDetailsService Of Spring Security#
Create Your Own Custom Implementation of UserDetailsService#
- In this section we will create our custom implementation of UserDetailsService. So, why do we need to create a custom implementation of
UserDetailsServiceof spring security? - The answer is that when you already have an database with users and authorities and you just want to migrate your existed users in the database into the spring security for using but your existing table is so different with the default implementation of spring security such as
JdbcUserDetailsManager(See Configure Users With JdbcUserDetailsManager) . So we need to create your own customUserDetailsServiceimplementation
Dependencies#
- We will use Spring Data JPA to integrate with the
customerstable that we defined in the database. So you will need to add some dependencies as below.
| pom.xml | |
|---|---|
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 | |
Prepare Tables In Database#
- Now let's use the script below to create your own table in the database for storing customer users. You should note that the column's names and table's name don't need to have the same name as in the default implementation in spring security, you can put any name you want for table and column names here.
1 2 3 4 5 6 7 8 9 10 11 | |
Create Entity Class#
- Now let's create the
CustomerEntityclass which will be mapped with tablecustomersin your database as below
| CustomerEntity.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Create CustomerRepository#
- Next, we will create the
CustomerRepositoryinterface for ourCustomerEntityabove with a method for getting the CustomerEntities by email.
| CustomerRepository.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Create Your Custom Implementation of UserDetails#
- Now, we will create a class name
SecurityCustomerfor our customUserDetailsimplementation. This class will have 1 attribute which is theCustomerEntitywhich we will load from our database. Because in our database we don't have column forAccountNonExpired,AccountNonLocked,CredentialsNonExpiredandEnabledso we will set them as true.
| SecurityCustomer.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 56 | |
Create Your Custom Implementation Of UserDetailsService#
- We will crate a class name
BankUserDetailsServicewhich will implement theUserDetailsServicefor our customUserDetailsServiceimplementation. So we will@Overridethe methodloadUserByUsernameofUserDetailsService
| BankUserDetailsService.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 | |
- We will use
CustomerRepositoryto load our CustomerEntity from database then we will use this CustomerEntity to create theSecurityCustomerwhich is the implementation ofUserDetails. - So we have done custom the
UserDetailsService.
Configure Spring Security#
- Now, let's open the class
ProjectSecurityConfigand comment the configuration related toJdbcUserDetailsManageras 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 | |
Enable JPA Repositories#
- Go to the main class and add the annotation
@EnableJpaRepositoriesas below
| CustomDefaultSpringSecurityApplication.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | |
Configure DataSource#
- Now, let add the datasource configuration in the
application.yml. Then start the application up to test.application.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#spring: # security: # user: # name: user # password: 12345 # #server: # servlet: # session: # timeout: 1m spring: datasource: url: jdbc:mysql://localhost:3306/worldbank?useUnicode=true&characterEncoding=UTF-8 username: root password: password
Testing#
- Now let's start your spring boot application and try to use the email/password that existed in the database for authentication and you will see the successful result as below.
See Also#
- Configure Users With JdbcUserDetailsManager
- Defining And Managing Users
- Understanding User Management interfaces and Classes



