如何使用 PKCE 在单页应用中进行身份验证

此指南展示了如何配置 Spring授权服务器 以支持带有代码交换证明密钥(PKCE)的单页应用(SPA)。 本指南的目的在于演示如何支持公共客户端并要求使用PKCE进行身份验证。spring-doc.cadn.net.cn

Spring Authorization Server 不会为公共客户端颁发刷新Tokens。我们建议使用后端为前端(BFF)模式作为替代方案,以替代公开客户端的暴露。有关更多信息,请参见 gh-297

允许CORS

SPA 由静态资源组成,可以以多种方式部署。 它可以单独部署在后端之外的地方,例如通过 CDN 或独立的 Web 服务器,或者可以与后端一起使用 Spring Boot 部署。spring-doc.cadn.net.cn

当单页应用程序(SPA)托管在不同的域名下时,可以使用跨源资源共享(CORS)来允许该应用与后端进行通信。spring-doc.cadn.net.cn

例如,如果您在本地端口4200上运行了一个 Angular 开发服务器,您可以定义一个CorsConfigurationSource @Bean并配置 Spring Security 允许预检请求,可以使用 DSL 作为以下示例:spring-doc.cadn.net.cn

允许CORS
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.server.authorization.config.annotation.web.configurers.OAuth2AuthorizationServerConfigurer;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint;
import org.springframework.security.web.util.matcher.MediaTypeRequestMatcher;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

	@Bean
	@Order(1)
	public SecurityFilterChain authorizationServerSecurityFilterChain(HttpSecurity http)
			throws Exception {
		OAuth2AuthorizationServerConfigurer authorizationServerConfigurer =
				OAuth2AuthorizationServerConfigurer.authorizationServer();

		http
			.securityMatcher(authorizationServerConfigurer.getEndpointsMatcher())
			.with(authorizationServerConfigurer, (authorizationServer) ->
				authorizationServer
					.oidc(Customizer.withDefaults())	// Enable OpenID Connect 1.0
			)
			.authorizeHttpRequests((authorize) ->
				authorize
					.anyRequest().authenticated()
			)
			// Redirect to the login page when not authenticated from the
			// authorization endpoint
			.exceptionHandling((exceptions) -> exceptions
				.defaultAuthenticationEntryPointFor(
					new LoginUrlAuthenticationEntryPoint("/login"),
					new MediaTypeRequestMatcher(MediaType.TEXT_HTML)
				)
			);

		return http.cors(Customizer.withDefaults()).build();
	}

	@Bean
	@Order(2)
	public SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http)
			throws Exception {
		http
			.authorizeHttpRequests((authorize) -> authorize
				.anyRequest().authenticated()
			)
			// Form login handles the redirect to the login page from the
			// authorization server filter chain
			.formLogin(Customizer.withDefaults());

		return http.cors(Customizer.withDefaults()).build();
	}

	@Bean
	public CorsConfigurationSource corsConfigurationSource() {
		UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
		CorsConfiguration config = new CorsConfiguration();
		config.addAllowedHeader("*");
		config.addAllowedMethod("*");
		config.addAllowedOrigin("http://127.0.0.1:4200");
		config.setAllowCredentials(true);
		source.registerCorsConfiguration("/**", config);
		return source;
	}

}
点击上方代码示例中的“展开折叠文本”图标以显示完整示例。

配置公共客户端

SPA 无法安全存储凭据,因此必须被视为一个公共客户端。 公共客户端应当被要求使用 授权码交换密钥证明 (PKCE)。spring-doc.cadn.net.cn

继续前面的示例,您可以配置 Spring Authorization Server 以支持使用客户端认证方法none的公共客户端,并要求进行 PKCE,如下所示:spring-doc.cadn.net.cn

spring:
  security:
    oauth2:
      authorizationserver:
        client:
          public-client:
            registration:
              client-id: "public-client"
              client-authentication-methods:
                - "none"
              authorization-grant-types:
                - "authorization_code"
              redirect-uris:
                - "http://127.0.0.1:4200"
              scopes:
                - "openid"
                - "profile"
            require-authorization-consent: true
            require-proof-key: true
@Bean
public RegisteredClientRepository registeredClientRepository() {
	RegisteredClient publicClient = RegisteredClient.withId(UUID.randomUUID().toString())
		.clientId("public-client")
		.clientAuthenticationMethod(ClientAuthenticationMethod.NONE)
		.authorizationGrantType(AuthorizationGrantType.AUTHORIZATION_CODE)
		.redirectUri("http://127.0.0.1:4200")
		.scope(OidcScopes.OPENID)
		.scope(OidcScopes.PROFILE)
		.clientSettings(ClientSettings.builder()
			.requireAuthorizationConsent(true)
			.requireProofKey(true)
			.build()
		)
		.build();

	return new InMemoryRegisteredClientRepository(publicClient);
}
The requireProofKey setting is important to prevent the PKCE 降级攻击.

使用客户端进行身份验证

一旦服务器配置为支持公共客户端,一个常见的问题是:如何对客户端进行身份验证并获取访问Tokens? 简短的答案是:与任何其他客户端相同的方式。spring-doc.cadn.net.cn

SPA 是基于浏览器的应用程序,因此与任何其他客户端一样使用基于重定向的流程。这个问题通常是因为期望可以通过 REST API 进行身份验证,而在 OAuth2 中并非如此。

需要更详细的回答,前提是对 OAuth2 和 OpenID Connect 中涉及的流程有深入了解,在这种情况下是授权码(Authorization Code)流程。 授权码(Authorization Code)流程的具体步骤如下:spring-doc.cadn.net.cn

  1. 客户端通过重定向到授权端点发起OAuth2请求。对于公开客户端,此步骤包括生成code_verifier并计算code_challenge,然后将code_challenge作为查询参数发送。spring-doc.cadn.net.cn

  2. 如果用户未经过身份验证,授权服务器将重定向到登录页面。完成认证后,用户会被重定向回授权端点再次。spring-doc.cadn.net.cn

  3. 如果用户尚未同意请求的范围(权限)并且需要获取同意,则会显示同意页面。spring-doc.cadn.net.cn

  4. 用户同意后,授权服务器生成一个authorization_code并通过redirect_uri重定向回客户端。spring-doc.cadn.net.cn

  5. The client通过查询参数获取到authorization_code,并执行一个请求到Token Endpoint。对于公共客户端,在此步骤中需要发送code_verifier参数而不是凭据进行身份验证。spring-doc.cadn.net.cn

正如你所见,这个流程相当复杂,而本概览仅触及皮毛。spring-doc.cadn.net.cn

建议您使用单页应用框架支持的健壮的客户端库来处理授权码流程。