此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Security 6.4.5spring-doc.cadn.net.cn

OAuth 2.0 更改

驗證typHeader 替换为JwtTypeValidator

NimbusJwtDecoder在 Spring Security 7 中将移动typ标头验证设置为JwtTypeValidator而不是依赖 Nimbus。 这使它与NimbusJwtDecoder验证声明,而不是依赖 Nimbus 来验证它们。spring-doc.cadn.net.cn

如果要在jwtProcessorCustomizer方法,那么您应该将其移动到JwtTypeValidatorOAuth2TokenValidator你自己的。spring-doc.cadn.net.cn

要检查您是否已准备好进行此更改,请添加默认的JwtTypeValidator添加到您的验证者列表中,因为默认情况下这将包含在 7 中:spring-doc.cadn.net.cn

@Bean
JwtDecoder jwtDecoder() {
	NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(false) (1)
        // ... your remaining configuration
        .build();
	jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
		new JwtIssuerValidator(location), JwtTypeValidator.jwt())); (2)
	return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
    val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(false) (1)
        // ... your remaining configuration
        .build()
    jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
        JwtIssuerValidator(location), JwtTypeValidator.jwt())) (2)
    return jwtDecoder
}
1 - 关闭 Nimbus,验证typ(默认情况下,这将在 7 中关闭)
2 - 添加默认的typvalidator (默认情况下,这将包含在 7 中)

请注意,默认值验证typ值要么是JWT或 not present,这与 Nimbus 默认值相同。 它还与 RFC 7515 保持一致,后者规定typ是可选的。spring-doc.cadn.net.cn

我正在使用DefaultJOSEObjectTypeVerifier

如果您的配置中有类似以下内容的内容:spring-doc.cadn.net.cn

@Bean
JwtDecoder jwtDecoder() {
	NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .jwtProcessorCustomizer((c) -> c
            .setJWSTypeVerifier(new DefaultJOSEObjectTypeVerifier<>("JOSE"))
        )
        .build();
	return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
    val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .jwtProcessorCustomizer {
            it.setJWSTypeVerifier(DefaultJOSEObjectTypeVerifier("JOSE"))
        }
        .build()
    return jwtDecoder
}

然后将其更改为:spring-doc.cadn.net.cn

@Bean
JwtDecoder jwtDecoder() {
	NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(false)
        .build();
	jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
		new JwtIssuerValidator(location), new JwtTypeValidator("JOSE")));
	return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
    val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(false)
        .build()
	jwtDecoder.setJwtValidator(JwtValidators.createDefaultWithValidators(
		JwtIssuerValidator(location), JwtTypeValidator("JOSE")))
    return jwtDecoder
}

要指示typheader 是可选的,请使用#setAllowEmpty(true)(这相当于包括nullDefaultJOSEObjectTypeVerifier).spring-doc.cadn.net.cn

我想选择退出

如果你想继续按照你现在的方式做事,那么步骤是相似的,只是相反:spring-doc.cadn.net.cn

@Bean
JwtDecoder jwtDecoder() {
	NimbusJwtDecoder jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(true) (1)
        .jwtProcessorCustomizer((c) -> c
            .setJWSTypeVerifier(new DefaultJOSEObjectTypeVerifier<>("JOSE"))
        )
        .build();
	jwtDecoder.setJwtValidator(new DelegatingOAuth2TokenValidator<>(
		new JwtTimestampValidator(), new JwtIssuerValidator(location))); (2)
	return jwtDecoder;
}
@Bean
fun jwtDecoder(): JwtDecoder {
    val jwtDecoder = NimbusJwtDecoder.withIssuerLocation(location)
        .validateTypes(true) (1)
        .jwtProcessorCustomizer {
            it.setJWSTypeVerifier(DefaultJOSEObjectTypeVerifier("JOSE"))
        }
        .build()
	jwtDecoder.setJwtValidator(DelegatingOAuth2TokenValidator(
        JwtTimestampValidator(), JwtIssuerValidator(location))) (2)
    return jwtDecoder
}
1 - 将 Nimbus 类型验证保留为 on
2 - 指定您需要的验证者列表,不包括JwtTypeValidator

有关其他指导,请参阅参考中的 JwtDecoder 验证程序部分。spring-doc.cadn.net.cn

将为您编码不透明的令牌凭证

为了更紧密地遵守 Introspection RFC,Spring Security 的不透明令牌支持将在创建授权标头之前对客户端 ID 和密钥进行编码。 此更改意味着您不再需要自己对客户端 ID 和 secret 进行编码。spring-doc.cadn.net.cn

如果您的客户端 ID 或密钥包含 URL 不安全的字符,则您可以通过执行以下作来为此更改做好准备:spring-doc.cadn.net.cn

替换 的用法introspectionClientCredentials

由于 Spring Security 现在可以为您进行编码,因此将introspectionClientCredentials通过发布以下内容@Bean:spring-doc.cadn.net.cn

@Bean
OpaqueTokenIntrospector introspector() {
	return SpringOpaqueTokenIntrospector.withIntrospectionUri(introspectionUri)
            .clientId(unencodedClientId).clientSecret(unencodedClientSecret).build();
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
    return SpringOpaqueTokenIntrospector.withIntrospectionUri(introspectionUri)
            .clientId(unencodedClientId).clientSecret(unencodedClientSecret).build()
}

以上将是 7.0 中的默认值。spring-doc.cadn.net.cn

如果此设置给您带来麻烦或您暂时无法应用它,您可以使用RestOperationsconstructor 代替:spring-doc.cadn.net.cn

@Bean
OpaqueTokenIntrospector introspector() {
	RestTemplate rest = new RestTemplate();
	rest.addInterceptor(new BasicAuthenticationInterceptor(encodedClientId, encodedClientSecret));
	return new SpringOpaqueTokenIntrospector(introspectionUri, rest);
}
@Bean
fun introspector(): OpaqueTokenIntrospector {
	val rest = RestTemplate()
	rest.addInterceptor(BasicAuthenticationInterceptor(encodedClientId, encodedClientSecret))
	return SpringOpaqueTokenIntrospector(introspectionUri, rest)
}