Integrating Keycloak with Spring Boot for GitHub Login

In this tutorial, we’ll examine how to use Keycloak to access a resource using our github credentials.

To understand how to login with keycloak using spring boot kindly refer here – Securing Spring Boot Application with Keycloak.

In our previous article we have configured Keycloak by adding username and password in keycloak server.

In this article let us login to our application with help of github credentials.

First let us configure our github, We need to make github to accept the authorization requests from our client.

Step 1:

First let us create an identity provider in our keycloak,

Make sure to not the Redirect URI -> http://localhost:8080/realms/javainfiniteapp/broker/github/endpoint

Step 2:

Go to github and login,

Go to settings,


Then select developer settings,

developer settings

Select OAuth apps and add a new OAuth app,

OAuth app

Step 3:

Generate client credentials,


We have generated client credentials and please note we have used that Authorization callback URL which we generated from Keycloak. (http://localhost:8080/realms/javainfiniteapp/broker/github/endpoint)

Client name to identify our client and homepage url is the server and port where our application is running.

Copy the ClientId and Client Secret.

Step 4:

In our keycloak Identity provider, fill the client ID and client Secret values and save.

clientId and clientSecret

Spring Boot code:

Project Structure:

Project Structure


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="" xmlns:xsi=""
		<relativePath/> <!-- lookup parent from repository -->
	<name>Keycloak Security</name>
	<description>KeyCloak Security Example</description>
		<!-- -->



package com.javainfinite.keycloack.controller;

import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken;
import org.keycloak.representations.AccessToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

public class KeycloakController {

    public String login() {
        return "Welcome to login screen";

    public String homepage(Principal principal) {
        KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) principal;
        AccessToken accessToken = token.getAccount().getKeycloakSecurityContext().getToken();
        return "Welcome to homepage, " + accessToken.getPreferredUsername() + " successfully logged in";



import org.keycloak.adapters.KeycloakConfigResolver;
import org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

public class KeycloakConfigurationResolver {

    public KeycloakConfigResolver KeycloakConfigResolver() {
        return new KeycloakSpringBootConfigResolver();


import org.keycloak.adapters.springsecurity.KeycloakConfiguration;
import org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider;
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

public class KeycloakSecurity extends KeycloakWebSecurityConfigurerAdapter {

    protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
        return new RegisterSessionAuthenticationStrategy(
                new SessionRegistryImpl());

    protected void configure(HttpSecurity http) throws Exception {

    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
        keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());

  • @KeycloakConfiguration: It includes EnableWebSecurity and Configuration and provides Keycloak based configurations
  • SessionAuthenticationStrategy:
    • Allows pluggable support for HttpSession-related behaviour when an authentication occurs.
    • Typical use would be to make sure a session exists or to change the session Id to guard against session-fixation attacks. for details click here
  • ConfigureGlobal:
    • Keycloak Authentication provider is included with authentication manager.
    • In our Configure method, we have made homepage to be authenticated and authorized for the users with role – standard

We have secured “/homepage” endpoint and other endpoint does not require any permission.


package com.javainfinite.keycloack;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

public class KeycloakSecurityApplication {

	public static void main(String[] args) {, args);


Now let us run our application,

application started
Able to login to access without any permissions.

Now let us try to login to our “/homepage” with help of github credentials,

http://localhost:8082/homepage will redirect to below page,

login page

Click on GitHub,

GitHub login

Once we login with our account then we will get an prompt webpage to verify whether the access can be provided,

Authorization Page

Once we grant the permission we will be able to login to our homepage.

We have successfully implemented Keycloak with github login using Spring boot.

For any doubts or queries or needed help with implementing above example please do leave a comment

Code can be downloaded here.

By Sri

One thought on “Integrating Keycloak with Spring Boot for GitHub Login”

Leave a Reply

Your email address will not be published. Required fields are marked *