Securing REST Services With Spring Boot OAuth2

Spring Boot oauth2 example

In this tutorial, we will learn how to build an authorization server in Spring Boot OAuth2 to authenticate your identity and provide access_token which is used in requesting data from server.

This article will explain how to provide security for REST services in Spring Boot. I have explained this article in simple language and with illustrative examples :

What is OAuth 2.0

OAuth 2 is basically an authorization method used for security. It is used to provide access to the secured resources over the HTTP protocol.

OAuuth2 basically enables a third-party application which obtains limited access to an HTTP service :

Whether by allowing that third party application to obtain the access of service on its own behalf
Or on behalf of a resource owner by orchestrating an approval interaction between the resource owner and the HTTP service.

Now let’s understand the roles used in OAuth flow :


OAuth basically defines below mentioned four roles –

  • Resource Owner : This role is handled by the user of application.
  • Client : Client is the application which user is using and which requires access to user data on the resource server.
  • Resource Server : Resource server are used to store the http services and user data. It stores http services which can return the user’ data to the authenticated clients.
  • Authorization Server : Authorization server is responsible for authenticating the identity of user and also gives an authorization token. The received token is used by resource servers to basically validate your identity.
Spring Boot Oauth2
The flow of OAuth2

Access Token and Refresh Token

An Access token in OAuth2 technology is basically a string value. This string represents the authorization which is issued to the client.

The Tokens in OAuth2 represents some specific scopes and also the access duration. The scope and access durations are granted by the resource owner and Resource server and Authorization servers are responsible for enforcing them.

Refresh token in OAuth2 is issued with the access token to the client. Refresh Token is issued by the authorization server.

This token is basically used for obtaining a new access token in the case when the current access token expires or becomes invalid.

Refresh token can also be used to obtain some additional access tokens with either the identical scope or the narrower scope. Thus, The access tokens may have lesser permission and also a short lifetime compare to as authorized by the resource owner.

At the time of Authorization server discretion , It is optional to issue a refresh token.

Spring Boot OAuth2

The Responsibilities of Access token and Refresh token :

  • Access token is responsible to access data before it gets expired
  • Refresh token is responsible to request for a new access token when the existing access token is expired.

How Spring Boot OAuth2 Technology Works

Let’s understand How OAuth2 technology works with simple scenario based example. The scenario is to do a payment in the store by using credit card.

There are three entities involved :

  • The Store / Web Server.
  • The Bank / Authentication Server.
  • The Buyer / Client.

It is similar in OAuth2 protocol. Below are the steps performed :

The Buyer or Client requests the bank for a Credit Card.

The Bank will request and collects our information.

The Bank verifies the information and provides us a Credit Card if eligibility criteria matches.

In the OAuth2 protocol technology, Authentication Server is the one who grants us the card basis upon our verification.

If Bank gives us the Credit Card, we can go to the Store. In OAuth2 protocol Web Server acts as Store. We present the Card at Web Server.

The store can ask the bank for verification through card reader and also what is the limit of money withdrawal. The Store is the Resource Server here.

The store will then allow us to buy items but only for the permissible amount of money.

Similarly , In the OAuth2 protocol, the Web Server allows us to access pages, depending on our financial status.

Maven dependencies for Spring Security OAuth2

Below is our pom.xml file :

<project xmlns=""
        <relativePath /> 




Spring Boot OAuth2 – Authorization Server

In the below example, we are creating Spring Boot OAuth2 Authorization server.

We are creating the authorization server using the module of Spring Boot security module – OAuth. The annotation @EnableAuthorizationServer is used to create the authorization server and also we need to inherit the class AuthorizationServerConfigurerAdapter.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
public class OAuth2AuthorizationServer extends AuthorizationServerConfigurerAdapter 
    private BCryptPasswordEncoder passwordEncoder;
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            .authorizedGrantTypes("password", "authorization_code", "refresh_token")

Let’s understand the above example of Spring Boot OAuth2 Authorization server :

Spring Security OAuth module is exposing two endpoints for checking tokens which are /oauth/check_token and /oauth/token_key. These endpoints are by default protected.

The methods tokenKeyAccess() and checkTokenAccess() are basically used for opening these endpoints to use.

In the above example, ClientDetailsServiceConfigurer is basically used for defining an in-memory or the JDBC implementation of the client details service.

