Understanding User Management interfaces and Classes#
UserDetails Hierarchy#
- So
UserDetails
is an interface which provide core user information. This interface will provide some abstract methods likegetAuthorities
,getPassword
,getUsername
and so on, they are core user information that an simple application have to provide. In spring security we have an sample implementation of thisUserDetails
interface 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 sampleUser
implementation of spring security.
- So, the
User
class implementUserDetails
and if you look into theUser
class 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
UserDetails
please 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
UserDetails
calledMutableUserDetails
. This interface provide one more methodsetPassword
and the classMutableUser
is the implementation of it. - So with the
User
class 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 theMutableUser
will 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 hierarchy
then you will understand how end users will be managed by spring security. - Below are interfaces and classes revolving around user management inside spring security.
UserDetailsService
is 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 theUserdetails
from the database or LDAP server or from the memory of the spring container.- So
UserDetailsService
is an interface which have a logic of fetching the user from the database or any other places and the return type ofloadUserByusername
isUserdetails
, because whenever we are dealing with users, we decided to useUserdetails
schema adhering to the spring security. - Then we have
UserDetailsManager
which will extend theUserDetailsService
with abstract methods for user management likecreateUser
,updateUser
,deleteUser
,changePassword
anduserExists
.
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 areInMemoryUserDetailsManager
andJdbcUserDetailsManager
.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, thisJdbcUserDetailsManager
contain all the code related to loading theUserDetails
, maintaining them, creating them, deleting them, changing passwords.
InMemoryUserDetailsManager#
- If you check
InMemoryUserDetailsManager
you will see there is a propertyusers
which is a HashMap ofusername
andMutableUserDetails
. So basically, when you create an user usingInMemoryUserDetailsManager
, your user will be type ofMutableUserDetails
and it will saved into a HashMap.
InMemoryUserDetailsManager.java | |
---|---|
1 2 3 4 5 6 7 |
|
- So if you look into the
InMemoryUserDetailsManager
class, you will see it implement 2 interfaces, one is theUserDetailsManager
which is the main interface for managingUserDetails
and the second one is theUserDetailsPasswordService
that provide the methodupdatePassword
for theUserDetails
.
JdbcUserDetailsManager#
- If you check the
JdbcUserDetailsManager
you will see it implementsUserDetailsManager
and another interface calledGroupManager
. Because in production mode, many user can be added into a group (Ex: admin group). Then in theGroupManager
you 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
JdbcUserDetailsManager
will be the one that implement all methods provided for managing user inUserDetails
and group inGroupManager
.
See Also#
- Configure Users With inMemoryAuthentication
- Configure Users With inMemoryUserDetailsManager
- Defining And Managing Users
- Configure Users With JdbcUserDetailsManager