The following was discovered as part of building this project:
- The original package name 'com.examples.resource-server' is invalid and this project uses 'com.examples.resource_server' instead.
The purpose of this project is to illustrate how to create a reactive resource server microservice, using Keycloak as the authentication server. This project demonstrates the integration of Spring WebFlux for reactive programming and Keycloak for securing the microservice.
To run Keycloak using Docker, you can use the following command:
docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin jboss/keycloak
This command will start a Keycloak server on port 8080 with the username admin and password admin.
- Open your browser and navigate to
http://localhost:8080. - Log in to the Keycloak admin console using the username
adminand passwordadmin. - Create a new realm or select an existing realm.
- Navigate to the "Clients" section and click on "Create".
- Enter
demo-serviceas the client ID. - Set the client protocol to
openid-connect. - Set the access type to
confidential. - Save the client.
- In the "Credentials" tab, note the client secret. You will need this for your application configuration.
- In the "Settings" tab, set the "Valid Redirect URIs" to
http://localhost:8081/*(or the appropriate URL for your application). - In the "Roles" tab, add any roles that your application requires (e.g.,
admin,user). - In the "Mappers" tab, add a mapper to include roles in the access token:
- Click "Create".
- Enter a name (e.g.,
roles). - Set the "Mapper Type" to
User Realm Role. - Set the "Token Claim Name" to
roles. - Set the "Claim JSON Type" to
String. - Save the mapper.
To obtain an access token from Keycloak, follow these steps:
- Open Postman.
- Create a new POST request.
- Set the request URL to
http://localhost:8080/realms/{realm-name}/protocol/openid-connect/token.- Replace
{realm-name}with the name of your realm.
- Replace
- In the "Authorization" tab, select "Basic Auth" and enter the client ID and client secret.
- Username:
demo-service - Password: [client secret from Keycloak]
- Username:
- In the "Body" tab, select "x-www-form-urlencoded" and add the following parameters:
grant_type:passwordusername: [your Keycloak username]password: [your Keycloak password]
- Click "Send" to obtain the access token.
To test the endpoints using Postman, follow these steps:
- Open Postman.
- Create a new GET request.
- Set the request URL to
http://localhost:8081/api/sample-entities(or the appropriate endpoint URL). - In the "Authorization" tab, select "Bearer Token" and enter the access token obtained from Keycloak.
- Click "Send" to test the endpoint.
For further reference, please consider the following sections:
- Official Gradle documentation
- Spring Boot Gradle Plugin Reference Guide
- Create an OCI image
- Spring Reactive Web
The following guides illustrate how to use some features concretely:
These additional references should also help you: