此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 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