How to create my own filter with Spring MVC?

前端 未结 7 1559
名媛妹妹
名媛妹妹 2020-12-14 07:40

I use Spring MVC (4.0.1) as a backend for rest services and angularjs as frontend.

every request to my server backend has a http-header with a session id

I c

7条回答
  •  清歌不尽
    2020-12-14 07:45

    You can create and configure your own filter by doing following steps.

    1) Create your class by implementing the filter interface and override its methods.

    public class MyFilter implements javax.servlet.Filter{
    
    
    public void destroy(){}
    public void doFilter(Request, Response, FilterChain){//do what you want to filter
    }
    ........
    }
    

    2) Now configure your filter in web.xml

    
      myFilter
      MyFilter
    
    

    3) Now provide url mapping of the filter.

    
       myFilter
       *
    
    

    4) Now restart your server and check all the web request will first come to MyFilter and then proceed to the respective controller.

    Hopefully it will be the required answer.

提交回复
热议问题