Add How-to: Authenticate using Social Login

Closes gh-538
This commit is contained in:
Steve Riesenberg
2023-05-04 09:46:01 -05:00
parent 64ddcfc3ec
commit 048896ef74
10 changed files with 339 additions and 0 deletions

View File

@@ -15,6 +15,7 @@
*/
package sample.federation;
// tag::imports[]
import java.io.IOException;
import jakarta.servlet.ServletException;
@@ -31,6 +32,7 @@ import org.springframework.security.web.DefaultRedirectStrategy;
import org.springframework.security.web.RedirectStrategy;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.web.util.UriComponentsBuilder;
// end::imports[]
/**
* An {@link AuthenticationEntryPoint} for initiating the login flow to an
@@ -40,6 +42,7 @@ import org.springframework.web.util.UriComponentsBuilder;
* @author Steve Riesenberg
* @since 1.1
*/
// tag::class[]
public final class FederatedIdentityAuthenticationEntryPoint implements AuthenticationEntryPoint {
private final RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
@@ -80,3 +83,4 @@ public final class FederatedIdentityAuthenticationEntryPoint implements Authenti
}
}
// end::class[]

View File

@@ -15,6 +15,7 @@
*/
package sample.federation;
// tag::imports[]
import java.io.IOException;
import java.util.function.Consumer;
@@ -28,6 +29,7 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
// end::imports[]
/**
* An {@link AuthenticationSuccessHandler} for capturing the {@link OidcUser} or
@@ -36,6 +38,7 @@ import org.springframework.security.web.authentication.SavedRequestAwareAuthenti
* @author Steve Riesenberg
* @since 1.1
*/
// tag::class[]
public final class FederatedIdentityAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
private final AuthenticationSuccessHandler delegate = new SavedRequestAwareAuthenticationSuccessHandler();
@@ -66,3 +69,4 @@ public final class FederatedIdentityAuthenticationSuccessHandler implements Auth
}
}
// end::class[]

View File

@@ -15,6 +15,7 @@
*/
package sample.federation;
// tag::imports[]
import java.util.function.Consumer;
import org.springframework.context.ApplicationContext;
@@ -24,6 +25,7 @@ import org.springframework.security.oauth2.client.registration.ClientRegistratio
import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.util.Assert;
// end::imports[]
/**
* A configurer for setting up Federated Identity Management.
@@ -31,6 +33,7 @@ import org.springframework.util.Assert;
* @author Steve Riesenberg
* @since 1.1
*/
// tag::class[]
public final class FederatedIdentityConfigurer extends AbstractHttpConfigurer<FederatedIdentityConfigurer, HttpSecurity> {
private String loginPageUrl = "/login";
@@ -123,3 +126,4 @@ public final class FederatedIdentityConfigurer extends AbstractHttpConfigurer<Fe
// @formatter:on
}
// end::class[]

View File

@@ -15,6 +15,7 @@
*/
package sample.federation;
// tag::imports[]
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -30,6 +31,7 @@ import org.springframework.security.oauth2.core.oidc.user.OidcUser;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.oauth2.server.authorization.token.JwtEncodingContext;
import org.springframework.security.oauth2.server.authorization.token.OAuth2TokenCustomizer;
// end::imports[]
/**
* An {@link OAuth2TokenCustomizer} to map claims from a federated identity to
@@ -38,6 +40,7 @@ import org.springframework.security.oauth2.server.authorization.token.OAuth2Toke
* @author Steve Riesenberg
* @since 1.1
*/
// tag::class[]
public final class FederatedIdentityIdTokenCustomizer implements OAuth2TokenCustomizer<JwtEncodingContext> {
private static final Set<String> ID_TOKEN_CLAIMS = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
@@ -89,3 +92,4 @@ public final class FederatedIdentityIdTokenCustomizer implements OAuth2TokenCust
}
}
// end::class[]

View File

@@ -15,11 +15,13 @@
*/
package sample.federation;
// tag::imports[]
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import org.springframework.security.oauth2.core.user.OAuth2User;
// end::imports[]
/**
* Example {@link Consumer} to perform JIT provisioning of an {@link OAuth2User}.
@@ -27,6 +29,7 @@ import org.springframework.security.oauth2.core.user.OAuth2User;
* @author Steve Riesenberg
* @since 1.1
*/
// tag::class[]
public final class UserRepositoryOAuth2UserHandler implements Consumer<OAuth2User> {
private final UserRepository userRepository = new UserRepository();
@@ -55,3 +58,4 @@ public final class UserRepositoryOAuth2UserHandler implements Consumer<OAuth2Use
}
}
// end::class[]