在正常的Spring MVC 框架中使用Filter时需要在XML文件中配置
<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> 但是在Spring boot 中抛弃了XML配置,所以要在启动主函数中添加
@Beanpublic 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;}补充: 如果想要在Filter中执行Services方法,要在初始化Filter中执行下图方法(因为Filter执行顺序提前与Services)
时间: 2024-10-13 01:48:05