Configure Authorities#
Supported Methods#
- In Spring Security the
authorities
of the user can be configured and validated using the following methods:
Method | Description |
---|---|
hasAuthority() | Accepts a single authority for which the endpoint will be configured and user will be validated against the single authority mentioned. Only users having the same authority configured can call the endpoint. |
hasAnyAuthority() | Accepts multiple authorities for which the endpoint will be configured and user will be validated against the authorities mentioned. Only users having any of the authority configured can call the endpoint. |
access() | Using Spring Expression Language (SpEL) it provides you unlimited possibilities for configuring authorities which are not possible which the above methods. We can use operators like OR, AND inside access() method. |
Example Configurations#
- So base on the spring security application that we used in the CSRF - Cross-Site Request Forgery configuration, we will continue configuring authority.
Database Tables#
- We will create the
authorities
table which will containauthorities
name such asREAD
,WRITE
,DELETE
. - 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 asREAD
,WRITE
andDELETE
. So this customer will have 3 authorities.
1 2 3 |
|
- Next, we will use the SQL statements below to create a new customer and only set one authority
READ
for it.
1 2 3 4 5 |
|
- 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 |
authorities table
:
id | authority | customer_id |
---|---|---|
6 | READ | 2 |
7 | WRITE | 2 |
8 | DELETE | 2 |
9 | READ | 3 |
Entity#
- So after updating database, we will also extend our
CustomerEntity
withAuthorityEntity
. So inCustomerEntity
we will have a set ofAuthorityEntity
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 |
|
- Then in we create the
AuthorityEntity
class as below:
AuthorityEntity.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 |
|
Edit Authentication Provider#
- Now, we are changing our database and Entities which are used for
Authentication
. In the Custom Authentication Provider, we create the list ofGrantedAuthority
as empty mean mean there are noAuthority
. Thus, we will update this code by getting the set ofAuthorities
from the 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 applyingAuthorities
. We will usehasAnyAuthority()
orhasAuthority()
method afterantMatchers()
methods. In details, everyantMatchers()
(api path pattern) will needAuthorities
which is matched as defined inhasAuthority()
orhasAnyAuthority()
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
DELETE
authority can access the/v1/card
api, then for/v1/user
any customer user which has one in 3 authoritiesREAD
,WRITE
,DELETE
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 withREAD
authorization to access, so the user with emailhan.do@example.com
can access this api because it has 3 authoritiesREAD
,WRITE
andDELETE
in database. The user with emailjohn.wick@example.com
can not access this api because it has onlyREAD
authority. - 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 3 authorities,READ
,WRITE
andDELETE
.