Spring Boot OpenID Connect login with Spring Security

You need three values from the developer console: the client ID, the client secret (web server apps only) and the redirect URI you registered. Everything else comes from the discovery document at https://identity.dazr.eu/.well-known/openid-configuration.

Spring Boot (Spring Security)

Spring Security’s OAuth 2.0 login (Spring Boot 3, spring-boot-starter-oauth2-client) reads the discovery document. Two additions: Spring expects RS256 ID tokens by default, so give it a decoder factory for ES256, and it sends PKCE only for public clients, so turn PKCE on for your web server app. Register the redirect URI https://app.example.eu/login/oauth2/code/dazr.

# application.yml
spring:
  security:
    oauth2:
      client:
        registration:
          dazr:
            provider: dazr
            client-id: ${DAZR_CLIENT_ID}
            client-secret: ${DAZR_CLIENT_SECRET}
            client-authentication-method: client_secret_basic
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            scope: openid,profile,email
        provider:
          dazr:
            issuer-uri: https://identity.dazr.eu
            user-name-attribute: sub
// SecurityConfig.java
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.client.oidc.authentication.OidcIdTokenDecoderFactory;
import org.springframework.security.oauth2.client.registration.ClientRegistration;
import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
import org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizationRequestResolver;
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestCustomizers;
import org.springframework.security.oauth2.jose.jws.SignatureAlgorithm;
import org.springframework.security.oauth2.jwt.JwtDecoderFactory;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
public class SecurityConfig {

  @Bean
  SecurityFilterChain security(HttpSecurity http, ClientRegistrationRepository clients) throws Exception {
    var resolver = new DefaultOAuth2AuthorizationRequestResolver(clients, "/oauth2/authorization");
    resolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce());
    http
      .authorizeHttpRequests(a -> a.requestMatchers("/", "/error").permitAll().anyRequest().authenticated())
      .oauth2Login(o -> o.authorizationEndpoint(e -> e.authorizationRequestResolver(resolver)));
    return http.build();
  }

  // Dazr Identity signs ID tokens with ES256.
  @Bean
  JwtDecoderFactory<ClientRegistration> idTokenDecoderFactory() {
    var factory = new OidcIdTokenDecoderFactory();
    factory.setJwsAlgorithmResolver(registration -> SignatureAlgorithm.ES256);
    return factory;
  }
}

Start the sign-in at /oauth2/authorization/dazr. In a controller, @AuthenticationPrincipal OidcUser user gives user.getSubject(), the Dazr sub. To accept Dazr access tokens in a resource server, set spring.security.oauth2.resourceserver.jwt.issuer-uri to https://identity.dazr.eu and jws-algorithms to ES256, and check that aud is your client ID.

More for developers