Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I exclude a specific URL in a filter in Spring?

Tags:

java

spring

I have a basic application filter config that looks like this:

@Configuration
public class ApplicationFilterConfig {

    /**
    * Check if user is logged in
    */
    @Bean
    public FilterRegistrationBean applicationFilterRegistration() {

        // created a collections that need logins to be validated.
        ArrayList<String> urlPatterns = new ArrayList<String>();
        urlPatterns.add("/home");
        urlPatterns.add("/about");
        urlPatterns.add("/contact");
        // ... 

        LoginFilter loginFilter = new LoginFilter();
        FilterRegistrationBean registration = new FilterRegistrationBean(); 
        registration.setFilter(loginFilter);
        registration.setUrlPatterns(urlPatterns);
        registration.setOrder(1);
        return registration;

    } 


}

However, the list urlPatterns grows to be very long, even when using the star annotation (e.g. /employees/*). The main reason why I don't just use /* is because I don't want to require login validation for my login page. If I did, it'd create an infinite loop of redirects.

Does the FilterRegistrationBean class allow you to apply a filter to all URL patterns except certain patterns?

I could put everything except the login page in a directory and set my URL pattern to be /subdirectory/* but that adds an unnecessary level of depth to every file in my webapp.

like image 653
nicholas79171 Avatar asked Aug 29 '16 18:08

nicholas79171


People also ask

How do I exclude a URL from filter mapping?

The solution for excluding URLs from a third-party filter is to wrap it with a new custom filter which just adds the exclude functionality and delegates the filter logic to the wrapped class. // Forward the request to the next filter or servlet in the chain.

How do I restrict URL in Spring Security?

Securing the URLs The most common methods are: authenticated(): This is the URL you want to protect, and requires the user to login. permitAll(): This is used for URL's with no security applied for example css, javascript. hasRole(String role): Restrict to single role.

What is Spring FilterRegistrationBean?

FilterRegistrationBean registers a Filter as Spring bean and it provides methods to add URL mappings, set Filter order etc. When we register a Filter using Spring @Component , we can set Filter order using Spring @Order annotation but there is no way to change default URL mappings in this case.


1 Answers

FilterRegistrationBean doesnot provide(atleast until now) any method to exclude URL patterns. But it can be taken care from from Filter implementation(in this case its LoginFilter). Implement LoginFilter by extending OncePerRequestFilter which has a method called shouldNotFilter() which can be overriden to suit your needs.

    public class LoginFilter extends OncePerRequestFilter {

        private List<String> excludeUrlPatterns = new ArrayList<String();

        @Override
        protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
                FilterChain filterChain) throws ServletException, IOException
        {
            // Implementation....
        }

       @Override
       protected boolean shouldNotFilter(HttpServletRequest request) throws ServletException 
       {
           // Populate excludeUrlPatterns on which one to exclude here
       }

    }

For more details you can follow this link

like image 153
Sumanth Shastry Avatar answered Oct 15 '22 17:10

Sumanth Shastry