Spring Boot: OpenID Connect-inloggning med Spring Security
Inloggning med Dazr Identity: kostnadsfri OpenID Connect, i EU, med nycklar (passkeys), e-postkoder och Google- eller Microsoft-konton bakom en enda knapp.
Du behöver tre värden från utvecklarkonsolen: klient-id:t, klienthemligheten (bara webbserverappar) och den omdirigerings-URI du registrerade. Resten finns i discovery-dokumentet på https://identity.dazr.eu/.well-known/openid-configuration.
Spring Boot (Spring Security)
Spring Securitys OAuth 2.0-inloggning (Spring Boot 3, spring-boot-starter-oauth2-client) läser discovery-dokumentet. Två tillägg: Spring förväntar sig som standard ID-token med RS256, så ge den en decoder factory för ES256, och den skickar PKCE bara för publika klienter, så slå på PKCE för din webbserverapp. Registrera omdirigerings-URI:n 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;
}
}
Starta inloggningen på /oauth2/authorization/dazr. I en controller ger @AuthenticationPrincipal OidcUser user user.getSubject(), alltså Dazr-sub. För att acceptera Dazr-åtkomsttoken i en resource server sätter du spring.security.oauth2.resourceserver.jwt.issuer-uri till https://identity.dazr.eu och jws-algorithms till ES256 och kontrollerar att aud är ditt klient-ID.
sub är parvist: detsamma för alla appar i din organisation, olika för alla andra organisationer. Det ändras aldrig för en person, medan en e-postadress kan ändras. Spara sub som kontonyckel och behandla email som kontaktuppgift.
Mer för utvecklare
UtvecklardokumentationAlla integrationsguiderValfritt OpenID Connect-bibliotekPriser