Spring Security
  1. Spring Security
  2. SEC-2136

Injecting Bean in a class used in custom PermissionEvaluator leads to NoSuchBeanDefinitionException

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Fixed
    • Affects Version/s: 3.1.3
    • Fix Version/s: 3.2.0.M2
    • Component/s: Namespace
    • Labels:
      None
    • Environment:
      Windows 7 32.bit
      JDK 7
      Netbeans 7.2.1

      Description

      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.

        Issue Links

          Activity

          Hide
          Rob Winch added a comment -

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

          Show
          Rob Winch added a comment - This issue is actually a Spring Data issue. See DATACMNS-292
          Hide
          Rob Winch added a comment -

          Simplified sample project demonstrating the issue with and without Spring Security

          Show
          Rob Winch added a comment - Simplified sample project demonstrating the issue with and without Spring Security
          Hide
          Rob Winch added a comment -

          After some feedback from Oliver Gierke 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>
          
          Show
          Rob Winch added a comment - After some feedback from Oliver Gierke 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>

            People

            • Assignee:
              Rob Winch
              Reporter:
              Thomas Struntz
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: