异常:org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean ‘myrbacService’
在springboot中添加了springsecurity,在配置权限的时候发生了这个异常
@Override
public void config(ExpressionUrlAuthorizationConfigurer<HttpSecurity>.ExpressionInterceptUrlRegistry config) {
config.anyRequest().access("@myrbacService.hasPermission(request, authentication)");
}
原因是这个bean需要加在上下文对象中,只需添加2个配置即可
@Bean
public OAuth2WebSecurityExpressionHandler oAuth2WebSecurityExpressionHandler(ApplicationContext applicationContext) {
OAuth2WebSecurityExpressionHandler expressionHandler = new OAuth2WebSecurityExpressionHandler();
expressionHandler.setApplicationContext(applicationContext);
return expressionHandler;
}
在ResourceServerConfigurerAdapter
的实现类中配置如下
@Autowire
private OAuth2WebSecurityExpressionHandler expressionHandler;
@Override
public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
resources.expressionHandler(expressionHandler);
}
具体可查看https://github.com/spring-projects/spring-security-oauth/issues/730#issuecomment-219480394