Open any IDE you love and create simple starter web project, say OAuth2Demo for instance
Then add below dependencies in pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-oauth2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-security</artifactId>
<dependency>
By now it should pull relevant jars needed to configure OAuth2, now open OAuth2DemoApplication.java which would have been created while creating starter web project and add @EnableAuthorizationServer annotation.
It should look like below.
package com.javator.oauth2demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
@SpringBootApplication
@EnableAuthorizationServer
public class OAuth2DemoApplication {
public static void main(String[] args) {
SpringApplication.run(OAuth2DemoApplication.class, args);
}
}
Rename application.properties file to application.yml for sake of simplicity/better code readability.
Now add below properties.
server:
port: 8282
spring:
security:
user:
name: root
password: admin
security:
oauth2:
client:
client-id: javator
client-secret: javator
access-token-validity-seconds: 600
authorized-grant-types:
- refresh_token
- authorization_code
- password
scope:
- READ
- WRITE
Above details should suffice to run basic authorization server.
To check it working, open postman and hit url http://localhost:8282/oauth/token
And it will says unauthorized, to make it work, open Authorization tab -> (TYPE) Basic Auth ->(Enter) Username : javator and password : javator
Basic Auth credentials should be same as client-id and client-secret in application.yml
Also, select body tab and enter below details
grant_type : password
username : root
password : admin
Now hit url again and it’s all set to see it in action, and should able to provide output as below
{
"access_token": "8b93ef49-5fbb-4fef-bdb3-d6105f6517f3",
"token_type": "bearer",
"refresh_token": "89112be6-fc88-4c43-b92f-7d885652d27c",
"expires_in": 587,
"scope": "READ WRITE"
}
Same can be downloaded from my github account