1.编写类并实现Filter接口
package com.test; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.test.service.TestService; public class AuthenticationFilter implements Filter { // 验证码接口 private TestService testService; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String path = httpRequest.getServletPath(); String tag = httpRequest.getParameter("tag"); // 拦截登录action if ("/login".equals(path) && null != tag) { testService.saveTest(); httpResponse.sendRedirect(httpRequest.getContextPath() + "/login"); return; } chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { } @Override public void destroy() { } public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } }
2.在spring配置文件中声明此类
<bean id="authenticationFilter" class="com.test.AuthenticationFilter"> <property name="testService" ref="testService" /> </bean>
3.修改web.xml如下:
<filter> <filter-name>authenticationFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>authenticationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
通过上述3步骤实现spring整合Filter。
二、通过获取spring的上下文整合Filter
1.编写一个类实现Filter接口
package com.test; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.test.service.TestService; public class AuthenticationFilter implements Filter { // 验证码接口 private TestService testService; @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpRequest = (HttpServletRequest) request; HttpServletResponse httpResponse = (HttpServletResponse) response; String path = httpRequest.getServletPath(); String tag = httpRequest.getParameter("tag"); // 拦截登录action if ("/login".equals(path) && null != tag) { testService.saveTest(); httpResponse.sendRedirect(httpRequest.getContextPath() + "/login"); return; } chain.doFilter(request, response); } @Override public void init(FilterConfig config) throws ServletException { captchaService = (CaptchaService) getBean("captchaService", config); } @Override public void destroy() { } /** * 通过spring工具类获取,spring接管的实体bean * * @param beanName * bean名称 * @param config * FilterConfig对象 * * @return 实体bean */ private Object getBean(String beanName, FilterConfig config) { WebApplicationContext wac = WebApplicationContextUtils .getRequiredWebApplicationContext(config.getServletContext()); return wac.getBean(beanName); } public TestService getTestService() { return testService; } public void setTestService(TestService testService) { this.testService = testService; } }
2.修改web.xml如下:
<filter> <filter-name>authenticationFilter</filter-name> <filter-class>com.test.AuthenticationFilter</filter-class> </filter> <filter-mapping> <filter-name>authenticationFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>