BearerTokenAccessDeniedHandler 类定义 未找到

我正在尝试使用spring boot 2.1.1和spring sec 5的演示项目,作为OAuth2资源服务器,但是当我尝试运行以下内容时

环境网络

  • 弹簧靴 2.1.1 发布
  • 弹簧安全核心 5.1.2

  • 爪哇 8

法典

    @RestController
    @SpringBootApplication
   //  @EnableResourceServer
    public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
    @GetMapping("/hello")
    public String sayHello() {
    return "Hello World";
    }

    @Configuration
    static class MyWebSecurityConfigurerAdapter extends 
    WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests().anyRequest().authenticated()
                .and()
                .oauth2ResourceServer().jwt(); // <--- throws error

        }
      }

    }

这会引发错误

工厂方法'springSecurityFilTerChain'抛出异常;nested exception is java.lang.NoClassDefFoundError: org/springframework/security/oauth2/server/resource/web/access/BearerTokenAccessDeniedHandler

我的依赖项如下所示

dependencies {

implementation('org.springframework.boot:spring-boot-starter-web')
implementation('org.springframework.boot:spring-boot-starter-security')
implementation(group: 'org.springframework.security.oauth.boot', name: 'spring-security-oauth2-autoconfigure', version: '2.1.1.RELEASE')

implementation(group: 'org.springframework.security.oauth', name: 'spring-security-oauth2', version: '2.3.4.RELEASE')
}

答案 1

当我添加依赖项时,异常消失了spring-boot-starter-oauth2-resource-server


答案 2

我也是。

但我在org.springframework.boot:spring-boot-starter-oauth2-resource-server


顺便说一句,我导入了软件包并使用:org.springframework.boot:spring-boot-starter-oauth2-resource-server

        http
                .authorizeRequests()
                .antMatchers("/**").hasAnyRole("admin")
                .anyRequest().authenticated();
                //.and()
                //.oauth2ResourceServer() don't use it,
                //.jwt();
                // Do not use it, otherwise you must define 
                // jwtDecoderByIssuerUri or jwtDecoderByJwkKeySetUri

如果你想启用,也许你需要等待Spring Security 5.3.x。oauth2ResourceServer

也许它与下一代oauth-2-0-支持弹簧安全性有关


推荐