Search in sources :

Example 1 with KeycloakAuthenticationProvider

use of org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider in project vboard by voyages-sncf-technologies.

the class WebSecurityConfig method configure.

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.sessionManagement().sessionAuthenticationStrategy(SESSION_AUTH_STRATEGY).sessionCreationPolicy(SessionCreationPolicy.ALWAYS);
    http.csrf().disable();
    http.authorizeRequests().antMatchers("/pins/vblog").permitAll();
    if (KeycloakEnabledInEnv.evaluate()) {
        http.authenticationProvider(new KeycloakAuthenticationProvider());
        configureKeycloakSecurity(http);
    } else if (awsCognitoConfig.isEnabled()) {
        http.authenticationProvider(new AwsCognitoAuthenticationProvider(awsCognitoConfig)).securityContext().securityContextRepository(new AwsCognitoSecurityContextRepository(awsCognitoConfig));
    }
    if (isAuthEnabled()) {
        http.authorizeRequests().requestMatchers(new AntPathRequestMatcher("/**", "OPTIONS")).permitAll().requestMatchers(new AntPathRequestMatcher("/**", "GET")).permitAll().requestMatchers(new AntPathRequestMatcher("/pins/url", "POST")).permitAll().antMatchers("/**").authenticated();
    } else {
        http.authorizeRequests().antMatchers("/**").permitAll();
    }
}
Also used : AntPathRequestMatcher(org.springframework.security.web.util.matcher.AntPathRequestMatcher) AwsCognitoAuthenticationProvider(com.vsct.vboard.config.cognito.AwsCognitoAuthenticationProvider) AwsCognitoSecurityContextRepository(com.vsct.vboard.config.cognito.AwsCognitoSecurityContextRepository) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider)

Example 2 with KeycloakAuthenticationProvider

use of org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider in project tutorials by eugenp.

the class SecurityConfig method configureGlobal.

// Submits the KeycloakAuthenticationProvider to the AuthenticationManager
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
    KeycloakAuthenticationProvider keycloakAuthenticationProvider = keycloakAuthenticationProvider();
    keycloakAuthenticationProvider.setGrantedAuthoritiesMapper(new SimpleAuthorityMapper());
    auth.authenticationProvider(keycloakAuthenticationProvider);
}
Also used : SimpleAuthorityMapper(org.springframework.security.core.authority.mapping.SimpleAuthorityMapper) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider) Autowired(org.springframework.beans.factory.annotation.Autowired)

Example 3 with KeycloakAuthenticationProvider

use of org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider in project shinyproxy by openanalytics.

the class KeycloakAuthenticationBackend method keycloakAuthenticationProvider.

protected KeycloakAuthenticationProvider keycloakAuthenticationProvider() {
    return new KeycloakAuthenticationProvider() {

        @Override
        public Authentication authenticate(Authentication authentication) throws AuthenticationException {
            KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) super.authenticate(authentication);
            List<GrantedAuthority> auth = token.getAuthorities().stream().map(t -> t.getAuthority().toUpperCase()).map(a -> a.startsWith("ROLE_") ? a : "ROLE_" + a).map(a -> new KeycloakRole(a)).collect(Collectors.toList());
            return new KeycloakAuthenticationToken2(token.getAccount(), auth);
        }
    };
}
Also used : ServletException(javax.servlet.ServletException) SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) HashMap(java.util.HashMap) KeycloakAccount(org.keycloak.adapters.spi.KeycloakAccount) KeycloakPreAuthActionsFilter(org.keycloak.adapters.springsecurity.filter.KeycloakPreAuthActionsFilter) HttpSecurity(org.springframework.security.config.annotation.web.builders.HttpSecurity) Inject(javax.inject.Inject) AuthenticationManagerBuilder(org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder) RegisterSessionAuthenticationStrategy(org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider) Map(java.util.Map) AdapterDeploymentContextFactoryBean(org.keycloak.adapters.springsecurity.AdapterDeploymentContextFactoryBean) HttpSessionManager(org.keycloak.adapters.springsecurity.management.HttpSessionManager) KeycloakAuthenticationEntryPoint(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationEntryPoint) AuthenticationException(org.springframework.security.core.AuthenticationException) LogoutFilter(org.springframework.security.web.authentication.logout.LogoutFilter) KeycloakConfigResolver(org.keycloak.adapters.KeycloakConfigResolver) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) KeycloakAuthenticationException(org.keycloak.adapters.springsecurity.KeycloakAuthenticationException) Request(org.keycloak.adapters.spi.HttpFacade.Request) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakDeploymentBuilder(org.keycloak.adapters.KeycloakDeploymentBuilder) KeycloakAuthenticationProcessingFilter(org.keycloak.adapters.springsecurity.filter.KeycloakAuthenticationProcessingFilter) BasicAuthenticationFilter(org.springframework.security.web.authentication.www.BasicAuthenticationFilter) AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) AdapterDeploymentContext(org.keycloak.adapters.AdapterDeploymentContext) Collection(java.util.Collection) AuthenticationEntryPoint(org.springframework.security.web.AuthenticationEntryPoint) Collectors(java.util.stream.Collectors) ApplicationContext(org.springframework.context.ApplicationContext) IAuthenticationBackend(eu.openanalytics.shinyproxy.auth.IAuthenticationBackend) Serializable(java.io.Serializable) GrantedAuthority(org.springframework.security.core.GrantedAuthority) List(java.util.List) Component(org.springframework.stereotype.Component) KeycloakDeployment(org.keycloak.adapters.KeycloakDeployment) SessionRegistryImpl(org.springframework.security.core.session.SessionRegistryImpl) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Environment(org.springframework.core.env.Environment) Bean(org.springframework.context.annotation.Bean) Authentication(org.springframework.security.core.Authentication) KeycloakLogoutHandler(org.keycloak.adapters.springsecurity.authentication.KeycloakLogoutHandler) AdapterConfig(org.keycloak.representations.adapters.config.AdapterConfig) KeycloakAuthenticationToken(org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken) KeycloakRole(org.keycloak.adapters.springsecurity.account.KeycloakRole) Authentication(org.springframework.security.core.Authentication) GrantedAuthority(org.springframework.security.core.GrantedAuthority) KeycloakAuthenticationProvider(org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider)

Aggregations

KeycloakAuthenticationProvider (org.keycloak.adapters.springsecurity.authentication.KeycloakAuthenticationProvider)3 AwsCognitoAuthenticationProvider (com.vsct.vboard.config.cognito.AwsCognitoAuthenticationProvider)1 AwsCognitoSecurityContextRepository (com.vsct.vboard.config.cognito.AwsCognitoSecurityContextRepository)1 IAuthenticationBackend (eu.openanalytics.shinyproxy.auth.IAuthenticationBackend)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 ServletException (javax.servlet.ServletException)1 AdapterDeploymentContext (org.keycloak.adapters.AdapterDeploymentContext)1 KeycloakConfigResolver (org.keycloak.adapters.KeycloakConfigResolver)1 KeycloakDeployment (org.keycloak.adapters.KeycloakDeployment)1 KeycloakDeploymentBuilder (org.keycloak.adapters.KeycloakDeploymentBuilder)1 Request (org.keycloak.adapters.spi.HttpFacade.Request)1 KeycloakAccount (org.keycloak.adapters.spi.KeycloakAccount)1 AdapterDeploymentContextFactoryBean (org.keycloak.adapters.springsecurity.AdapterDeploymentContextFactoryBean)1 KeycloakAuthenticationException (org.keycloak.adapters.springsecurity.KeycloakAuthenticationException)1