Spring Boot: inloggen via OpenID Connect met Spring Security
Inloggen met Dazr Identity: gratis OpenID Connect, gehost in de EU, met passkeys, e-mailcodes en Google- of Microsoft-accounts achter één knop.
Je hebt drie waarden uit de ontwikkelaarsconsole nodig: de client-ID, het clientgeheim (alleen voor webserver-apps) en de redirect-URI die je hebt geregistreerd. De rest staat in het discovery-document op https://identity.dazr.eu/.well-known/openid-configuration.
Spring Boot (Spring Security)
De OAuth 2.0-login van Spring Security (Spring Boot 3, spring-boot-starter-oauth2-client) leest het discovery-document. Twee aanvullingen: Spring verwacht standaard RS256-ID-tokens, dus geef het een decoder factory voor ES256, en het stuurt PKCE alleen voor publieke clients, dus zet PKCE aan voor je webserver-app. Registreer de 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 het inloggen op /oauth2/authorization/dazr. In een controller geeft @AuthenticationPrincipal OidcUser user met user.getSubject() de Dazr-sub. Om Dazr-access tokens te accepteren in een resource server, zet je spring.security.oauth2.resourceserver.jwt.issuer-uri op https://identity.dazr.eu en jws-algorithms op ES256, en controleer je dat aud je client-ID is.
sub is pairwise: hetzelfde voor elke app van je organisatie, anders voor elke andere organisatie. Hij verandert nooit voor een persoon, een e-mailadres wel. Sla sub op als sleutel van het account en behandel email als contactgegeven.
Meer voor ontwikkelaars
OntwikkelaarsdocumentatieAlle integratiehandleidingenElke OpenID Connect-bibliotheekPrijzen