liveBook · Manning

OAuth2AuthorizationServerConfigurer

As explained in Spring Security in Action, Second Edition

OAuth2AuthorizationServerConfigurer is a Spring Security Authorization Server configuration API for configuring OAuth 2 authorization-server protocol endpoints and features.

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

How Spring Security in Action, Second Edition covers it

How deep it goes
Explains default application, explicit feature enablement, login redirection, and filter-chain ordering, but not endpoint implementation internals.
Examples
Uses an annotated Java SecurityFilterChain with default security, oidc(), LoginUrlAuthenticationEntryPoint, and @Order(1).
What it assumes
Assumes basic Spring Security configuration and bean management while showing the required authorization-server filter-chain setup.
What you can do after
Operate/configure Spring Security to create a minimal authorization-server filter chain and selectively enable OpenID Connect.
How it connects
Connects OAuth 2 authorization servers with OpenID Connect, SecurityFilterChain, HttpSecurity, authentication, and authorization-code grant flows.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring Security in Action, Second Edition. The section links point to the parts of the book it draws on.