-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
since Keycloak does no longer support the Spring Boot Starter we may just as well upgrade and do the integration manually Signed-off-by: Kai Helbig <[email protected]>
- Loading branch information
Showing
8 changed files
with
118 additions
and
48 deletions.
There are no files selected for viewing
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
12 changes: 4 additions & 8 deletions
12
...kend/src/main/java/com/tngtech/keycloakmock/examplebackend/ExampleBackendApplication.java
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
18 changes: 18 additions & 0 deletions
18
...d/src/main/java/com/tngtech/keycloakmock/examplebackend/config/KeycloakConfiguration.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.tngtech.keycloakmock.examplebackend.config; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.bind.ConstructorBinding; | ||
|
||
@ConfigurationProperties(prefix = "keycloak") | ||
public class KeycloakConfiguration { | ||
private final String clientId; | ||
|
||
@ConstructorBinding | ||
public KeycloakConfiguration(String clientId) { | ||
this.clientId = clientId; | ||
} | ||
|
||
public String getClientId() { | ||
return clientId; | ||
} | ||
} |
74 changes: 50 additions & 24 deletions
74
...ckend/src/main/java/com/tngtech/keycloakmock/examplebackend/config/WebSecurityConfig.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,62 @@ | ||
package com.tngtech.keycloakmock.examplebackend.config; | ||
|
||
import org.keycloak.adapters.springsecurity.KeycloakConfiguration; | ||
import org.keycloak.adapters.springsecurity.config.KeycloakWebSecurityConfigurerAdapter; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | ||
import org.springframework.security.authentication.AbstractAuthenticationToken; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.web.authentication.session.NullAuthenticatedSessionStrategy; | ||
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.oauth2.jwt.Jwt; | ||
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
|
||
@KeycloakConfiguration | ||
public class WebSecurityConfig extends KeycloakWebSecurityConfigurerAdapter { | ||
@Configuration | ||
public class WebSecurityConfig { | ||
private final KeycloakConfiguration keycloakConfiguration; | ||
|
||
@Override | ||
protected void configure(final HttpSecurity http) throws Exception { | ||
super.configure(http); | ||
http.authorizeRequests() | ||
.antMatchers(HttpMethod.OPTIONS, "/**") | ||
.permitAll() | ||
.antMatchers("/api/**") | ||
.authenticated() | ||
.antMatchers("/**") | ||
.permitAll(); | ||
public WebSecurityConfig(KeycloakConfiguration keycloakConfiguration) { | ||
this.keycloakConfiguration = keycloakConfiguration; | ||
} | ||
|
||
@Override | ||
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { | ||
return new NullAuthenticatedSessionStrategy(); | ||
@Bean | ||
public SecurityFilterChain configure(final HttpSecurity http) throws Exception { | ||
http.authorizeHttpRequests( | ||
authorizeRequests -> | ||
authorizeRequests | ||
.requestMatchers(HttpMethod.OPTIONS, "/**") | ||
.permitAll() | ||
.requestMatchers("/api/**") | ||
.authenticated() | ||
.requestMatchers("/**") | ||
.permitAll()) | ||
.oauth2ResourceServer( | ||
server -> server.jwt(jwt -> jwt.jwtAuthenticationConverter(this::convert))); | ||
return http.build(); | ||
} | ||
|
||
@Autowired | ||
public void configureGlobal(final AuthenticationManagerBuilder auth) { | ||
auth.authenticationProvider(keycloakAuthenticationProvider()); | ||
// as Keycloak no longer supports their Spring Boot Starter module, we need to extract roles | ||
// ourselves here ... | ||
private AbstractAuthenticationToken convert(Jwt jwt) { | ||
Set<GrantedAuthority> authorities = | ||
Stream.concat( | ||
jwt | ||
.<Map<String, Collection<String>>>getClaim("realm_access") | ||
.getOrDefault("roles", Collections.emptyList()) | ||
.stream(), | ||
jwt | ||
.<Map<String, Map<String, Collection<String>>>>getClaim("resource_access") | ||
.getOrDefault(keycloakConfiguration.getClientId(), Collections.emptyMap()) | ||
.getOrDefault("roles", Collections.emptyList()) | ||
.stream()) | ||
.map(SimpleGrantedAuthority::new) | ||
.collect(Collectors.toSet()); | ||
return new JwtAuthenticationToken(jwt, authorities); | ||
} | ||
} |
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
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
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
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