Skip to content

Instantly share code, notes, and snippets.

@sjohnr
Last active October 20, 2021 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sjohnr/e7cb2eccddaa009b6711120fc7da36f8 to your computer and use it in GitHub Desktop.
Save sjohnr/e7cb2eccddaa009b6711120fc7da36f8 to your computer and use it in GitHub Desktop.
StackOverflowException when adapter's AuthenticationManager gets published as a bean #10419
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