Understanding User Management interfaces and Classes#
UserDetails Hierarchy#
- So
UserDetailsis an interface which provide core user information. This interface will provide some abstract methods likegetAuthorities,getPassword,getUsernameand so on, they are core user information that an simple application have to provide. In spring security we have an sample implementation of thisUserDetailsinterface calledUser. For example, we don't have a very big application which need to be create our own user schema, so we can go with the sampleUserimplementation of spring security.
- So, the
Userclass implementUserDetailsand if you look into theUserclass you can see there are some attributes as below:
| User.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
- So if you have to create your custom user implementation from
UserDetailsplease make sure that put the same naming conventions and name variables as above, because these are things that understand by spring security framework. - Next, we have another interface and extend the
UserDetailscalledMutableUserDetails. This interface provide one more methodsetPasswordand the classMutableUseris the implementation of it. - So with the
Userclass don't have any method to set the password (the password is only created in the constructor ofUser), this is inconvenient when the user forgot the password, or want to change it. So theMutableUserwill help you to solve it by provide the methodsetPassword. As you can see in some websites when you click into a link forgot password and put the email into it, then you will received an email with a link to set the new password and retype it. So this an feature ofMutableUser.
UserDetailsService Hierarchy#
- Let's take a look into the
UserDetailsService hierarchythen you will understand how end users will be managed by spring security. - Below are interfaces and classes revolving around user management inside spring security.
UserDetailsServiceis an interface, which have a single abstract method calledloadUserByusername. Ideally, most of the applications they just wanted to validate whether the given user is present inside a database or LDAP application or inside memory of my spring security container. So in all that scenarios, we just pass username to this abstract method that we are receiving from the UI and this method we have in logic of fetching theUserdetailsfrom the database or LDAP server or from the memory of the spring container.- So
UserDetailsServiceis an interface which have a logic of fetching the user from the database or any other places and the return type ofloadUserByusernameisUserdetails, because whenever we are dealing with users, we decided to useUserdetailsschema adhering to the spring security. - Then we have
UserDetailsManagerwhich will extend theUserDetailsServicewith abstract methods for user management likecreateUser,updateUser,deleteUser,changePasswordanduserExists.
| UserDetailsManager.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 | |
- By default, spring security provides 2 implementations of
UserDetailsManager. They areInMemoryUserDetailsManagerandJdbcUserDetailsManager.InMemoryUserDetailsManager: so if you want to some proof of concept or building some sample applications to demo someone, you can useInMemoryUserDetailsManager. That means you can maintain the users, you can load the user details, authentication details, authorities, everything from the memory itself, which will store and set the spring container. See Example: Configure Users With inMemoryAuthentication, Configure Users With inMemoryUserDetailsManager.JdbcUserDetailsManager: this is the most famous implementation of spring security and this is a production grade of implementation That means if you provide data source details of MySQL or Oracle or any database, thisJdbcUserDetailsManagercontain all the code related to loading theUserDetails, maintaining them, creating them, deleting them, changing passwords.
InMemoryUserDetailsManager#
- If you check
InMemoryUserDetailsManageryou will see there is a propertyuserswhich is a HashMap ofusernameandMutableUserDetails. So basically, when you create an user usingInMemoryUserDetailsManager, your user will be type ofMutableUserDetailsand it will saved into a HashMap.
| InMemoryUserDetailsManager.java | |
|---|---|
1 2 3 4 5 6 7 | |
- So if you look into the
InMemoryUserDetailsManagerclass, you will see it implement 2 interfaces, one is theUserDetailsManagerwhich is the main interface for managingUserDetailsand the second one is theUserDetailsPasswordServicethat provide the methodupdatePasswordfor theUserDetails.
JdbcUserDetailsManager#
- If you check the
JdbcUserDetailsManageryou will see it implementsUserDetailsManagerand another interface calledGroupManager. Because in production mode, many user can be added into a group (Ex: admin group). Then in theGroupManageryou will see it contains some methods for group management.
| JdbcUserDetailsManager.java | |
|---|---|
1 2 3 | |
| GroupManager.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 57 58 59 60 | |
- Finally, the
JdbcUserDetailsManagerwill be the one that implement all methods provided for managing user inUserDetailsand group inGroupManager.
See Also#
- Configure Users With inMemoryAuthentication
- Configure Users With inMemoryUserDetailsManager
- Defining And Managing Users
- Configure Users With JdbcUserDetailsManager

