虽然我创建了一个customJwtAuthenticationConverter,但Spring boot未读取Auth0权限

问题描述 投票:0回答:1

在我的 Spring boot 项目中,我使用 Auth0

我需要从 Auth0 jwt 令牌读取权限,然后使用它们来评估 hasAuthority。

尽管我付出了一切努力,但我无法读取权限,尽管我创建了一个 customJwtAuthenticationConverter 来执行此操作

我使用了以下 JWT 令牌(我删除了一些字符,因此这里没有共享任何明智的数据):

{
  "iss": "https://dev-hxo7qtywv.eu.auth0.com/",
  "sub": "google-oauth2|10344498554237",
  "aud": [
    "https://test/api",
    "https://dev-hxo7qtywv.eu.auth0.com/userinfo"
  ],
  "iat": 17010717,
  "exp": 17019317,
  "azp": "dv1rEpS37p9jaGIPXO0S5fnjMh",
  "scope": "openid profile email offline_access",
  "permissions": [
    "read:admin",
    "SCOPE_read:admin"
  ]
}

这是我的 SecurityFilterChain 类内容的一部分

@Bean
  public JwtAuthenticationConverter customJwtAuthenticationConverter() {
    JwtGrantedAuthoritiesConverter grantedAuthoritiesConverter = new JwtGrantedAuthoritiesConverter();
    grantedAuthoritiesConverter.setAuthoritiesClaimName("permissions");
    JwtAuthenticationConverter jwtAuthenticationConverter = new JwtAuthenticationConverter();
    jwtAuthenticationConverter.setJwtGrantedAuthoritiesConverter(grantedAuthoritiesConverter);
    return jwtAuthenticationConverter;
  }

还有

      .requestMatchers("/api/test/admin").hasAuthority("read:admin")

还有

http.oauth2ResourceServer(oauth2 -> oauth2
        .jwt(jwt -> jwt.jwtAuthenticationConverter(customJwtAuthenticationConverter())));

我检查了控制器中的权限,我看到以下内容:

Authority: SCOPE_openid
Authority: SCOPE_profile
Authority: SCOPE_email
Authority: SCOPE_offline_access

知道为什么 Spring boot 不使用我的 customJwtAuthenticationConverter 吗?

spring-boot oauth-2.0 oauth auth0
1个回答
0
投票

找到解决方案。在最新的库中,您只能通过以下方式进行配置(在application.yml中)

okta:
    oauth2:
        groupsClaim: permissions
© www.soinside.com 2019 - 2024. All rights reserved.