Home > Mobile >  How can I set http method in servlet filter(in context of Spring Framework)?
How can I set http method in servlet filter(in context of Spring Framework)?

Time:11-05

I have got a such issue: I do have got antMatcher​ in Spring Security. And I have an opportunity to set http method GET/POST/PUT/PATCH/DELETE for url mapping for my filter. So requests with some methods will be filtered with my filter if they have certain methods and will not be filtered otherways. But how can I specify http method without Spring Security? Is it possible to do it somehow in a cool way using FilterRegistrationBean or I will be have to use

switch(request.getMethod()) {
   ...
}

in my fileter doFilterInternal() method (I use OncePerRequestFilter) and act according to which method is specified in HttpServletRequest? That is tedious, isn`t it?

I tried to find out some info according to this issue and researches have not been successful yet.

@Bean
public FilterRegistrationBean<CustomFilter> registrationBean(){
    FilterRegistrationBean<CustomFilter> registration = new FilterRegistrationBean<>();
    registration.setFilter(customFilter);
    registration.addUrlPatterns("/open/*");
    /*may be it is possible to addHttpMethods?*/
    registration.setName("customLoggingFilter");
    registration.setOrder(2);
    return registration;
}

CodePudding user response:

Well, no. It is impossible, but I was told that it is a really not bad idea to override the shouldFilter method of the OncePerRequestFilter.

  • Related