title: Spring Security - Url with request parameters rules ignored
body: <p>I have a web application that uses spring security. It uses <code>&lt;intercept-url ../&gt;</code> elements to describe the access filters for different urls. By default, this does not take request parameters of urls into account. I needed to set custom security rules of an url based on the request parameters. So I've done the following:</p>
  
  <p>1) I created a bean post-processor class that will enable request parameters option for the spring security mechanisms:</p>
  
  <pre><code>&lt;beans:beans&gt;
      . . .
      &lt;beans:bean class="MySecurityBeanPostProcessor"&gt;
          &lt;beans:property name="stripQueryStringFromUrls" value="false" /&gt;
      &lt;/beans:bean&gt;
      . . .
  &lt;/beans:beans&gt;
  </code></pre>
  
  <p>And the code:</p>
  
  <pre><code>public class MySecurityBeanPostProcessor implements BeanPostProcessor {
  
      private Boolean stripQueryStringFromUrls = null;
  
      @Override
      public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
          if (bean instanceof DefaultFilterInvocationSecurityMetadataSource &amp;&amp; stripQueryStringFromUrls != null) {
              ((DefaultFilterInvocationSecurityMetadataSource) bean)
                  .setStripQueryStringFromUrls(stripQueryStringFromUrls.booleanValue());
          }
          return bean;
      }
  
      // code stripped for clarity
  }
  </code></pre>
  
  <p>This should set the spring security metadata source to take into account the request parameters. I have debugged the above code and the <code>stripQueryStringFromUrls</code> property is being set.</p>
  
  <p>2) In my security context xml I have the following definitions:</p>
  
  <pre><code>&lt;intercept-url pattern="/myUrl?param=value" access="!isAuthenticated() or hasRole('ROLE_GUEST')" /&gt;
  &lt;intercept-url pattern="/myUrl" filters="none" /&gt;
  ...
  &lt;intercept-url pattern="/**" access="isAuthenticated()" /&gt;
  </code></pre>
  
  <p>As you can see, I need to access the url with the params specified only if the user is not authenticated, or uses a guest account. Also, I have added a rule for the same url, but without any params, which has no filters.</p>
  
  <p>As far as I know, spring security should be configured providing the more-specific url <strong>BEFORE</strong> the less-specific, because otherwise the chain will detect the more-general rule first and will not continue to the more-specific. That is why I expect the url with params to be more-specific, therefore to be denied access of authenticated non-guest users. Instead, the defined below more general rule applies.
  Here is the output:</p>
  
  <blockquote>
    <p>INFO  [STDOUT] 186879 [http-0.0.0.0-8080-1] DEBUG org.springframework.security.web.FilterChainProxy  - Candidate is: '/myUrl'; pattern is /myUrl; matched=true</p>
    
    <p>INFO  [STDOUT] 186879 [http-0.0.0.0-8080-1] DEBUG org.springframework.security.web.FilterChainProxy  - /myUrl?param=value has an empty filter list</p>
  </blockquote>
  
  <p>I also tried to remove the rule for the url wit no params. Then, instead of getting my rule for the url with params to work, the filter chooses the <code>/**</code> pattern and requires the users to log-in.
  The output for that is:</p>
  
  <blockquote>
    <p>INFO  [STDOUT] 73066 [http-0.0.0.0-8080-1] DEBUG org.springframework.security.web.FilterChainProxy  - Candidate is: '/myUrl'; pattern is /**; matched=true</p>
    
    <p>INFO  [STDOUT] 73068 [http-0.0.0.0-8080-1] DEBUG org.springframework.security.web.FilterChainProxy  - /myUrl?param=value at position 1 of 8 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'</p>
  </blockquote>
  
  <p>The application is written in Java 1.6, uses Spring v3.0 and is deployed on JBoss v5.1.0-GA, on a linux machine. I have no clues why the filters behave in the way I described. Your help and advices will be greatly appreciated.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>As a conclusion, what I observed is that the <code>/myUrl?param=value</code> filter is never applied - as if the entry in the security-context.xml was ignored. This fits the behavior I have observed up to now. I have also tried replacing <code>filters="none"</code> with <code>access="permitAll"</code>, switched to regex (and changes patterns accordingly - for example <code>/myUrl?param=value</code> becomes <code>\A/myUrl\?param=value\Z</code>) and in all variations the behavior I get is the same.</p>
  
  <hr>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>The issue described here is actually invalid. The reasons for this is the following matter: the project where the problem is present has excluded some spring packages due to internal library clashes and incompatibility, while the whole setup somehow worked. I was never made aware of that and actually this unpure configuration renders the whole question obsolete. The concrete reason is that the implementations of isAuthenticated() and isAnonymous() methods were not working as expected, therefore any advice provided here was not working.</p>