此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Security 6.4.5! |
OAuth 2.0 更改
驗證typ
Header 替换为JwtTypeValidator
NimbusJwtDecoder
在 Spring Security 7 中将移动typ
标头验证设置为JwtTypeValidator
而不是依赖 Nimbus。
这使它与NimbusJwtDecoder
验证声明,而不是依赖 Nimbus 来验证它们。
如果要在jwtProcessorCustomizer
方法,那么您应该将其移动到JwtTypeValidator
或OAuth2TokenValidator
你自己的。
要检查您是否已准备好进行此更改,请添加默认的JwtTypeValidator
添加到您的验证者列表中,因为默认情况下这将包含在 7 中:
-
Java
-
Kotlin
@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 | - 添加默认的typ validator (默认情况下,这将包含在 7 中) |
请注意,默认值验证typ
值要么是JWT
或 not present,这与 Nimbus 默认值相同。
它还与 RFC 7515 保持一致,后者规定typ
是可选的。
我正在使用DefaultJOSEObjectTypeVerifier
如果您的配置中有类似以下内容的内容:
-
Java
-
Kotlin
@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
}
然后将其更改为:
-
Java
-
Kotlin
@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
}
要指示typ
header 是可选的,请使用#setAllowEmpty(true)
(这相当于包括null
在DefaultJOSEObjectTypeVerifier
).
我想选择退出
如果你想继续按照你现在的方式做事,那么步骤是相似的,只是相反:
-
Java
-
Kotlin
@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 验证程序部分。