Search in sources :

Example 11 with KeycloakAuthenticationToken

use of org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken 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

KeycloakAuthenticationToken (org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken)11 Authentication (org.springframework.security.core.Authentication)5 KeycloakAccount (org.keycloak.adapters.spi.KeycloakAccount)4 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 Before (org.junit.Before)3 Test (org.junit.Test)3 RefreshableKeycloakSecurityContext (org.keycloak.adapters.RefreshableKeycloakSecurityContext)3 SimpleKeycloakAccount (org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 Principal (java.security.Principal)2 KeycloakSecurityContext (org.keycloak.KeycloakSecurityContext)2 KeycloakRole (org.keycloak.adapters.springsecurity.account.KeycloakRole)2 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 IAuthenticationBackend (eu.openanalytics.shinyproxy.auth.IAuthenticationBackend)1 Serializable (java.io.Serializable)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1