Last active
October 20, 2021 20:06
-
-
Save sjohnr/e7cb2eccddaa009b6711120fc7da36f8 to your computer and use it in GitHub Desktop.
StackOverflowException when adapter's AuthenticationManager gets published as a bean #10419
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.springframework.context.annotation.Bean | |
import org.springframework.security.authentication.AnonymousAuthenticationProvider | |
import org.springframework.security.authentication.AuthenticationManager | |
import org.springframework.security.authentication.ProviderManager | |
import org.springframework.security.config.annotation.web.builders.HttpSecurity | |
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity | |
import org.springframework.security.config.web.servlet.invoke | |
import org.springframework.security.oauth2.jwt.JwtDecoder | |
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationConverter | |
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationProvider | |
import org.springframework.security.oauth2.server.resource.authentication.JwtGrantedAuthoritiesConverter | |
import org.springframework.security.web.SecurityFilterChain | |
import java.util.UUID | |
/** | |
* Security configuration using the kotlin `HttpSecurityDsl`. | |
* | |
* @author Steve Riesenberg | |
*/ | |
@EnableWebSecurity | |
class SecurityConfiguration { | |
@Bean | |
fun securityFilterChain(http: HttpSecurity): SecurityFilterChain { | |
http { | |
authorizeRequests { | |
authorize(anyRequest, authenticated) | |
} | |
oauth2ResourceServer { | |
jwt { } | |
} | |
} | |
return http.build() | |
} | |
@Bean | |
fun jwtAuthenticationConverter(): JwtAuthenticationConverter { | |
val jwtGrantedAuthoritiesConverter = JwtGrantedAuthoritiesConverter() | |
jwtGrantedAuthoritiesConverter.setAuthoritiesClaimName("groups") | |
jwtGrantedAuthoritiesConverter.setAuthorityPrefix("") | |
val jwtAuthenticationConverter = JwtAuthenticationConverter() | |
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(jwtGrantedAuthoritiesConverter) | |
return jwtAuthenticationConverter | |
} | |
@Bean | |
fun jwtAuthenticationProvider(jwtDecoder: JwtDecoder, jwtAuthenticationConverter: JwtAuthenticationConverter): JwtAuthenticationProvider { | |
val jwtAuthenticationProvider = JwtAuthenticationProvider(jwtDecoder) | |
jwtAuthenticationProvider.setJwtAuthenticationConverter(jwtAuthenticationConverter) | |
return jwtAuthenticationProvider | |
} | |
@Bean | |
fun authenticationManager(jwtAuthenticationProvider: JwtAuthenticationProvider): AuthenticationManager { | |
val anonymousAuthenticationProvider = AnonymousAuthenticationProvider(UUID.randomUUID().toString()) | |
return ProviderManager(anonymousAuthenticationProvider, jwtAuthenticationProvider) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment