Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从资源服务器中的Spring Security OAuht2启动中提取声明?_Java_Spring_Spring Boot_Spring Security_Spring Security Oauth2 - Fatal编程技术网

Java 如何从资源服务器中的Spring Security OAuht2启动中提取声明?

Java 如何从资源服务器中的Spring Security OAuht2启动中提取声明?,java,spring,spring-boot,spring-security,spring-security-oauth2,Java,Spring,Spring Boot,Spring Security,Spring Security Oauth2,我有一个授权服务器内置在.Net核心使用!它按照预期工作,授权来自Node Js和.Net的客户端和资源。现在,我尝试添加一个JavaSpringBoot2API(JDK1.8)作为受保护的资源。我通过使用!到目前为止一切正常。现在,我需要从授权服务器生成的访问令牌中提取声明。这是JWT类型的承载令牌。我为此采取的措施如下: @Configuration @EnableResourceServer @EnableGlobalMethodSecurity(prePostEnabled = true

我有一个授权服务器内置在.Net核心使用!它按照预期工作,授权来自Node Js和.Net的客户端和资源。现在,我尝试添加一个JavaSpringBoot2API(JDK1.8)作为受保护的资源。我通过使用!到目前为止一切正常。现在,我需要从授权服务器生成的访问令牌中提取声明。这是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;
  }
}
@组件
公共类CustomAccessTokenConverter扩展了DefaultAccessTokenConverter{
@凌驾
公共OAuth2Authentication extractAuthentication(映射声明){
OAuth2AuthenticationAuthentication=super.extractAuthentication(声明);
认证。详细信息(声明);
返回认证;
}
}
但是我还没有找到注入或引用这个新转换器的位置。

不幸的是,似乎没有提供一种方法来替换
DefaultAccessTokenConverter
,它是
RemoteTokenServices
中的默认令牌转换器。要替换转换器,您必须替换默认创建的
RemoteTokenServices

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


当前的实现适用于jwt或不透明令牌。我想保留这个特性,您可以在这里阅读有关SpringSecurityJWTs的内容
@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;
    }

    // ..