Last active
December 28, 2018 17:44
-
-
Save rkbalgi/a56ea09d054a79ca140c8009ad6711ad to your computer and use it in GitHub Desktop.
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
@Configuration | |
public class AppConfig{ | |
@Bean | |
public KeycloakConfigResolver KeycloakConfigResolver(KeycloakSpringBootProperties props) { | |
return new SimpleKcConfigResolver(props); | |
} | |
} |
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 gist explains setting up an API application (stateless) using bearer authentication with keycloak (v 4.5.0). | |
AppConfig.java is required as a workaround for the issue described here - https://issues.jboss.org/browse/KEYCLOAK-8444 | |
Below are the important dependencies - | |
<dependency> | |
<groupId>org.keycloak</groupId> | |
<artifactId>keycloak-spring-boot-starter</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
<version>2.0.4.RELEASE</version> | |
</dependency> |
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
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; | |
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | |
import org.springframework.security.config.http.SessionCreationPolicy; | |
import org.springframework.security.core.authority.mapping.SimpleAuthorityMapper; | |
import org.springframework.security.web.authentication.session.NullAuthenticatedSessionStrategy; | |
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy; | |
/** | |
* | |
*/ | |
@KeycloakConfiguration | |
public class KeycloakSecurityConfig extends KeycloakWebSecurityConfigurerAdapter { | |
/** | |
* Registers the KeycloakAuthenticationProvider with the authentication manager. | |
*/ | |
@Autowired | |
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { | |
final KeycloakAuthenticationProvider authenticationProvider = keycloakAuthenticationProvider(); | |
authenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper()); | |
auth.authenticationProvider(authenticationProvider); | |
} | |
/** | |
* Defines the session authentication strategy. | |
*/ | |
@Bean | |
@Override | |
protected SessionAuthenticationStrategy sessionAuthenticationStrategy() { | |
return new NullAuthenticatedSessionStrategy(); | |
} | |
@Override | |
protected void configure(HttpSecurity http) throws Exception { | |
super.configure(http); | |
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) | |
.sessionAuthenticationStrategy(sessionAuthenticationStrategy()).and() | |
.authorizeRequests().antMatchers("/resrc/unprotected/**") | |
.permitAll() | |
.and() | |
.authorizeRequests() | |
.anyRequest().authenticated(); | |
} | |
} |
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
import org.keycloak.adapters.KeycloakConfigResolver; | |
import org.keycloak.adapters.KeycloakDeployment; | |
import org.keycloak.adapters.KeycloakDeploymentBuilder; | |
import org.keycloak.adapters.spi.HttpFacade.Request; | |
import org.keycloak.adapters.springboot.KeycloakSpringBootProperties; | |
import org.springframework.beans.BeansException; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ApplicationContextAware; | |
/** | |
* | |
*/ | |
public class SimpleKcConfigResolver implements KeycloakConfigResolver, ApplicationContextAware { | |
private final KeycloakDeployment deployment; | |
public SimpleKcConfigResolver(KeycloakSpringBootProperties props) { | |
deployment = KeycloakDeploymentBuilder | |
.build(props); | |
} | |
@Override | |
public KeycloakDeployment resolve(Request facade) { | |
return deployment; | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext ctx) | |
throws BeansException { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment