【问题标题】:How to enable OAuth on a specific endpoint using spring security如何使用 Spring Security 在特定端点上启用 OAuth
【发布时间】:2021-12-12 06:48:12
【问题描述】:

我正在尝试熟悉 Spring Security,特别是从 Spring Security OAuth 迁移到 Soring Security(如以下示例/指南 https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide 中所示)。

但是,我似乎只收到 403 Forbidden 错误。我正在从 Postman 访问,并且正在使用我公司现有的 OAuth 服务器。我能够从身份验证服务器获取令牌,因此我知道我的凭据正确,并且我已经验证了 OAuth 用户的角色。

我正在使用以下依赖项:

implementation 'org.springframework.boot:spring-boot-starter-oauth2-resource-server'
implementation 'org.springframework.boot:spring-boot-starter-web'

这是我尝试访问的简单端点:

@RestController
public class AppController
{
   @GetMapping("/hello")
    public String hello()
    {
        return "hello";
    }
}

这是我的 application.yml 文件:

spring:
  security:
    oauth2:
      resourceserver:
        jwt:
          jwk-set-uri: <<company-website-here>>/uaa/oauth/token_keys

这是我的安全配置类:

@Configuration
public class SecurityConfiguration extends WebSecurityConfigurerAdapter
{
    @Override
    protected void configure(HttpSecurity http) throws Exception
    {
        http.authorizeRequests()
                .antMatchers(HttpMethod.GET, "/hello").hasRole("MY_ROLE")
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt();
    }
}

我似乎无法弄清楚为什么我似乎只收到 403 错误。我也尝试将@EnableWebSecurity 添加到安全配置类,但这并没有什么不同。将身份验证服务器 URL 显式添加到服务器和/或手动创建 JwtDecoder 也没有成功;似乎 url 是根据其属性名称自动从 yml 文件中提取的。

我正在尝试放弃使用 org.springframework.security.oauth.boot 依赖项和 ResourceServerConfigurerAdapter。

我必须像这样添加自己的转换器:

private static class JwtAuthenticationConverter implements Converter<Jwt, AbstractAuthenticationToken>
    {
        private final Converter<Jwt, Collection<GrantedAuthority>> jwtGrantedAuthoritiesConverter;

        public JwtAuthenticationConverter()
        {
            this.jwtGrantedAuthoritiesConverter = jwt -> jwt
                    .getClaimAsStringList("authorities")
                    .stream()
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }

        @Override
        public final AbstractAuthenticationToken convert(@NonNull Jwt jwt)
        {
            Collection<GrantedAuthority> authorities = jwtGrantedAuthoritiesConverter.convert(jwt);
            return new JwtAuthenticationToken(jwt, authorities, jwt.getClaimAsString("client_id"));
        }
    }

然后必须将其添加到主安全配置中:

.jwtAuthenticationConverter(new JwtAuthenticationConverter());

【问题讨论】:

    标签: java spring-boot spring-security


    【解决方案1】:

    可能会发生一些事情。

    1. 当您迁移到 Spring Security 5 时,您可能需要手动提取权限。检查这个post and it's correct answer

    2. 您正在使用 hasRole 功能,这将在您的权限/角色之前附加“ROLE_”。因此,如果您的 JWT 令牌上的角色不是 ROLE_JWT_ROLE 您应该使用 hasTransaction

    【讨论】:

    • 看来你是对的。我必须创建自己的转换器,我编辑了自己的答案以反映我的所作所为
    猜你喜欢
    • 2017-09-25
    • 2015-04-22
    • 2013-05-28
    • 2020-01-16
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2021-09-11
    • 2017-09-17
    相关资源
    最近更新 更多