Level: Beginner Technologies: SpringBoot, Spring MVC, Spring Security Summary: SpringBoot, Spring MVC, Spring Security Target Product: Keycloak Source: https://github.com/keycloak/keycloak-quickstarts
The app-springboot
quickstart demonstrates how to write a SpringBoot application that is secured with Keycloak.
This application contains 2 pages :
- A public landing page.
- A secured product page.
This application also show how to combine Spring Security with Keycloak.
The quickstart requires that you have the SpringBoot Service running. It assumes the
services are located at http://localhost:8081/products
. If the services are running elsewhere you need to edit
resources/application.properties
and replace the value of product.service.url
.
All you need to build this project is Java 8.0 (Java SDK 1.8) or later and Maven 3.1.1 or later.
Prior to running the quickstart you need to create a client in Keycloak and download the installation file.
The following steps shows how to create the client required for this quickstart:
- Open the Keycloak admin console
- Select
Clients
from the menu - Click
Create
- Add the following values:
- Client ID: You choose (for example
app-springboot
) - Client Protocol:
openid-connect
- Root URL: URL to the application (for example
http://localhost:8080/app-springboot
)
- Client ID: You choose (for example
- Click
Save
Once saved you need to change the Access Type
to public
and click save.
As an alternative you can create the client by importing the file client-import.json.
If you deploy the application somewhere else change the hostname and port of the URLs accordingly.
Finally you need to update the adapter using SpringBoot's configuration file (application.properties) :
server.compression.enabled: true
server.compression.min-response-size: 1
server.connection-timeout=5000
server.port = 8080
keycloak.realm=springboot-quickstart
keycloak.auth-server-url=http://localhost:8180/auth
keycloak.ssl-required=external
keycloak.resource=app-springboot
keycloak.public-client=true
-
Open a terminal and navigate to the root directory of this quickstart.
-
The following shows the command to deploy the quickstart:
mvn spring-boot:run
You can access the application with the following URL: http://localhost:8080/.
- Make sure you have an Keycloak server running with an admin user in the
master
realm or use the provided docker image - Be sure to set the
TestHelper.keycloakBaseUrl
in thecreateArchive
method (default URL is localhost:8180/auth). - Set accordingly the correct url for the
keycloak.auth-server-url
in the test application.properties. - Maker sure the service-springboot-rest is running
- Run
mvn test -Pspring-boot