Configure Roles#
Supported Methods#
- In Spring Security the
roles
of the user can be configured and validated using the following methods.
Method | Description |
---|---|
hasRole() | Accepts a single role name for which the endpoint will be configured and the user will be validated against the single role mentioned. Only users having the same role configured can call the endpoint |
hasAnyRole() | Accepts multiple roles for which the endpoint will be configured and user will be validated against the roles mentioned. Only users having any of the role configured can call the endpoint |
access() | Using Spring Expression Language (SpEL) it provides you unlimited possibilities for configuring roles which are not possible with the above methods. We can use operators like OR, AND inside access() method |
ROLE_prefix
only to be used while configuring the role in database. But when we configure theroles
, we do it only by its name.access()
method can be used not only for configuringauthorization
based on authority or role but also with any special requirements. For example, we can confgiure access based on the country of the user or current time/date.
Example Configuration#
- Base on the spring security application that we used in the Authorities Configuration, we will continue configuring role.
Database Table#
- We will create the
roles
table which will containroles
name such asROLE_USER
,ROLE_ADMIN
. - Next, we will extend the
customers
table which will link to theauthorities
table. So every customer will have one or many authorities. - User the SQL statement below to create
authorities
table.
1 2 3 4 5 6 7 8 9 10 |
|
- Now for the current customer with email
han.do@example.com
in thecustomers
table we will use the SQL statements below to set someauthorities
such asROLE_USER
andROLE_ADMIN
. So this customer will have 2 roles.
1 2 |
|
- Next, we will use the SQL statements below to insert a
ROLE_USER
for the customer with emailjohn.wick@example.com
.
1 |
|
- So, after all our table will have data as below:
customers table
id | password | role | |
---|---|---|---|
1 | duc.nguyen@example.com | 12345 | admin |
2 | han.do@example.com | $2a$12$V.A53NkiPnA45W44aRYi2OLwUbbu08aDoY409/SKY/bT7cdF1PpLO | admin |
3 | john.wick@example.com | $2a$12$V.A53NkiPnA45W44aRYi2OLwUbbu08aDoY409/SKY/bT7cdF1PpLO | user |
roles table
id | role | customer_id |
---|---|---|
1 | ROLE_ADMIN | 2 |
3 | ROLE_USER | 2 |
2 | ROLE_USER | 3 |
Entity#
- So after updating database, we will also extend our
CustomerEntity
withRoleEntity
. So inCustomerEntity
we will have a set ofRoleEntity
with@OneToMany
relationship.
CustomerEntity.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 |
|
- Then in we create the
RoleEntity
class as below:
RoleEntity.java | |
---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Edit Authentication Provider#
- In the Configure Authorities, we create the list of
GrantedAuthority
fromAuthorities
which are loaded fromauthorities
table in the database. Thus, we will update this code by getting the set ofRoles
from the tableroles
in database throughCustomerEntity
and map them toList<GrantedAuthority>
. - The
CustomerAuthenticationProvider
will be changed as below:
CustomerAuthenticationProvider.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 61 |
|
Authorization Configuration#
- In the
ProjectSecurityConfig
, we will updateconfigure()
method for applyingRoles
. We will usehasAnyRole()
orhasRole()
method afterantMatchers()
methods. In details, everyantMatchers()
(api path pattern) will needRoles
which is matched as defined inhasRole()
orhasAnyRole()
to access. - Our configurations will look like 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 56 57 58 59 60 61 62 63 64 |
|
- So now, only customer user with
ROLE_ADMIN
role can access the/v1/loan
api, then for/v1/user
any customer user which has one in 2 rolesROLE_ADMIN
,ROLE_USER
, can access this api.
Testing#
- So, let's start our spring security application and call api
v1/loan
for testing, so this api will need the user withROLE_ADMIN
authorization to access, so the user with emailhan.do@example.com
can access this api because it has 2 rolesROLE_ADMIN
andROLE_USER
in database. The user with emailjohn.wick@example.com
can not access this api because it has onlyROLE_READ
role. - Using email
han.do@example.com
we can accessv1/loan
successfully.
- Then using
john.wick@example.com
we will get the error code 403 forbidden.
- Now, let's try to call api
/v1/user
, we expect that 2 users above can access this api because this api requires one of 2 roles,ROLE_ADMIN
,ROLE_USER
.