views:

78

answers:

1

Hi guys, I currently running into trouble with spring security, I have been following the two last tutorials referenced on the spring security article page

Methods secured with the org.springframework.security.access.annotation.Secured don't seem to trigger any Spring Security logic.

This is my test file:

 public class AclServiceTest {

 @Autowired
 PersonDataOnDemand pdod;

 @Autowired
 MyAclService aclService;

 UserDetailsService uds = new MyUserDetailsService();

 @Test
 public void testWriteResourceAnnotation(){
  Person p0 = pdod.getSpecificPerson(0);
  Person p1 = pdod.getSpecificPerson(1);

  Assert.isTrue(!p0.getId().equals(p1.getId()));

  Resource r = new Resource(p0.getSite(), p0, p0.getPrivateFolder());

  authenticatePerson(p0);
  securedWriteResource(r);

  authenticatePerson(p1);
  try{
   securedWriteResource(r);
   fail();
  } catch(Exception e){

  }
 }


 @Secured("ACL_RESOURCE_WRITE")
 public void securedWriteResource(Resource r){
  return;  
 }

 private void authenticatePerson(Person p){
  UserDetails ud = uds.loadUserByUsername(p.getEmail());
  SecurityContextHolder.getContext().setAuthentication(new RunAsUserToken("user-"+p.getId(), ud, p.getPassword(), ud.getAuthorities().toArray(new GrantedAuthority[0]), null));
 }
}

If added the following lines to my web.xml

<!--Spring security filter-->
<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

And this is my security.xml configuration with the beans that are used:

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                    http://www.springframework.org/schema/security
                    http://www.springframework.org/schema/security/spring-security-3.0.xsd" >
<global-method-security secured-annotations="enabled" access-decision-manager-ref="businessAccessDecisionManager"/>

<beans:bean id="businessAccessDecisionManager" class="org.springframework.security.access.vote.UnanimousBased">
    <beans:property name="allowIfAllAbstainDecisions" value="false"/>
    <beans:property name="decisionVoters">
        <beans:list>
            <beans:ref local="roleVoter"/>
            <beans:ref local="aclResourceReadVoter"/>
            <beans:ref local="aclResourceWriteVoter"/>
            <beans:ref local="aclResourceDeleteVoter"/>
            <beans:ref local="aclResourceAdminVoter"/>
        </beans:list>
    </beans:property>
</beans:bean>

<beans:bean id="roleVoter" class="org.springframework.security.access.vote.RoleVoter"/>

<beans:bean id="aclResourceReadVoter" class="org.springframework.security.acls.AclEntryVoter">
    <beans:constructor-arg ref="jdbcMutableAclService"/>
    <beans:constructor-arg value="ACL_RESOURCE_READ"/>
    <beans:constructor-arg>
        <beans:list>
            <beans:ref local="administrationPermission"/>
            <beans:ref local="readPermission"/>
        </beans:list>
    </beans:constructor-arg>
    <beans:property name="processDomainObjectClass" value="myapp.models.Resource"/>
    <beans:property name="internalMethod" value="getRootFolder"/>
</beans:bean>

<beans:bean id="aclResourceWriteVoter" class="org.springframework.security.acls.AclEntryVoter">
    <beans:constructor-arg ref="jdbcMutableAclService"/>
    <beans:constructor-arg value="ACL_RESOURCE_WRITE"/>
    <beans:constructor-arg>
        <beans:list>
            <beans:ref local="administrationPermission"/>
            <beans:ref local="writePermission"/>
        </beans:list>
    </beans:constructor-arg>
    <beans:property name="processDomainObjectClass" value="myapp.models.Resource"/>
    <beans:property name="internalMethod" value="getRootFolder"/>
</beans:bean>

<beans:bean id="aclResourceDeleteVoter" class="org.springframework.security.acls.AclEntryVoter">
    <beans:constructor-arg ref="jdbcMutableAclService"/>
    <beans:constructor-arg value="ACL_RESOURCE_DELETE"/>
    <beans:constructor-arg>
        <beans:list>
            <beans:ref local="administrationPermission"/>
            <beans:ref local="deletePermission"/>
        </beans:list>
    </beans:constructor-arg>
    <beans:property name="processDomainObjectClass" value="myapp.models.Resource"/>
    <beans:property name="internalMethod" value="getRootFolder"/>
</beans:bean>

<beans:bean id="aclResourceAdminVoter" class="org.springframework.security.acls.AclEntryVoter">
    <beans:constructor-arg ref="jdbcMutableAclService"/>
    <beans:constructor-arg value="ACL_RESOURCE_ADMIN"/>
    <beans:constructor-arg>
        <beans:list>
            <beans:ref local="administrationPermission"/>
        </beans:list>
    </beans:constructor-arg>
    <beans:property name="processDomainObjectClass" value="myapp.models.Resource"/>
    <beans:property name="internalMethod" value="getRootFolder"/>
</beans:bean>


<beans:bean id="administrationPermission" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
    <beans:property name="staticField" value="org.springframework.security.acls.domain.BasePermission.ADMINISTRATION"/>
</beans:bean>

<beans:bean id="readPermission" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
     <beans:property name="staticField" value="org.springframework.security.acls.domain.BasePermission.READ"/>
</beans:bean>

<beans:bean id="writePermission" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
    <beans:property name="staticField" value="org.springframework.security.acls.domain.BasePermission.WRITE"/>
</beans:bean>

<beans:bean id="deletePermission" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
    <beans:property name="staticField" value="org.springframework.security.acls.domain.BasePermission.DELETE"/>
</beans:bean>

The businessAccessDecisionManager bean is created and is given the voters, but the decide method is never called.

Anyone has an idea what's going wrong ?

Thank for your help.

A: 

I found out that <global-method-security ../> wasn't inserted in the right xml file. See here for more details. This file happens to be webmvc-config.xml in my Roo project.

That unfolded several problem, I need SpEL for ACL so I finally settled for this config:

<bean class="org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler" id="methodExprHandler">
    <property name="permissionEvaluator" ref="aclPermissionEvaluator" />
</bean>
<bean class="org.springframework.security.acls.AclPermissionEvaluator" id="aclPermissionEvaluator">
    <constructor-arg ref="jdbcMutableAclService" />
</bean>


<security:global-method-security pre-post-annotations="enabled">
    <security:expression-handler ref="methodExprHandler"/>
</security:global-method-security>

No voter logic for authorization.

Nicolas