Skip to content

Instantly share code, notes, and snippets.

@akoserwal
Created October 2, 2019 15:17
Show Gist options
  • Save akoserwal/3648953f708d1a036a7b8c3ad9b410a9 to your computer and use it in GitHub Desktop.
Save akoserwal/3648953f708d1a036a7b8c3ad9b410a9 to your computer and use it in GitHub Desktop.
keycloak-spring-boot-config
@KeycloakConfiguration
class SecurityConfig extends KeycloakWebSecurityConfigurerAdapter {
/**
* Registers the KeycloakAuthenticationProvider with the authentication manager.
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
auth.authenticationProvider(keycloakAuthenticationProvider);
}
/**
* Provide a session authentication strategy bean which should be of type
* RegisterSessionAuthenticationStrategy for public or confidential applications
* and NullAuthenticatedSessionStrategy for bearer-only applications.
*/
@Bean
@Override
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() {
return new RegisterSessionAuthenticationStrategy(new SessionRegistryImpl());
}
/**
* Use properties in application.properties instead of keycloak.json
*/
@Bean
@Primary
public KeycloakConfigResolver keycloakConfigResolver(KeycloakSpringBootProperties properties) {
return new CustomKeycloakSpringBootConfigResolver(properties);
}
/**
* Secure appropriate endpoints
*/
@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
http.authorizeRequests()
.antMatchers("/**").authenticated()
.anyRequest().authenticated();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment