DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Authentication With Remote LDAP Server in Spring WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Spring Security Oauth2: Google Login

Trending

  • The Death of "Text-Only" ChatOps: Why Google's A2UI Matters for DevOps and SRE
  • How to Prevent Data Loss in C#
  • Why Your QA Engineer Should Be the Most Stubborn Person on the Team
  • How to Build and Optimize AI Models for Real-World Applications
  1. DZone
  2. Coding
  3. Frameworks
  4. Implementing Your Own Spring Boot Oauth2 Authorization Server

Implementing Your Own Spring Boot Oauth2 Authorization Server

In this article, we will be talking about implementing your own authorization server in a Spring Boot application, and we will also test a few types of grant types.

By 
Amit Mishra user avatar
Amit Mishra
·
Dec. 02, 20 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
23.3K Views

Join the DZone community and get the full member experience.

Join For Free

Before starting this post, please make sure that you have a conceptual understanding of how OAuth works and its terminologies. If you don't, you may want to check out this comprehensive explanation.

In the modern world, we have seen major IT organizations have their own OAuth implementation and have their own OAuth servers. If we look around, we will find that most of the applications are now powered by OAuth and guarantee your account security to a great extent. In Spring Boot applications, spring team has given support for spring security, which of course is a great way of securing your applications. But to leverage the features of single sign on (SSO) and social login, you may want to implement your own OAuth server in your Spring Boot application. The Spring team has also implemented OAuth specifications and given support to implement your standalone authorization server.

Spring Security OAuth is a separate project, and when developers started using this a lot, the Spring team decided to rewrite the whole Spring security and OAuth together, so currently, OAuth2 is in maintenance mode, and the Spring team has released resource server and client server in a single Spring security project with Spring security 5 release. The OAuth2 authorization server is in currently experimental mode and you can check this out here.

In this article, we will implement the Oauth2 authorization server, which is currently in maintenance mode. 

Getting Started

To use your own authorization server, please go ahead and create a new spring starter project and make sure your pom is similar to the one that I have.

XML
 




x


 
1
<dependencies>
2
        <dependency>
3
            <groupId>org.springframework.boot</groupId>
4
            <artifactId>spring-boot-starter-web</artifactId>
5
        </dependency>
6
        <dependency>
7
            <groupId>org.springframework.security</groupId>
8
            <artifactId>spring-security-jwt</artifactId>
9
            <version>1.1.1.RELEASE</version>
10
        </dependency>
11
        <dependency>
12
            <groupId>org.springframework.security.oauth.boot</groupId>
13
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
14
            <version>2.4.0</version>
15
        </dependency>
16
        <dependency>
17
            <groupId>org.springframework.boot</groupId>
18
            <artifactId>spring-boot-starter-test</artifactId>
19
            <scope>test</scope>
20
        </dependency>
21
    </dependencies>



Enable Authorization Support

Open your main application class and add @EnableAuthorizationServer to enable the support for Spring authorization server. 

Java
 




xxxxxxxxxx
1


 
1
@EnableAuthorizationServer
2
@SpringBootApplication
3
public class SpringAuthorizationServerApplication {
4

          
5
    public static void main(String[] args) {
6
        SpringApplication.run(SpringAuthorizationServerApplication.class, args);
7
    }
8

          
9
}



By default, adding @EnableAuthorizationServer enables the client credentials grant type as per OAuth specifications for your spring boot application. As discussed in this post.

Adding ClientId and Client Secret

YAML
 




xxxxxxxxxx
1


 
1
security:
2
  oauth2:
3
    client:
4
      client-id: clientId
5
      client-secret: very-strong-secret



Now, run your application and you can make a postman request like this.

localhost:8080/oauth/token?grant_type=client_credentials&scope=any

Please make sure you've added your clientId and client secret in the basic auth header of the authorization tab in postman and you get a successful response like this.

JSON
 




xxxxxxxxxx
1


 
1
{
2
    "access_token": "qbE0ipKzzX5FNj3OVe8LWu40T_s",
3
    "token_type": "bearer",
4
    "expires_in": 43199,
5
    "scope": "any"
6
}



Now, you can use this access token to access the protected resource. Thank you so much for giving your time to read out this article. I will be posting more articles on implementing different grant types in OAuth. This project is also available on GitHub, please click on this link.

Please note that, spring doesn't recommend to use this auth server anymore. I posted this just because I wanted to give you a picture of working with oauth in spring. Spring team as rewrote the whole security module and introduced spring security 5 with resource and client server support. They are working on authorization server but it's in experimental and once it's officially available, we will surely be posting about that.

Spring Framework Spring Security authentication Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Authentication With Remote LDAP Server in Spring WebFlux
  • Authentication With Remote LDAP Server in Spring Web MVC
  • How to Implement Two-Factor Authentication in A Spring Boot OAuth Server? Part 2: Under the Hood
  • Spring Security Oauth2: Google Login

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook