Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

How to integrate Spring Boot 3, Spring Security, and Keycloak

July 24, 2023
Muhammad Edwin
Related topics:
JavaSpring BootSecurity
Related products:
Red Hat support for Spring Boot

    Quite some time ago, Keycloak deprecated its adapters, including OpenID connect for Java adapters. For Spring Boot developers, this means we need to use Spring Security for OpenID and OAuth2 connectivity with Keycloak instead of relying on Keycloak adapters.

    This tutorial demonstrates how to create a Java application on top of Spring Boot 3 and protect it by using Spring Security and Keycloak, without having to use Keycloak adapters. It walks through the following steps:

    1. Install Keycloak.
    2. Define the Spring Boot version.
    3. Define the Keycloak integration.
    4. Create the Java files.
    5. Test the application.

    Install Keycloak

    First, you need to install Keycloak to our system. In this example, we are using Keycloak 17 and installing it using a container. Here we've used admin as the administrator username and password as its password.

    $ docker pull keycloak/keycloak:17.0.0
    $ docker run -p 8080:8080 \ 
        -e KEYCLOAK_ADMIN=admin \ 
        -e KEYCLOAK_ADMIN_PASSWORD=password \ 
        keycloak/keycloak:17.0.0  start-dev
    

    You can open the login page and input the credentials there (Figure 1).

    Keycloak login page.
    Figure 1: The Keycloak login page.

    After login, create a new "realm" with the name External (Figure 2).

    Adding a new Keycloak realm.
    Figure 2: Creating a new external realm in Keycloak.

    Once the realm has been created, create a Keycloak client with the name external-client (Figure 3).

    Setting up the Keycloak client.
    Figure 3: Setting up the Keycloak client.

    Make sure to configure the client as follows:

    • Client ID: external-client
    • Enabled: On
    • Client Protocol: openid-connect
    • Access type: Confidential
    • Standard flow enabled: On
    • Direct access grants enabled: On
    • Valid redirects URI: http://localhost:8081/*

    Capture the client secret, as shown in Figure 4.

    Adding the Keycloak client secret.
    Figure 4: The Secret field in the client configuration credentials tab.

    Next, create a new user for this realm (Figure 5).

    Adding the Keycloak user.
    Figure 5: Adding a sample user on the Add user page.

    After that, you can create a password for this user (Figure 6).

    Setting the password for the new Keycloak user.
    Figure 6: Setting the password for the new user.

    Once you have completed all of the preceding steps, you are ready to proceed to the next section.

    Define the Spring Boot version

    First, define the Spring version in your pom.xml file. In this example, we are using Spring 3.0.4 and Java 17.

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.edw</groupId>
        <artifactId>spring-3-keycloak</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>3.0.4</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>17</maven.compiler.source>
            <maven.compiler.target>17</maven.compiler.target>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-oauth2-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-security</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>
    

    Define the Keycloak integration

    Define your Keycloak integration by setting them in the application.properties:

    ### server port
    server.port=8081
    spring.application.name=Spring 3 and Keycloak
    
    ## logging
    logging.level.org.springframework.security=INFO
    logging.pattern.console=%d{dd-MM-yyyy HH:mm:ss} %magenta([%thread]) %highlight(%-5level) %logger.%M - %msg%n
    
    ## keycloak
    spring.security.oauth2.client.provider.external.issuer-uri=http://localhost:8080/realms/external
    
    spring.security.oauth2.client.registration.external.provider=external
    spring.security.oauth2.client.registration.external.client-name=external-client
    spring.security.oauth2.client.registration.external.client-id=external-client
    spring.security.oauth2.client.registration.external.client-secret=(put your client secret here)
    spring.security.oauth2.client.registration.external.scope=openid,offline_access,profile
    spring.security.oauth2.client.registration.external.authorization-grant-type=authorization_code
    

    Create the Java files

    Once you've define your configuration, the next step is to create your Java files. We can start with our security configuration:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.security.config.annotation.web.builders.HttpSecurity;
    import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
    import org.springframework.security.config.http.SessionCreationPolicy;
    import org.springframework.security.web.SecurityFilterChain;
    
    @Configuration
    @EnableWebSecurity
    public class SecurityConfiguration {
    
        @Bean
        public SecurityFilterChain configure(HttpSecurity http) throws Exception {
            http
                    .oauth2Client()
                        .and()
                    .oauth2Login()
                    .tokenEndpoint()
                        .and()
                    .userInfoEndpoint();
    
            http
                    .sessionManagement()
                    .sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
    
            http
                    .authorizeHttpRequests()
                                .requestMatchers("/unauthenticated", "/oauth2/**", "/login/**").permitAll()
                                .anyRequest()
                                    .fullyAuthenticated()
                    .and()
                        .logout()
                        .logoutSuccessUrl("http://localhost:8080/realms/external/protocol/openid-connect/logout?redirect_uri=http://localhost:8081/");
    
            return http.build();
        }
    }
    

    Next, we'll create our controller and main Java files:

    package com.edw.controller;
    
    import org.springframework.security.core.context.SecurityContextHolder;
    import org.springframework.security.oauth2.core.user.OAuth2User;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.HashMap;
    
    @RestController
    public class IndexController {
    
        @GetMapping(path = "/")
        public HashMap index() {
            // get a successful user login
            OAuth2User user = ((OAuth2User)SecurityContextHolder.getContext().getAuthentication().getPrincipal());
            return new HashMap(){{
                put("hello", user.getAttribute("name"));
                put("your email is", user.getAttribute("email"));
            }};
        }
    
    
        @GetMapping(path = "/unauthenticated")
        public HashMap unauthenticatedRequests() {
            return new HashMap(){{
                put("this is ", "unauthenticated endpoint");
            }};
        }
    }
    
    package com.edw;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class Main {
        public static void main(String[] args) {
            SpringApplication.run(Main.class, args);
        }
    }
    

    Test the application

    We can directly open our Java application's URL located in port 8081 and be automatically redirected to our Keycloak login page. You can also use a cURL command to check what is happening behind the scenes:

    $ curl -v http://localhost:8081/
    *   Trying ::1:8081...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 8081 (#0)
    > GET / HTTP/1.1
    > Host: localhost:8081
    > User-Agent: curl/7.65.0
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 302
    < Set-Cookie: JSESSIONID=D002DC6523769DB2D4D0559D851575E6; Path=/; HttpOnly
    < X-Content-Type-Options: nosniff
    < X-XSS-Protection: 0
    < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    < Pragma: no-cache
    < Expires: 0
    < X-Frame-Options: DENY
    < Location: http://localhost:8081/oauth2/authorization/external
    < Content-Length: 0
    < Date: Mon, 27 Mar 2023 07:08:27 GMT
    <
    * Connection #0 to host localhost left intact
    
    
    
    $ curl -v  http://localhost:8081/oauth2/authorization/external
    *   Trying ::1:8081...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 8081 (#0)
    > GET /oauth2/authorization/external HTTP/1.1
    > Host: localhost:8081
    > User-Agent: curl/7.65.0
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 302
    < Set-Cookie: JSESSIONID=73BF322BC83966BF49C39398ACD20DAB; Path=/; HttpOnly
    < X-Content-Type-Options: nosniff
    < X-XSS-Protection: 0
    < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    < Pragma: no-cache
    < Expires: 0
    < X-Frame-Options: DENY
    < Location: http://localhost:8080/realms/external/protocol/openid-connect/auth?response_type=code&client_id=external-client&scope=openid%20offline_access%20profile&state=5wK6GouLBPi3DU1hu_AqcoDHefWNt67G5sPfGxfjZtk%3D&redirect_uri=http://localhost:8081/login/oauth2/code/external&nonce=5A8TcFCXueHsf2xBXJQ_NXEjmOtK4BwRh4uvI-kvvIs
    < Content-Length: 0
    < Date: Mon, 27 Mar 2023 07:08:58 GMT
    <
    * Connection #0 to host localhost left intact
    

    You can see that all requests to the root URL have a 302 HTTP response, indicating that our application is protected by the Keycloak login page. However, we can test our whitelist insecure URL and see that we can access it directly without having to log in first.

    $ curl -v  http://localhost:8081/unauthenticated
    *   Trying ::1:8081...
    * TCP_NODELAY set
    * Connected to localhost (::1) port 8081 (#0)
    > GET /unauthenticated HTTP/1.1
    > Host: localhost:8081
    > User-Agent: curl/7.65.0
    > Accept: */*
    >
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 200
    < Set-Cookie: JSESSIONID=22CA2E6EE6B79F7FD649592D87405C71; Path=/; HttpOnly
    < X-Content-Type-Options: nosniff
    < X-XSS-Protection: 0
    < Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    < Pragma: no-cache
    < Expires: 0
    < X-Frame-Options: DENY
    < Content-Type: application/json
    < Transfer-Encoding: chunked
    < Date: Mon, 27 Mar 2023 07:13:00 GMT
    <
    * Connection #0 to host localhost left intact
    
    {"this is ": "unauthenticated endpoint"} 
    

    Let's try to insert the username and password into the login page, as shown in Figure 7.

    Entering the username and password on the Keycloak sign-in page.
    Figure 7: The Keycloak sign-in page.

    The result after login is illustrated in Figure 8.

    The REST API result is shown after successfully logging in with Keycloak credentials.
    Figure 8: The login is successful.

    Summary

    This article showed how Spring Boot 3 and Spring Security can connect to Keycloak using the default Oauth2 client library that comes with Spring Boot (spring-boot-starter-oauth2-client).

    Code for this project can be accessed at https://github.com/edwin/spring-3-keycloak.

    Last updated: April 14, 2025

    Related Posts

    • Easily secure your Spring Boot applications with Keycloak

    • Automate your SSO with Ansible and Keycloak

    • Deploy Keycloak single sign-on with Ansible

    • Spring Boot and OAuth2 with Keycloak

    • Keycloak: Core concepts of open source identity and access management

    • How to restrict user authentication in Keycloak during identity brokering

    Recent Posts

    • What GPU kernels mean for your distributed inference

    • Debugging image mode with Red Hat OpenShift 4.20: A practical guide

    • EvalHub: Because "looks good to me" isn't a benchmark

    • SQL Server HA on RHEL: Meet Pacemaker HA Agent v2 (tech preview)

    • Deploy with confidence: Continuous integration and continuous delivery for agentic AI

    What’s up next?

    Learn how to optimize Java for today’s compute and runtime demands in Quarkus for Spring Developers. This practical guide helps those familiar with Spring’s conventions make a quick and easy transition.

    Get the e-book
    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.