Spring mvc filter for some controllers

I need to process all requests coming to some Spring controllers in order to get information about the information request or throw exceptions, such as a security filter.

I would like if something built in Spring as a filter for controllers (I do not need this for all controllers, but only for someone). I do not want to apply this filter on url request, but with class or method extension or annotation.

This is my actual solution:

@Controller public class MyFilteredController extends FilterController { @RequestMapping("/filtered") public void test1(HttpServletRequest req){ InfoBean infobean=filter(req); //... controller job } } 

A controller that extends a class using a filtering method.

 public abstract FilterController{ protected InfoBean filter(HttpServletRequest req){ //... filter job return infobean; } } 
+5
source share
4 answers

I do not want to apply this filter on url request, but with class / method extension or annotation

You can register a HandlerInterceptor for this purpose. For example, you can apply a filter to all handler methods that are annotated using SomeAnnotation with the following code:

 public class CustomHandlerIntercepter extends HandlerInterceptorAdapter { @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; SomeAnnotation annotation = handlerMethod.getMethodAnnotation(SomeAnnotation.class); if (annotation != null) { // do stuff } } return true; } } 

In addition, you must register your interceptor with WebConfig :

 @EnableWebMvc @Configuration public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new CustomHandlerIntercepter()); } } 

You can learn more about interceptors in the spring help documentation .

+7
source

Everything used in SpringDispatcherServlet is URL based, I don’t think you can do it with a controller.

You will need to use the Filter, look at the API here https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/package-summary.html , you probably want to use OnePerRequestFilter.

 public class MyFilter extends OncePerRequestFilter{ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { // TODO Auto-generated method stub } } 

you will need to add a filter in web.xml

  <filter> <filter-name>requestFilter</filter-name> <filter-class>com.greg.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>errorHandlerFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

Now the hacked bit, if you want to get Spring beans here, you can create a Bridge class in it with statics.

 public class Bridge { private static PaymentService paymentService; public PaymentService getPaymentService() { return paymentService; } public void setPaymentService(PaymentService paymentService) { Bridge.paymentService = paymentService; } } 

If you want to insert some Spring beans in this class

 <bean id="paymentService" class="net.isban.example.service.PaymentService" /> <bean class="net.isban.example.util.Bridge"> <property name="paymentService" ref="paymentService" /> </bean> 

Then in your filter (not Spring class).

 PaymentService paymentService = new Bridge().getPaymentService(); 

Happy if someone shows me a less hacky way to do this.

0
source

Take a look at SpringSandwich http://springsandwich.com/

It allows you to set filters (Spring Interceptors, in fact) directly as controller annotations. Unlike regular servlet filters, you will also have full access to your Spring context.

Disclosure: I am the author of this structure :)

0
source

Source: https://habr.com/ru/post/1243823/


All Articles