Abdul Mohsin
Abdul Mohsin

Reputation: 1435

Spring boot filter always mapped to /*

In my Spring Boot application I have the following filter class

public class UserIdAuthenticationFilter implements Filter {
    // all the handling
}

I am not using any annotation on this class, I am using applicationContext.xml to define this bean as following:

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <security:filter-chain-map request-matcher="ant">
                <security:filter-chain  pattern="/lms/s/reward/*" filters="UIDAuthenticationFilter"/>
    </security:filter-chain-map>
</bean>
<bean id="UIDAuthenticationFilter" class="com.mycompany.security.filter.UserIdAuthenticationFilter" >
</bean>

I created another class that is used to load this xml configuration:

@Configuration
@ImportResource({"classpath:applicationContext.xml"})
public class XMLConfiguration {
}

Now when I start my application i can see in the logs that the filter is being loaded from applicationContext.xml:

15:06:01.516 [localhost-startStop-1] INFO - Mapping filter: 'UIDAuthenticationFilter' to: [/*]

It is clearly saying that the filter is mapped to /* So the filter is being called for each request, I dont want this behaviour, I want that my filter should only be called for the pattern that I provided in the applicationContext.xml. (/lms/s/reward/*)

Also I don't want this solution. Because I have to configure the filter in the applicationContext.xml, not in the java code.

Any help would be appreciated, Thanks

Upvotes: 1

Views: 368

Answers (1)

Abdul Mohsin
Abdul Mohsin

Reputation: 1435

I got the solution for this. I've changed my applicationContext.xml as below:

<bean id="MyFilter" class="com.mycompany.security.filter.MyFilter">
</bean>
<bean id="filterRegistrationBean"
    class="org.springframework.boot.web.servlet.FilterRegistrationBean">
    <property name="filter" ref="MyFilter">
    </property>
    <property name="urlPatterns" value="/lms/s/*">
    </property>
</bean>

Now my Filter is geting mapped to /lms/s/* , Thanks a lot to @M.Deinum

Upvotes: 2

Related Questions