Spring Boot: OpenID Connect-login med Spring Security

Du skal bruge tre værdier fra udviklerkonsollen: klient-id’et, klienthemmeligheden (kun webserverapps) og den omdirigerings-URI, du har registreret. Resten står i discovery-dokumentet på https://identity.dazr.eu/.well-known/openid-configuration.

Spring Boot (Spring Security)

Spring Securitys OAuth 2.0-login (Spring Boot 3, spring-boot-starter-oauth2-client) læser discovery-dokumentet. To tilføjelser: Spring forventer som standard RS256-ID-tokens, så giv den en decoder factory til ES256, og den sender kun PKCE for offentlige klienter, så slå PKCE til for din webserver-app. Registrér redirect-URI’en 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 login på /oauth2/authorization/dazr. I en controller giver @AuthenticationPrincipal OidcUser user user.getSubject(), altså Dazr-sub. For at acceptere Dazr-access tokens i en resource server skal du sætte spring.security.oauth2.resourceserver.jwt.issuer-uri til https://identity.dazr.eu og jws-algorithms til ES256 og kontrollere, at aud er dit client-ID.

Mere til udviklere