Java Spring boot Oauth2 JWT作用域不足错误

Java Spring boot Oauth2 JWT作用域不足错误,java,spring-boot,jwt,spring-security-oauth2,Java,Spring Boot,Jwt,Spring Security Oauth2,我试图在spring boot应用程序中使用oauth创建一个授权和资源服务器,但我似乎无法为它获得授权 Spring引导:1.5.10.RELEASE、Java 8、Spring-security-oauth2:2.3.4.RELEASE和Spring-security jwt:1.1.0.RELEASE 我已经在同一个应用程序中设置了授权和资源服务器,其配置如下 授权服务器 @Configuration @EnableAuthorizationServer public class OAu

我试图在spring boot应用程序中使用oauth创建一个授权和资源服务器,但我似乎无法为它获得授权

Spring引导:1.5.10.RELEASE、Java 8、Spring-security-oauth2:2.3.4.RELEASE和Spring-security jwt:1.1.0.RELEASE


我已经在同一个应用程序中设置了授权和资源服务器,其配置如下

授权服务器

@Configuration
@EnableAuthorizationServer
public class OAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private DataSource dataSource;

    private CustomTokenEnhancer customTokenEnhancer;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource)
                .passwordEncoder(passwordEncoder())
                .clients(clientDetailsService());
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.passwordEncoder(passwordEncoder());
        security.checkTokenAccess("isAuthenticated()");
        security.tokenKeyAccess("permitAll()");
        super.configure(security);
    }

    @Override
    public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        enhancerChain.setTokenEnhancers(Arrays.asList(customTokenEnhancer, jwtAccessTokenConverter()));
        endpoints
                .tokenStore(tokenStore())
                .tokenEnhancer(enhancerChain)
                .reuseRefreshTokens(false);
    }
}
@Configuration
@EnableResourceServer
@EnableWebSecurity
public class OAuthResourceConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/oauth/token", "/oauth/authorize**", "/oauth/check_token")
                .permitAll();
        http.csrf().disable().requestMatchers().antMatchers("/test**")
                .and().authorizeRequests()
                .antMatchers(HttpMethod.POST, "/test**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.GET, "/test**").access("#oauth2.hasScope('read')");
    }
}
资源服务器

@Configuration
@EnableAuthorizationServer
public class OAuthServerConfiguration extends AuthorizationServerConfigurerAdapter {

    private DataSource dataSource;

    private CustomTokenEnhancer customTokenEnhancer;

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.jdbc(dataSource)
                .passwordEncoder(passwordEncoder())
                .clients(clientDetailsService());
    }

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        security.passwordEncoder(passwordEncoder());
        security.checkTokenAccess("isAuthenticated()");
        security.tokenKeyAccess("permitAll()");
        super.configure(security);
    }

    @Override
    public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
        TokenEnhancerChain enhancerChain = new TokenEnhancerChain();
        enhancerChain.setTokenEnhancers(Arrays.asList(customTokenEnhancer, jwtAccessTokenConverter()));
        endpoints
                .tokenStore(tokenStore())
                .tokenEnhancer(enhancerChain)
                .reuseRefreshTokens(false);
    }
}
@Configuration
@EnableResourceServer
@EnableWebSecurity
public class OAuthResourceConfiguration extends ResourceServerConfigurerAdapter {

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.csrf().disable().authorizeRequests()
                .antMatchers("/oauth/token", "/oauth/authorize**", "/oauth/check_token")
                .permitAll();
        http.csrf().disable().requestMatchers().antMatchers("/test**")
                .and().authorizeRequests()
                .antMatchers(HttpMethod.POST, "/test**").access("#oauth2.hasScope('write')")
                .antMatchers(HttpMethod.GET, "/test**").access("#oauth2.hasScope('read')");
    }
}

我正在使用
client\u凭证
grant类型从
/oauth/token
端点发出新令牌,并成功生成新令牌

以下是生成的JWT

eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiKiJdLCJzY29wZSI6WyJyZWFkIiwid3JpdGUiXSwiZXhwIjoxNTc4ODE5MDM3LCJqdGkiOiIzNWViZDNiMi04ZTM4LTRkMDEtYTUzYi01NDU0NzI2NjE1ZDIiLCJ0ZW5hbnQiOiJjOSIsImNsaWVudF9pZCI6ImM5In0.G4BFfefKQEWSbvJFUwQ_tPDiHOSNMP9205iJPOv6-w4
如果我分析这个,我可以看到它有读写作用域

我在应用程序中定义了以下控制器

@PostMapping
@ResponseBody
@PreAuthorize("#oauth2.hasScope('write')")
public ResponseEntity<String> test(@RequestHeader(value = "Authorization") String token) {}
@PostMapping
@应答器
@预授权(#oauth2.hasScope('write'))
公共响应性测试(@RequestHeader(value=“Authorization”)字符串令牌){}
现在,当我尝试使用给定的令牌调用此API时,出现以下错误

error=“作用域不足”,error\u description=“此资源的作用域不足”,scope=“write”


我不明白问题出在哪里。

为什么要使用过时和不受支持的Spring版本?尤其是Spring安全模块——这不仅意味着你不太可能获得所需的帮助,而且还带来了相当大的安全风险……这是一个老项目。这可能是这个问题的原因吗?旧的东西不能成为缺乏修补策略的借口,更不能成为在生产中使用EOL库而不支持安全修补的借口@不过,这不是问题所在。即使在这个旧版本的Spring Boot中,范围检查也可以正常工作。“安全风险不是这个问题的主题。@Gimby这就是为什么它是一个评论而不是一个答案。我认为指出这样的安全问题很重要,以使OP知道它们可能不在哪里。就像一个关于不使用预处理语句的SQL语法的问题一样——例如。