Skip to content

little-pinecone/spring-boot-swagger-ui-keycloak

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

spring-boot-swagger-ui-keycloak

keep_growing logo

About this project

This Spring Boot project shows an example configuration of Springdoc and Keycloak Spring Boot adapter that ensures that only authenticated users can call secured endpoints available through Swagger UI:

swagger ui with authorization code flow screenshot

  • client_id: spring-boot-example-app
  • client_secret should be left empty

Getting started

First, clone this repository.

Then, build it locally with:

mvn clean install

You can run the app in a command line with the following command:

mvn spring-boot:run

You can run the keycloak container with the following commands:

cd docker
docker-compose up -d

Running tests

You can run tests with:

mvn test

The MVC tests use Spring Boot Security not Keycloak. The HttpSecurity configuration stays the same.

Credentials

For the Keycloak admin

  • username: admin
  • password: admin

For example users of this app

  • available usernames: christina, hanna, carlo, noel
  • password: test
  • christina has the chief-operating-officer realm role that is required to call the POST: /api/products endpoint

The keycloak service starts with the default realm imported from the docker/keycloak/realms/realm-export.json file that specifies all the default users.

Visit API documentation

Make sure that the app is running.

Visit Keycloak

Make sure that the keycloak container is up.

Features

  • Application secured with Keycloak
  • Swagger UI available for everyone but only authenticated users can call secured endpoints
  • Only users with chief-operating-officer realm role can call the POST: /api/products endpoint (this role is assigned to christina by default)
  • OpenAPI 3 specification

Experimental Authorization configurations for Swagger UI

We can enable/disable specific application properties to run the app with different configurations.

Prior to springdoc-openapi v1.6.6, the Swagger configs for the OpenID Connect Discovery scheme, the Authorization Code and Password flows didn't work with Spring Boot csrf protection enabled for Springdoc. The issue was fixed in CSRF header should not be sent to cross domain sites PR and this project uses this fix.

Swagger UI with OpenID Connect Discovery scheme

To enable the Swagger Authentication config for the OpenID Connect Discovery scheme, edit the application.properties file so that it contains:

security.config.openid-discovery=true
security.config.authcode-flow=false

Alternatively, run the app with the following command:

mvn spring-boot:run -Dspring-boot.run.arguments="--security.config.openid-discovery=true --security.config.authcode-flow=false"

The result:

swagger ui with openid discovery screenshot

Swagger UI with Bearer Authentication

To enable the Swagger Authentication config for the Bearer Authentication, edit the application.properties file so that it contains:

security.config.bearer=true
security.config.authcode-flow=false

Alternatively, run the app with the following command:

mvn spring-boot:run -Dspring-boot.run.arguments="--security.config.bearer=true --security.config.authcode-flow=false"

The result:

swagger ui with bearer authentication screenshot

Make sure that the token contains realm roles (realm_access in the screenshot below):

token with realm roles screenshot

Otherwise, the POST endpoint will return 403 Forbidden (it requires the chief-operating-officer role).

Swagger UI with Resource Owner Password flow

To enable the Swagger Authentication config for the Password Flow, edit the application.properties file so that it contains:

security.config.password-flow=true
security.config.authcode-flow=false

Alternatively, run the app with the following command:

mvn spring-boot:run -Dspring-boot.run.arguments="--security.config.password-flow=true --security.config.authcode-flow=false"

The result:

swagger ui with password flow screenshot

Swagger UI with Implicit flow

This flow is deprecated1

To enable the Swagger Authentication config for the Implicit Flow, edit the application.properties file so that it contains:

security.config.implicit-flow=true
security.config.authcode-flow=false

Alternatively, run the app with the following command:

mvn spring-boot:run -Dspring-boot.run.arguments="--security.config.implicit-flow=true --security.config.authcode-flow=false"

The result:

swagger ui with keycloak auth for endpoints screenshot

Built With

Footnotes

  1. Why not Implicit flow in How to authorize requests via Postman

About

Demo Spring Boot app showing Swagger UI secured with Keycloak

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published