Spring Boot: OpenID Connect-innlogging med Spring Security
Innlogging med Dazr Identity: gratis OpenID Connect, driftet i EU, med passnøkler, e-postkoder og Google- eller Microsoft-kontoer bak én knapp.
Du trenger tre verdier fra utviklerkonsollen: klient-ID-en, klienthemmeligheten (bare apper med webserver) og omdirigerings-URI-en du registrerte. Resten står i discovery-dokumentet på https://identity.dazr.eu/.well-known/openid-configuration.
Spring Boot (Spring Security)
Spring Securitys OAuth 2.0-innlogging (Spring Boot 3, spring-boot-starter-oauth2-client) leser discovery-dokumentet. To tillegg: Spring forventer som standard ID-tokener med RS256, så gi den en decoder factory for ES256, og den sender PKCE bare for offentlige klienter, så slå på PKCE for webserverappen din. Registrer omdirigerings-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 innloggingen på /oauth2/authorization/dazr. I en controller gir @AuthenticationPrincipal OidcUser user user.getSubject(), altså Dazr-sub. For å godta Dazr-tilgangstokener i en resource server setter du spring.security.oauth2.resourceserver.jwt.issuer-uri til https://identity.dazr.eu og jws-algorithms til ES256, og kontrollerer at aud er klient-ID-en din.
sub er parvis: den samme for alle apper i organisasjonen din, forskjellig for alle andre organisasjoner. Den endres aldri for en person, mens en e-postadresse kan endres. Lagre sub som kontonøkkel og behandle email som kontaktopplysning.
Mer for utviklere
UtviklerdokumentasjonAlle integrasjonsveiledningerHvilket som helst OpenID Connect-bibliotekPriser