【问题标题】:How to extract claims from Spring Security OAuht2 Boot in the Resource Server?如何从资源服务器中的 Spring Security OAuht2 Boot 中提取声明?
【发布时间】:2019-11-10 16:37:33
【问题描述】:

我有一个使用Identity Server 4 内置在.Net Core 中的授权服务器!它按预期工作,从 Node Js 和 .Net 授权客户端和资源。现在我正在尝试添加一个 Java spring Boot 2 API (jdk 1.8) 作为受保护的资源。我通过使用OAuth2 Boot Documentation 实现了这个目标!到目前为止一切正常。现在,我需要从授权服务器生成的访问令牌中提取声明。这是 JWT 类型的不记名令牌。我对此的实现如下:

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
  public String resourceId;

  @Autowired
  public SecurityConfiguration(@Value("${security.oauth2.resource.id}") String resourceId) {
    this.resourceId = resourceId;
  }

@Override
  public void configure(ResourceServerSecurityConfigurer resources) {
    resources.resourceId(this.resourceId);
}

  @Override
  public void configure(HttpSecurity httpSecurity) throws Exception {
    httpSecurity
        .csrf()
        .disable()
        .authorizeRequests()
        .antMatchers("/swagger-ui.html", "/webjars/**", "/swagger-resources/**", "/**/api-docs/**", "/actuator/**")
        .permitAll()
        .and()
        .authorizeRequests().anyRequest().fullyAuthenticated();
  }

问题是当我尝试访问控制器内的声明时,它们不可用。我已经在 Spring Security 内部检查了 DefaultAccessTokenConverter 中的默认 extractAuthentication 方法,实际上它忽略了所有非默认声明。我想到的是创建一个扩展 DefaultAccessToken 转换器的新转换器,如下所示:

@Component
public class CustomAccessTokenConverter extends DefaultAccessTokenConverter {

  @Override
  public OAuth2Authentication extractAuthentication(Map<String, ?> claims) {
    OAuth2Authentication authentication = super.extractAuthentication(claims);
    authentication.setDetails(claims);
    return authentication;
  }
}

但我还没有弄清楚在哪里注入或引用这个新转换器。

【问题讨论】:

标签: java spring spring-boot spring-security spring-security-oauth2


【解决方案1】:

不幸的是,Spring Boot auto-configuration 似乎没有提供替代 DefaultAccessTokenConverter 的方法,DefaultAccessTokenConverterRemoteTokenServices 中的默认令牌转换器。要替换转换器,您必须替换默认创建的RemoteTokenServices

如果你的转换器是一个 bean,你可以在你自己的 RemoteTokenServices 对象上设置它,然后你可以在 ResourceServerSecurityConfigurer 上设置它(这样它就可以在幕后应用于OAuth2AuthenticationManager):

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends ResourceServerConfigurerAdapter {
    // ...

    @Autowired
    private ResourceServerProperties resource;

    @Autowired
    private CustomAccessTokenConverter customConverter;

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.tokenServices(customTokenServices());
        // ..
    }

    private RemoteTokenServices customTokenServices() {
        RemoteTokenServices services = new RemoteTokenServices();
        services.setAccessTokenConverter(this.customConverter);

        // configure based on .properties file 
        services.setCheckTokenEndpointUrl(this.resource.getTokenInfoUri());
        services.setClientId(this.resource.getClientId());
        services.setClientSecret(this.resource.getClientSecret());

        return services;
    }

    // ..


【讨论】:

    猜你喜欢
    • 2020-05-18
    • 2011-04-17
    • 1970-01-01
    • 2013-12-21
    • 2017-02-13
    • 2017-12-15
    • 2019-11-27
    • 2020-11-13
    • 2016-05-04
    相关资源
    最近更新 更多