![](/img/trans.png)
[英]EL1057E: No bean resolver registered in the context to resolve access to bean
[英]No bean resolver registered in the context to resolve access to bean
我正在嘗試使用 Java Config 實現方法安全性,但出現錯誤:-
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 類定義是 (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,而且我發現默認的 hasPermission() 方法在我實現它們時正在工作,唯一的問題是從 SpEL 讀取 bean。 我不確定出了什么問題。 任何指針?
我正在使用 Spring 4.1.5 和 Spring security 3.2.7
您需要確保在 DefaultMethodSecurityExpresssionHandler 上設置 ApplicationContext。 例如:
@Autowired
private ApplicationContext context;
// ...
@Override
protected MethodSecurityExpressionHandler expressionHandler() {
DefaultMethodSecurityExpressionHandler expressionHandler =
new DefaultMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(appPermissionEvaluator());
// !!!
expressionHandler.setApplicationContext(context);
return expressionHandler;
}
或者更簡潔地,如果您將單個 PermissionEvaluator 定義為 Bean,Spring Security 將自動選擇它(無需覆蓋 expressionHandler())。 例如:
@Bean
public PermissionEvaluator appPermissionEvaluator() {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.