Skip to content

ljubomirpaskali/spring-boot-oauth2-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-oauth2-jwt

Build Status

Table of Contents

Intro

This is a sample Spring Boot application for securing REST API with OAuth2 and JWT. It can be used as a quick start for your Spring Boot REST API project with fully functional security module.

Building blocks

Run the application

  • Build the application using Gradle: gradle clean build
  • Execute the resulting artifact (which can be found in build/libs) with: java -jar spring-boot-oauth2-jwt.jar

Test the application

In the sample application Resource Owner Password Credentials Grant flow is implemented meaning that both client and user credentials are required to obtain the access token. Please use next information to obtain the token:

  • client: client
  • secret: secret
  • username: user
  • password: user

Use the following command to generate the access token:

$ curl client:secret@localhost:8080/oauth/token -d grant_type=password -d username=user -d password=user

Access token response example:

{
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
    "token_type": "bearer",
    "expires_in": 599,
    "scope": "read write",
    "jti": "d36284c3-0efe-4565-b721-4202c3203f7d"
}

Use the generated token to access protected resources.

To find all users execute command:

curl http://localhost:8080/users -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

All users response example:

[
    {
        "id": 1,
        "name": "Ljubomir Paskali",
        "username": "user",
        "role": "USER_ROLE"
    },
    {
        "id": 2,
        "name": "John Doe",
        "username": "john.doe",
        "role": "USER_ROLE"
    }
]

To find a single user execute command:

curl http://localhost:8080/users/1 -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."

Single user response example:

{
    "id": 1,
    "name": "Ljubomir Paskali",
    "username": "user",
    "role": "USER_ROLE"
}

Contributions

Contributions are welcome. Please respect the Code of Conduct.

License

spring-boot-oauth2-jwt is licensed under the Apache License 2.0 License. See LICENSE for details.

About

Spring Boot REST API with OAuth2 & JWT sample application

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages