Configure Roles#
Supported Methods#
- In Spring Security the 
rolesof 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_prefixonly 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 configuringauthorizationbased 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 
rolestable which will containrolesname such asROLE_USER,ROLE_ADMIN. - 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 asROLE_USERandROLE_ADMIN. So this customer will have 2 roles. 
1 2  |  | 
- Next, we will use the SQL statements below to insert  a 
ROLE_USERfor 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 
CustomerEntitywithRoleEntity. So inCustomerEntitywe will have a set ofRoleEntitywith@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 26 27 28  |  | 
- Then in we create the 
RoleEntityclass 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 
GrantedAuthorityfromAuthoritieswhich are loaded fromauthoritiestable in the database. Thus, we will update this code by getting the set ofRolesfrom the tablerolesin 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 applyingRoles. We will usehasAnyRole()orhasRole()method afterantMatchers()methods. In details, everyantMatchers()(api path pattern) will needRoleswhich 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_ADMINrole can access the/v1/loanapi, then for/v1/userany 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/loanfor testing, so this api will need the user withROLE_ADMINauthorization to access, so the user with emailhan.do@example.comcan access this api because it has 2 rolesROLE_ADMINandROLE_USERin database. The user with emailjohn.wick@example.comcan not access this api because it has onlyROLE_READrole. - 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 2 roles,ROLE_ADMIN,ROLE_USER. 



