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-projects-issues opened this issue Feb 25, 2013 · 4 comments
Assignees
Labels
in: config An issue in spring-security-config type: bug A general bug type: jira An issue that was migrated from JIRA
Milestone

Comments

@spring-projects-issues
Copy link

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-projects-issues
Copy link
Author

Rob Winch said:

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

@spring-projects-issues
Copy link
Author

Rob Winch said:

Simplified sample project demonstrating the issue with and without Spring Security

@spring-projects-issues
Copy link
Author

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-projects-issues
Copy link
Author

This issue duplicates https://jira.spring.io/browse/DATACMNS-292
This issue relates to https://jira.spring.io/browse/SPR-10353

@rwinch rwinch added the in: config An issue in spring-security-config label May 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: config An issue in spring-security-config type: bug A general bug type: jira An issue that was migrated from JIRA
Projects
None yet
Development

No branches or pull requests

2 participants