上下文中没有注册 bean 解析器来解析对 bean 的访问
No bean resolver registered in the context to resolve access to bean
我正在尝试使用 Java 配置实现方法安全性,但出现错误:-
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() {
...
}
Config class 定义为 (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;
}
...
}
我检查过我能够在同一个 class 中自动装配 bean,我还发现默认的 hasPermission() 方法在我实现它们时工作,唯一的问题是读取 bean来自 SpEL。我不确定出了什么问题。有什么指点吗?
我正在使用 Spring 4.1.5 和 Spring 安全 3.2.7
您需要确保在 DefaultMethodSecurityExpressionHandler 上设置了 ApplicationContext。例如:
@Autowired
private ApplicationContext context;
// ...
@Override
protected MethodSecurityExpressionHandler expressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
// !!!
expressionHandler.setApplicationContext(context);
return expressionHandler;
}
或者更简洁,如果您将单个 PermissionEvaluator 定义为 Bean,并且 Spring 安全性将自动选择它(无需覆盖 expressionHandler())。例如:
@Bean
public PermissionEvaluator appPermissionEvaluator() {
...
}
我正在尝试使用 Java 配置实现方法安全性,但出现错误:-
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() {
...
}
Config class 定义为 (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;
}
...
}
我检查过我能够在同一个 class 中自动装配 bean,我还发现默认的 hasPermission() 方法在我实现它们时工作,唯一的问题是读取 bean来自 SpEL。我不确定出了什么问题。有什么指点吗?
我正在使用 Spring 4.1.5 和 Spring 安全 3.2.7
您需要确保在 DefaultMethodSecurityExpressionHandler 上设置了 ApplicationContext。例如:
@Autowired
private ApplicationContext context;
// ...
@Override
protected MethodSecurityExpressionHandler expressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
// !!!
expressionHandler.setApplicationContext(context);
return expressionHandler;
}
或者更简洁,如果您将单个 PermissionEvaluator 定义为 Bean,并且 Spring 安全性将自动选择它(无需覆盖 expressionHandler())。例如:
@Bean
public PermissionEvaluator appPermissionEvaluator() {
...
}