简体   繁体   English

上下文中没有注册 bean 解析器来解析对 bean 的访问

[英]No bean resolver registered in the context to resolve access to bean

I'm trying to implement method security using Java Config, but I'm getting a error:-我正在尝试使用 Java Config 实现方法安全性,但出现错误:-

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

The method is:-方法是:-

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

The Config class definition is (MethodSecurityConfig.java):- Config 类定义是 (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;
    }

    ...
}

I checked that I'm able to autowire the bean in the same class, also I found the default hasPermission() methods are working as I've implemented them, the only problem is reading the bean from SpEL.我检查了我是否能够在同一个类中自动装配 bean,而且我发现默认的 hasPermission() 方法在我实现它们时正在工作,唯一的问题是从 SpEL 读取 bean。 I'm not sure what's wrong.我不确定出了什么问题。 Any Pointers?任何指针?

I'm using Spring 4.1.5 and Spring security 3.2.7我正在使用 Spring 4.1.5 和 Spring security 3.2.7

You need to ensure that you set the ApplicationContext on the DefaultMethodSecurityExpresssionHandler.您需要确保在 DefaultMethodSecurityExpresssionHandler 上设置 ApplicationContext。 For example:例如:

@Autowired
private ApplicationContext context;

// ...

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

    // !!!
    expressionHandler.setApplicationContext(context);

    return expressionHandler;
}

Alternatively and more concisely, if you define a single PermissionEvaluator as a Bean and Spring Security will automatically pick it up (no need to override expressionHandler()).或者更简洁地,如果您将单个 PermissionEvaluator 定义为 Bean,Spring Security 将自动选择它(无需覆盖 expressionHandler())。 For example:例如:

@Bean
public PermissionEvaluator appPermissionEvaluator() {
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EL1057E: 没有在上下文中注册的 bean 解析器来解析对 bean 的访问 - EL1057E: No bean resolver registered in the context to resolve access to bean Spring Boot 3.0/Security 6.0 迁移 - “EL1057E:没有在上下文中注册的 bean 解析器来解析对 bean 的访问......”在 SecurityFilterChain 中 - Spring Boot 3.0/Security 6.0 Migration - "EL1057E: No bean resolver registered in the context to resolve access to bean..." in SecurityFilterChain 从 Spring 上下文获取 bean(以编程方式注册)? - Get a bean (programmatically registered) from Spring context? Spring应用程序上下文重新加载不会更新DelegatingFilterProxy注册的bean - Spring application context reloading doesn't update bean registered by DelegatingFilterProxy @RestController bean在根上下文中注册,尽管未包含在excludeFilters中 - @RestController bean registered in root context, despite being excluded in excludeFilters 如何从另一个上下文访问bean - How to access bean from another context 骆驼标准化豆未注册 - Camel normalizer bean not registered 以编程方式解决 Bean 注入问题 - Resolve Bean Injection Programmatically 无法解析bean driverClassName - Cannot resolve bean driverClassName 应用上下文bean - Application context bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM