The use of filter in the normal spring MVC framework requires configuration in the XML file
<filter> <filter-name>xxxFilter</filter-name> <filter-class> Xx.xx.xx.xx.xxxfilter</filter-class></filter><filter-mapping> <filter-name> Xxxfilter</filter-name> <url-pattern>/*</url-pattern></filter-mapping>
However, the XML configuration is discarded in spring boot, so to add a in the startup main function
@Bean
Public Filterregistrationbean Filterregistrationbean () {
Filterregistrationbean Registrationbean = new Filterregistrationbean ();
Wechatfilter wechatfilter = new Wechatfilter ();
Registrationbean.setfilter (Wechatfilter);
list<string> urlpatterns = new arraylist<string> ();
Urlpatterns.add ("/validate/*");
Registrationbean.seturlpatterns (Urlpatterns);
return Registrationbean;
}
Add:
If you want to execute the Services method in filter, execute the method in the Initialize filter (because the filter is executed in advance with the services)
How to use filter in Springboot