The in-memory implementation has following attributes :

clientIdIt is the Client Id. It is mandatory.
secretIt is the client secret which is required for trusted clients.
It is optional.
scopeIt defines the client’s scope.
If scope is empty or undefined then the client is not limited
by scope.
authorizedGrantTypesIt defines the Grant types which are authorized to use
for the client.
It’s default value is empty
authoritiesIt defines the authorities which are granted to the client.
These are regular Spring Security authorities.
redirectUrisIt redirects the user-agent to the client’s endpoint.
It must be an absolute URL.

Spring Boot OAuth2 – Resource Server

To create the Spring Boot OAuth2 resource server , we are using annotation @EnableResourceServer and we are also required to extend the class ResourceServerConfigurerAdapter .

import org.springframework.context.annotation.Configuration;
public class OAuth2ResourceServer extends ResourceServerConfigurerAdapter 
    public void configure(HttpSecurity http) throws Exception {

We can see that in above security class we have ensured that all the endpoints to be protected should start from /test only. For other endpoints, security is not required . It can be accessed without authentication.

We can also create resource servers which can authenticate users by themselves. In most of the cases, It will be a form based login. See below :

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    protected void configure(HttpSecurity http) throws Exception {
                .antMatchers("/oauth/authorize**", "/login**", "/error**")
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    public BCryptPasswordEncoder passwordEncoder(){ 
        return new BCryptPasswordEncoder(); 

We can see that above WebSecurityConfigurerAdapter class setup a form based login page. It opens up the all authorization urls with permitAll().

OAuth2 Secured REST resources

In below example of Spring Boot Rest services security using OAuth2 , we have created an API which is returning the logged in user’s name and email.

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
public class RestExample 
    public ResponseEntity<UserData> data() 
        //some sample data to return for testing
        User user = (User) 
        String email = user.getUsername() + "";
        UserData data = new UserData();
        return ResponseEntity.ok(data);
public class UserData
    private String name;
    private String email;
    //Setters and getters are omitted
    public String toString() {
        return "UserData [name=" + name + ", email=" + email + "]";

Testing / Demo

We have an API running in the URL http://localhost:8080/test/users/one.

We can access the API by directly putting username and password in login form but third party application cannot access the API as we do in browsers as they require oauth2 token.

Below steps are followed to test the OAuth 2 :

Get authorization grant code from user / client

As displayed in the diagram – “The flow of OAuth2”.

The very first step is ro get the authorization grant from resource owner.

We can get it from URL –  http://localhost:8080/oauth/authorize?client_id=clientapp&response_type=code&scope=read_profile_info

This URL will return a login page. As in the class – SecurityConfig, we have mentioned username and password as user1 and 1234 , we have to use these to login through the page.

OAuth2 login

After successful login, you will be redirected to the page of grant access. In this page, you can choose to grant access to third party application.

Spring Boot OAuth2

After clicking on Authorize, It will redirect the page to a URL like – http://localhost:8081/login?code=ylEpix. Where ‘ ylEpix ‘ is the Authorization code for the third party application.

Get access token from Authorization server

We have already received the authorization code in the previous step. Now the application will use that authorization grant to get the access token.

Now we are required to make the below request. Use the code which is obtained in the previous step.

Below is the data that we need to provide in the request –

 URL - http://localhost:8080/oauth/token

 Headers - 
 Content-Type: application/x-www-form-urlencoded
 authorization: Basic Y2xpZW50YXBwOjEyMzQ1Ng==

 Form data - application/x-www-form-urlencoded

 Form Parameters -


We are using Google’s REST APIs client to test . We are providing the above mentioned data. See below figures :

Spring Boot OAuth2
OAuth2 Testing

We have received below access token response from above request –

"access_token": "1223dfd4-2f87-4e64-ae8c-29ae499377eb",
"token_type": "bearer",
"refresh_token": "9c42ddf7-bdb6-415d-bf02-d083227276ae",
"expires_in": 119,
"scope": "read_profile_info"

Access the user data from resource server

Finally, we can access the user data from the resource server. Hit the following request :


Below is the Response received :

OAuth2 Results


In this article, we have learnt the Spring Boot OAuth2 technology and How to create the Resource and Authorization server with examples and How to achieve security of Rest Services in Spring Boot using OAuth2.

Leave a Reply

Your email address will not be published. Required fields are marked *