之前发生在我身上,我发现解决它的一种方法是将角色注入令牌声明,然后指示 Spring Security 提取它们。我在这里详细写过。文档解释了第一部分,但要点是这个 cURL 片段:
curl -X PUT "https://$REGION.appid.cloud.ibm.com/management/v4/$TENANT_ID/config/tokens" -H 'Content-Type: application/json' -H "Authorization: Bearer $IAM_TOKEN" -d '{
"access": {
"expires_in": 3600
},
"refresh": {
"enabled": true,
"expires_in": 2592001
},
"anonymousAccess": {
"enabled": false
},
"accessTokenClaims": [
{
"source": "roles"
}
],
"idTokenClaims": [
{
"source": "saml",
"sourceClaim": "attributes.uid"
}
]
}'
您也可以在Swagger UI中执行此操作。但请注意,这是一个 PUT 请求,因此它将覆盖您之前的任何配置。理想情况下,运行 GET 请求以获取当前配置,然后将声明添加到其中以避免出现问题。
然后,在 SecurityConfiguration 中,添加这个 JWT 转换器:
protected void configure(HttpSecurity http) throws Exception {
http
//...
.oauth2ResourceServer()
.jwt()
.jwtAuthenticationConverter(jwtAuthenticationConverter());
}
Converter jwtAuthenticationConverter() {
JwtGrantedAuthoritiesConverter converter = new JwtGrantedAuthoritiesConverter();
converter.setAuthoritiesClaimName("authorities");
converter.setAuthorityPrefix(""); //so that the role has the same name as the one that comes from App ID
JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(converter);
return jwtAuthenticationConverter;
}
现在 Spring Security 识别了角色,您可以使用注释或 antMatcher 配置来保护端点:
.antMatchers("/api/admin").hasRole("ADMIN")