【问题标题】:BearerTokenAccessDeniedHandler Class Definition Not foundBearerTokenAccessDeniedHandler 类定义 未找到
【发布时间】:2019-05-23 23:29:34
【问题描述】:

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

环境

  • Spring Boot 2.1.1 发布
  • Spring 安全核心 5.1.2

  • Java 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”抛出异常;嵌套异常是 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')
}

【问题讨论】:

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


    【解决方案1】:

    我也是。

    但我在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
    

    如果要启用oauth2ResourceServer,可能需要等待Spring Security 5.3.x。

    可能和next-generation-oauth-2-0-support-with-spring-security有关

    【讨论】:

      【解决方案2】:

      添加spring-boot-starter-oauth2-resource-server依赖后异常消失

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,希望我找到的解决方案能对某人有所帮助。

        当您使用 spring-security-oauth2 并启用资源服务器时,使用 WebSecurityConfigurerAdapter 进行端点安全配置并覆盖方法 public void configure(HttpSecurity security) throws Exception{}

        【讨论】:

          【解决方案4】:

          我赞成其他答案,但我在 build.gradle 文件中使用了“实现”

          implementation 'org.springframework.security:spring-security-oauth2-resource-server'
          

          编译配置仍然存在,但不应使用,因为它不能提供 api 和实现配置提供的保证。

          以上引用来自: ((https://docs.gradle.org/current/userguide/java_library_plugin.html#sec:java_library_separation))

          下面是我的完整文件

          plugins {
              id 'java'
          }
          
          group 'com.mycompany.mything'
          version '1.0-SNAPSHOT'
          
          sourceCompatibility = 1.8
          
          
          repositories {
              mavenCentral()
          }
          
          apply plugin: 'org.springframework.boot'
          apply plugin: 'io.spring.dependency-management'
          
          
          dependencies {
          
          implementation 'org.springframework.boot:spring-boot-starter-data-rest'
          implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
          
          
          
          implementation 'org.springframework.boot:spring-boot-starter-security'
          //    implementation 'org.springframework.boot:spring-boot-starter-web'
          implementation 'org.springframework.security:spring-security-oauth2-resource-server'
          implementation 'org.springframework.security:spring-security-oauth2-jose'
          //    implementation 'org.springframework.security:spring-security-config'
          
          
          
          
          
          
          }
          

          另见

          https://medium.com/mindorks/implementation-vs-api-in-gradle-3-0-494c817a6fa

          用于实现与 api 的讨论

          【讨论】:

            猜你喜欢
            • 2023-03-20
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-05-11
            • 2015-12-09
            • 2016-03-12
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多