Configure Authorities#
Supported Methods#
- In Spring Security the
authoritiesof 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
authoritiestable which will containauthoritiesname such asREAD,WRITE,DELETE. - Next, we will extend the
customerstable which will link to theauthoritiestable. So every customer will have one or many authorities. - User the SQL statement below to create
authoritiestable.
1 2 3 4 5 6 7 8 9 10 | |
- Now for the current customer with email
han.do@example.comin thecustomerstable we will use the SQL statements below to set someauthoritiessuch asREAD,WRITEandDELETE. 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
READfor 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
CustomerEntitywithAuthorityEntity. So inCustomerEntitywe will have a set ofAuthorityEntitywith@OneToManyrelationship.
| 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
AuthorityEntityclass 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 ofGrantedAuthorityas empty mean mean there are noAuthority. Thus, we will update this code by getting the set ofAuthoritiesfrom the database throughCustomerEntityand map them toList<GrantedAuthority>. - The
CustomerAuthenticationProviderwill 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 needAuthoritieswhich 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
DELETEauthority can access the/v1/cardapi, then for/v1/userany customer user which has one in 3 authoritiesREAD,WRITE,DELETEcan access this api.
Testing#
- So, let's start our spring security application and call api
v1/loanfor testing, so this api will need the user withREADauthorization to access, so the user with emailhan.do@example.comcan access this api because it has 3 authoritiesREAD,WRITEandDELETEin database. The user with emailjohn.wick@example.comcan not access this api because it has onlyREADauthority. - Using email
han.do@example.comwe can accessv1/loansuccessfully.
- Then using
john.wick@example.comwe 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,WRITEandDELETE.



