Spring Boot OAUTH2 Role-Based Authorization
Spring Boot OAUTH2 Role-Based Authorization
In this article, we will be securing REST APIs with role based OAUTH2 implementation. To do so, we will be creating two custom roles as ADMIN and USER and we will use @secured annotation provided by spring security to secure our controller methods based on role. To some of the endpoints, we will provide access to ADMIN role and others will be accesible to user having ADMIN and USER role. All the user details, credentials and associated roles will be saved into MySQL DB and we will be using spring data to perform our DB operations. We will use spring boot to take care of our most of the configurations.
For a 3rd party authorization server such as Google, you can visit this - Spring Boot OAuth2 with Google.
We will be using Postman to perform all of our CRUD operation and test all the APIs. You can visit my another article for an angular implementation with spring security and OAUTH2. Also, we will be using JwtTokenStore to translate access tokens to and from authentications. For an InMemoryTokenStore ou can visit here - here.
Technologies Used
Maven
Spring Boot 2.1.1.RELEASE
OAUTH 2.1.0.RELEASE
MySQL
JWT
Intellij
Project Structure
Head over to start.spring.io and download a sample spring boot app. Below is the snapshot of mine.
In this article, we will not be discussing much about the basics of OAUTH2 as we have discussed alot in our previous articles. For a complete list of articles on spring security, you can visit here - Spring Security Tutorials
Authorization Server Configuration in OAUTH2
Below is the implementation of our authorization server configuration that is responsible for generating authorization tokens. We have configuration of JWT token store along with the common code of OAUTH2 protocol to configure client id, client-secret and grant types. AuthorizationServerConfig.java
Resource Server Configuration
Resource in our context is the REST API which we have exposed for the crud operation. To access these resources, the client must be authenticated. We have not made any configurations here to make our endpoints secured as we will using method level security with annotation @Secured.
There is no exception handling done in resource server config. You can visit my another article for exception handling in spring security ResourceServerConfig.java
Now, to bootstrap the authorization server and resource server configuration, we have WebSecurityConfiguration. The parameter securedEnabled = true enables support for annotation @Secured. We will be using Bcypt password encoder for password encryption.
Controller Implementation
Below is the controller to expose the REST APIs for CRUD operations. Any user having a role USER can access the method getUser() and rest of the APIs is only accessible to user having ADMIN role. UserController.java
Service Implementation
The service implementation has common implementation for CRUD operation. The method loadUserByUsername() is important here which is an overriden method. This method is responsible for validating user and it's roles from DB.
Now, let us define our model classes. Below is our User and Role class. User.java
Role.java
UserDto.java
ApiResponse.java
application.properties
Below is the default script that can be executed. The username/password is admin/admin
Testing the Application
First of all let us generate admin token.
Now, let us create user. Remember, only ADMIN role has access to create user. Below is the Postman screenshot.
Now you can generate token for user using above defined POST method. Below is the screenshot.
Now we can access getUser() endpoint to fetch user with access token having USER role
Last updated
Was this helpful?