spring-projects / spring-security

Spring Security
http://spring.io/projects/spring-security
Apache License 2.0
8.79k stars 5.9k forks source link

When use custom Web Security Expressions meat exception:SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean #4950

Closed alexli0707 closed 6 years ago

alexli0707 commented 6 years ago

Summary

Thanks for helping first. When I using Spring Security in Spirng Boot and set Web Security Expressions according to project doc

Actual Behavior

I meat this exception:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1057E: No bean resolver registered in the context to resolve access to bean 'rbacService'

Expected Behavior

as doc have a happy ending like this

http.authorizeRequests()
            .antMatchers("/user/**").access("@webSecurity.check(authentication,request)")

Configuration

Here is my code,I try use @Bean as method and @Service("rbacService") both meat the same exception.

http
                .authorizeRequests()
                .antMatchers("/css/**", "/js/**", "/fonts/**").permitAll()  
                .antMatchers("/", "/login", "/register").permitAll()
                .anyRequest().access("@rbacService.hasPermission(authentication,request)")
                .expressionHandler(webExpressionHandler())
@Service("rbacService")
public class RbacServiceImpl implements RBACService {

    @Autowired
    AdminUserService adminUserService;

    @Override
    public boolean hasPermission(Authentication authentication,HttpServletRequest request) {
        // do something dynamic pair the url and check permission
        return hasPermission;
    }
}

Version

spring-security-config:4.2.2RELEASE spring-security-core:4.2.2RELEASE spring-security-web:4.2.2RELEASE spring-boot-starter-parent:1.5.9RELEASE

rwinch commented 6 years ago

It looks like you are providing a custom webExpressionHandler() You need to be sure that if it is a DefaultWebSecurityExpressionHandler that it is injected with an ApplicationContext which is how it resolves the beans.

I'm going to close this, but feel free to reopen if this doesn't resolve your issue