programing

베어러TokenAccessDeniedHandler 클래스 정의를 찾을 수 없습니다.

lovejava 2023. 10. 11. 20:21

베어러TokenAccessDeniedHandler 클래스 정의를 찾을 수 없습니다.

스프링 부트 2.1.1과 스프링 sec 5를 OAuth2 리소스 서버로 사용하여 데모 프로젝트를 시도하고 있지만 다음을 실행하려고 합니다.

ENV

  • 스프링 부트 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'이 예외를 던졌습니다. 중첩 예외는 java.lang입니다.NoClassDefFFoundError: 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')
}

추가할 때 예외가 사라졌습니다.spring-boot-starter-oauth2-resource-server의존

나도.

하지만 저는 그것을 발견했습니다.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

활성화하려면oauth2ResourceServerSpring Security 5.3.x를 기다려야 할 수도 있습니다.

아마도 차세대 oauth-2-0-support-with-spring-security와 관련이 있을 것입니다.

저는 다른 답변들에 찬성표를 던졌지만, build.gradle 파일에 "implementation"을 사용했습니다.

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 토론용

저는 같은 문제에 직면해 있고 제가 찾은 해결책이 누군가에게 도움이 되기를 바랍니다.

spring-security-oauth2를 사용하고 Resource 서버를 사용 가능으로 설정할 경우 다음을 사용합니다.WebSecurityConfigurerAdapter엔드포인트 보안 구성의 경우 및 메서드를 재정의합니다.public void configure(HttpSecurity security) throws Exception{}

언급URL : https://stackoverflow.com/questions/53919562/bearertokenaccessdeniedhandler-class-definition-not-found