복붙노트

[SPRING] Bean에 대한 액세스를 해결하기 위해 컨텍스트에 등록 된 Bean 확인자가 없습니다.

SPRING

Bean에 대한 액세스를 해결하기 위해 컨텍스트에 등록 된 Bean 확인자가 없습니다.

자바 구성을 사용하여 메서드 보안을 구현하려고하지만 오류가 발생합니다.

org.springframework.expression.spel.SpelEvaluationException: EL1057E:(pos 1): No bean resolver registered in the context to resolve access to bean 'appPermissionEvaluator'

방법은 다음과 같습니다.

@PreAuthorize("@appPermissionEvaluator.hasSystemPermission()")
public String something() {
    ...
}

구성 클래스 정의는 (MethodSecurityConfig.java)입니다. -

@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {

    @Bean
    public AppPermissionEvaluator appPermissionEvaluator() {
        return new AppPermissionEvaluator();
    }

    @Override
    protected MethodSecurityExpressionHandler createExpressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler =
                new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
        return expressionHandler;
    }

    ...
}

나는 같은 클래스에서 bean을 autowire 할 수 있는지 검사했다. 또한 hasPermission () 메소드가 구현 된 것처럼 동작한다는 것을 발견했다. 유일한 문제는 SpEL에서 bean을 읽는 것이다. 나는 틀린 것이 확실하지 않다. 어떤 포인터?

나는 스프링 4.1.5와 스프링 보안 3.2.7을 사용하고있다.

해결법

  1. ==============================

    1.DefaultMethodSecurityExpresssionHandler에 ApplicationContext를 설정해야합니다. 예 :

    DefaultMethodSecurityExpresssionHandler에 ApplicationContext를 설정해야합니다. 예 :

    @Autowired
    private ApplicationContext context;
    
    @Override
    protected MethodSecurityExpressionHandler expressionHandler() {
        DefaultMethodSecurityExpressionHandler expressionHandler =
                new DefaultMethodSecurityExpressionHandler();
        expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
        handler.setApplicationContext(context);
        return handler;
    }
    

    더 간단하게 말하자면, 하나의 PermissionEvaluator를 Bean으로 정의하고 Spring Security가 자동으로 선택하면 (expressionHandler ()를 오버라이드 할 필요가 없다). 예 :

    @Bean
    public PermissionEvaluator appPermissionEvaluator() {
        ...
    }
    
  2. from https://stackoverflow.com/questions/29328124/no-bean-resolver-registered-in-the-context-to-resolve-access-to-bean by cc-by-sa and MIT license