Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

SEC-2136: Injecting Bean in a class used in custom PermissionEvaluator leads to NoSuchBeanDefinitionException #2362

spring-issuemaster opened this Issue Feb 25, 2013 · 4 comments


None yet
2 participants

Thomas Struntz (Migrated from SEC-2136) said:

Autowiring a bean in a class used in a custom PermissionEvaluator leads to NoSuchBeanDefinitionException. If the @autowire annotation is commented in code, the context loads correctly (but tests obviously then fail).

This is similar to referenced post in spring forums.

public class TestEntityPermission extends AbstractPermission {

    @Autowired // comment this and context loads
    private TestEntityService testEntityService;

    public boolean isAllowed(Authentication authentication, Object targetDomainObject,
            String permissionType) {

public class DefaultPermissionEvaluator implements PermissionEvaluator {

    private Map<String, Permission> permissionNameToPermissionMap = new HashMap<>();

    private boolean checkPermissionByDomainObject(Authentication authentication,
            Object targetDomainObject, String permissionValue) {
        String permissionType = getPermissionType(permissionValue);
        String permissionName = getPermissionTarget(permissionValue);
        Permission permission = permissionNameToPermissionMap.get(permissionName);
        return permission.isAllowed(authentication, targetDomainObject, permissionType);


Attached is a sample App (maven) with the according test case.

Rob Winch said:

This issue is actually a Spring Data issue. See DATACMNS-292

Rob Winch said:

Simplified sample project demonstrating the issue with and without Spring Security

Rob Winch said:

After some feedback from olivergierke there seems to be little that Spring Data can do to prevent the issue. Specifically, Spring Data cannot make the EntityManager lazy because it needs to use it to validate the queries annotated. There are some things we can do on the Spring Security side to help prevent issues like this which have been integrated into master. Please see commit 914ec45

In the meantime, to work around this issue, you can proxy your permissionEvaluator using LazyInitTargetSource as shown below:

<bean id="permissionEvaluator" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="targetSource">
        <bean class="org.springframework.aop.target.LazyInitTargetSource">
            <property name="targetBeanName" value="defaultPermissionEvaluator" />
    <property name="proxyInterfaces" value="org.springframework.security.access.PermissionEvaluator" />
<bean id="defaultPermissionEvaluator"
    <constructor-arg index="0">
        <map key-type="java.lang.String"
            <entry key="TestEntity" value-ref="testEntityPermission"/>

@spring-issuemaster spring-issuemaster added this to the 3.2.0.M2 milestone Feb 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment