OAuth2AuthorizationServerConfigurer configures OAuth 2 authorization-server protocol endpoints and related features inside a Spring Security filter chain. A minimal setup first applies default security, then retrieves the configurer from HttpSecurity for targeted customization. section 14.1
Use the configurer to:
- Apply the minimal authorization-server configuration with OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http). section 14.1
- Enable OpenID Connect with http.getConfigurer(OAuth2AuthorizationServerConfigurer.class).oidc(...). section 14.1
- Route unauthenticated authorization-code users to /login through a LoginUrlAuthenticationEntryPoint. section 14.1
- Place the authorization-server SecurityFilterChain before the general chain with @Order(1). section 14.1
Minimal protocol-endpoint filter
@Bean @Order(1) SecurityFilterChain asFilterChain(HttpSecurity http) throws Exception { OAuth2AuthorizationServerConfiguration.applyDefaultSecurity(http); http.getConfigurer(OAuth2AuthorizationServerConfigurer.class) .oidc(Customizer.withDefaults()); return http.build(); } section 14.1
Listing 14.1 Implementing the filter for configuring protocol endpoints
@Bean
@Order(1)
public SecurityFilterChain asFilterChain(HttpSecurity http)
throws Exception {
OAuth2AuthorizationServerConfiguration #1
.applyDefaultSecurity(http); #1
http.getConfigurer(
OAuth2AuthorizationServerConfigurer.class) #2
.oidc(Customizer.withDefaults()); #2
http.exceptionHandling((e) ->
e.authenticationEntryPoint(
new LoginUrlAuthenticationEntryPoint("/login")) #3
);
return http.build();
}
Inspect the complete filter configuration, including default security, OpenID Connect, login redirection, and filter-chain construction. section 14.1
