Level: Beginner Technologies: SpringBoot Summary: SpringBoot REST Service Target Product: Red Hat SSO Source: https://github.com/redhat-developer/redhat-sso-quickstarts
The service-springboot-rest
quickstart demonstrates how to write a RESTful service with SpringBoot that is secured with Red Hat SSO.
There are 2 endpoints exposed by the service:
public
- requires no authenticationproducts
- can be invoked by users with theuser
role and returns a list of products
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 Red Hat SSO and download the installation file.
The following steps shows how to create the client required for this quickstart:
- Open the Red Hat SSO admin console
- Select
Clients
from the menu - Click
Create
- Add the following values:
- Client ID: You choose (for example
service-springboot
) - Client Protocol:
openid-connect
- Client ID: You choose (for example
- Click
Save
Once saved you need to change the Access Type
to bearer-only
and click save.
Finally you need to update the adapter using SpringBoot's configuration file (application.properties). If you followed the steps above it should be:
server.compression.enabled: true
server.compression.min-response-size: 1
server.connection-timeout=5000
server.port = 8081
keycloak.realm=springboot-quickstart
keycloak.auth-server-url=http://localhost:8180/auth
keycloak.ssl-required=external
keycloak.resource=service-springboot
keycloak.public-client=true
keycloak.bearer-only=true
keycloak.securityConstraints[0].securityCollections[0].name = protected resource
keycloak.securityConstraints[0].authRoles[0] = user
keycloak.securityConstraints[0].securityCollections[0].patterns[0] = /products
-
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
The endpoints for the service are:
- public - http://localhost:8081/public
- secured - http://localhost:8081/products
You can open the public endpoint directly in the browser to test the service. The other endpoint require invoking with a bearer token. To invoke these endpoints use one of the example quickstarts:
- app-springboot - SpringBoot application that invokes the example service. Requires service example to be deployed.
- app-jee-html5 - HTML5 application that invokes the example service. Requires service example to be deployed.
- app-jee-jsp - JSP application packaged that invokes the example service. Requires service example to be deployed.
- Make sure you have an Red Hat SSO 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. - Run
mvn test -Pspring-boot