Last active
June 11, 2023 22:08
-
-
Save ThomasVitale/5544d276479d3895f4e8632720f5f92b to your computer and use it in GitHub Desktop.
Securing a Spring Boot Application with Keycloak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keycloak.realm=public-library | |
keycloak.resource=app-client | |
keycloak.auth-server-url=http://localhost:8180/auth | |
keycloak.ssl-required=external | |
keycloak.public-client=true | |
keycloak.securityConstraints[0].authRoles[0]=Member | |
keycloak.securityConstraints[0].authRoles[1]=Librarian | |
keycloak.securityConstraints[0].securityCollections[0].name=member resource | |
keycloak.securityConstraints[0].securityCollections[0].patterns[0]=/books | |
keycloak.securityConstraints[1].authRoles[0]=Librarian | |
keycloak.securityConstraints[1].securityCollections[0].name=librarian resource | |
keycloak.securityConstraints[1].securityCollections[0].patterns[0]=/manager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.thomasvitale.keycloak.controller; | |
import com.thomasvitale.keycloak.repository.BookRepository; | |
import org.keycloak.KeycloakSecurityContext; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Controller; | |
import org.springframework.ui.Model; | |
import org.springframework.web.bind.annotation.GetMapping; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
@Controller | |
public class ApplicationController { | |
@Autowired | |
private HttpServletRequest request; | |
@Autowired | |
private BookRepository bookRepository; | |
@GetMapping(value = "/") | |
public String getHome() { | |
return "index"; | |
} | |
@GetMapping(value = "/books") | |
public String getBooks(Model model) { | |
configCommonAttributes(model); | |
model.addAttribute("books", bookRepository.readAll()); | |
return "books"; | |
} | |
@GetMapping(value = "/manager") | |
public String getManager(Model model) { | |
configCommonAttributes(model); | |
model.addAttribute("books", bookRepository.readAll()); | |
return "manager"; | |
} | |
@GetMapping(value = "/logout") | |
public String logout() throws ServletException { | |
request.logout(); | |
return "redirect:/"; | |
} | |
private void configCommonAttributes(Model model) { | |
model.addAttribute("name", getKeycloakSecurityContext().getIdToken().getGivenName()); | |
} | |
private KeycloakSecurityContext getKeycloakSecurityContext() { | |
return (KeycloakSecurityContext) request.getAttribute(KeycloakSecurityContext.class.getName()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
ext { | |
springBootVersion = '2.0.8.RELEASE' | |
} | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") | |
} | |
} | |
apply plugin: 'java' | |
apply plugin: 'org.springframework.boot' | |
apply plugin: 'io.spring.dependency-management' | |
group = 'com.thomasvitale' | |
version = '0.0.1-SNAPSHOT' | |
sourceCompatibility = '1.8' | |
repositories { | |
mavenCentral() | |
} | |
ext { | |
set('keycloakVersion', '4.8.3.Final') | |
} | |
dependencies { | |
// Spring | |
implementation 'org.springframework.boot:spring-boot-starter-web' | |
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' | |
implementation 'org.springframework.boot:spring-boot-devtools' | |
// Keycloak | |
implementation 'org.keycloak:keycloak-spring-boot-starter' | |
// Test | |
testImplementation 'org.springframework.boot:spring-boot-starter-test' | |
} | |
dependencyManagement { | |
imports { | |
mavenBom "org.keycloak.bom:keycloak-adapter-bom:${keycloakVersion}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's very helpful. Thank you!!!