Use Docker and Keycloak to specify the access token and execute the API

table of contents

--Start Keycloak from Docker --How to change the port --Keycloak client app and user account registration --Creating a REST API for Spring Boot --Resource server settings: Authorization server settings --Authentication access settings --API execution by specifying access token --About control by scope

Reference site

-OpenID Connect with Keycloak and Spring Boot/Security (Resource Server)

Launch Keycloak from Docker

Since Docker and Keycloak are the fastest OpenID Connect in the world, we will execute Keycloak from Docker. Reference site: https://www.keycloak.org/getting-started/getting-started-docker

docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:12.0.1

--After changing the port, it will be as follows.

docker run -p 8088:8088 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:12.0.1

How to change the port

Change the port number to 8088 because it will be a problem if you get the port number with other apps.

docker run -p 8088:8088 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:12.0.1 -Djboss.http.port=8088

Keycloak client app and user account registration

--Check the operation of Keycloak with this Link and register the client application and user account.

Creating a REST API that introduces Spring Security into your Spring Boot project

Create a REST API according to this Link.

Up to this point, you will need to log in to execute the API. Therefore, as a subsequent procedure, enable API execution using an access token without logging in.

Resource server settings: Authorization server settings

--Set the authorization server information in application.properties of the project created above.

application.properties

#Authorization server Issuer Identifier
spring.security.oauth2.resourceserver.jwt.issuer-uri= http://localhost:8088/auth/realms/realm1

--When you execute the API, it becomes [401 = Unauthorized]. This state is normal operation. image.png

Authentication access settings

Register the app in Keycloak, get an access token, and use it to try authenticated access.

① Add a client application with Keycloak.

image.png

--Set the following and save.
Access Type=Confidential
Authorization Enabled=ON
image.png

(2) Specify the access token and execute the API

② -1) Acquisition of Client Secrets Copy [Secret] of [Credentials] of the client application of Keycloak.

image.png

② -2) [When executing with Postman] API execution by specifying the access token ② -2) i) Acquisition of access token You can get an access token by setting and executing POST and "http: // localhost: 8088/auth/realms/realm1/protocol/openid-connect/token".

image.png

Response: 
{
    "access_token": "xxxxxxxxxxxxx",
    "expires_in": 300,
    "refresh_expires_in": 0,
    "token_type": "Bearer",
    "not-before-policy": 0,
    "scope": "profile email"
}

② -2) ii) If you specify the access token and execute the API, you will be able to check the implementation details.

Or image.png

② -3) [When executing on the endpoint] API execution by specifying the access token ② -3) i) Acquisition of access token

curl -X POST "http://localhost:8088/auth/realms/[Created realm name]/protocol/openid-connect/token" --data "grant_type=client_credentials&client_secret={Copyed Client Secrets}&client_id=[Created client name]"

image.png

② -3) ii) If you specify the access token and execute the API, you will be able to check the implementation details. image.png

About control by scope

Here, the access token is issued according to the scope and the API is executed. See API Execution by Scope (https://qiita.com/nayylin/items/3be85a6b7daf8a577f55).

Recommended Posts

Use Docker and Keycloak to specify the access token and execute the API
How to play MIDI files using the Java Sound API (specify the MIDI device to use)
I want to use the Java 8 DateTime API slowly (now)
How to call and use API in Java (Spring Boot)
[Java] Use ResolverStyle.LENIENT to handle the date and time nicely
[Docker] How to access the host from inside the container. http://host.docker.internal:
How to use Chain API
When requested access to the resource is denied when pushing with Docker
Use Jenkins to build inside Docker and then create a Docker image.
How to use the link_to method
How to use StringBurrer and Arrays.toString.
How to use the include? method
How to use the form_with method
How to use EventBus3 and ThreadMode
How to use the wrapper class
How to use equality and equality (how to use equals)
Item 59: Know and use the libraries
Hit the Docker API in Rust
[Artifactory] How to use Docker repository
Challenge to install WSL2 and docker
Wait for PostgreSQL to start with Docker and then start the WEB service
[Java] How to get the current date and time and specify the display format
To you who were told "Don't use Stream API" in the field
Is it possible to put the library (aar) in the Android library (aar) and use it?