spring security 中异常 org.springframework.expression.spel.SpelEvaluationException

异常: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


版权声明:本文为wkztselina原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。