Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

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

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link

commented Feb 25, 2013

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);
        verifyPermissionIsDefined(permissionName);
        Permission permission = permissionNameToPermissionMap.get(permissionName);
        return permission.isAllowed(authentication, targetDomainObject, permissionType);
    }

}

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

@spring-issuemaster

This comment has been minimized.

Copy link
Author

commented Feb 26, 2013

Rob Winch said:

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

@spring-issuemaster

This comment has been minimized.

Copy link
Author

commented Feb 26, 2013

Rob Winch said:

Simplified sample project demonstrating the issue with and without Spring Security

@spring-issuemaster

This comment has been minimized.

Copy link
Author

commented Feb 28, 2013

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" />
        </bean>
    </property>
    <property name="proxyInterfaces" value="org.springframework.security.access.PermissionEvaluator" />
</bean>
<bean id="defaultPermissionEvaluator"
      class="permissionevaluatorbug.security.DefaultPermissionEvaluator">
    <constructor-arg index="0">
        <map key-type="java.lang.String"
             value-type="permissionevaluatorbug.security.Permission">
            <entry key="TestEntity" value-ref="testEntityPermission"/>
        </map>
    </constructor-arg>
</bean>
@spring-issuemaster

This comment has been minimized.

Copy link
Author

commented Feb 6, 2016

@rwinch rwinch added the in: config label May 3, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.