一、使用spring提供的代理类(DelegatingFilterProxy)整合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 {
}
@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>
分享到:
相关推荐
a 加入支持:添加 spring核心包、hibernate 3.6 包、 spring整合hibernate包 , 在src下建立applicationContext.xml (先建立src下便于测试hibernate)。 b 编写实体类,加入hibernate注解,编写方法类测试类,在...
- Spring Boot 基础知识,包括SpringBoot起步、配置详解、aop、filter、拦截器、监听、启动器、全局异常处理、外部Tomcat启动、HTTPS、监控 等。 - springboot-data - Spring Boot 数据库操作,包括SpringJDBC、...
《Java Web整合开发王者归来(JSP+Servlet+Struts+Hibernate+Spring)》全面介绍了Java Web开发中的各种相关技术及知识。全书分为9篇,内容层次清晰,难度循序渐进。第1篇为入门篇,内容包括Java Web开发概述等;第2篇...
Struts+JSF+filter+Myfaces+A4j+Spring+hibernate+Mysql整合一个项目
Spring boot+Mybatis+Mysql+Swagger整合,包括spring boot下创建过滤器filter与intercepter拦截器,使用Intellij idea创建工程,测试OK
为了练手培训,给大家准备的 Open...3.通过 open session in view filter 支持 延迟加载 4.在页面上通过 jstl 很优雅的获取数据 5.通过 spring aop(aspectJ) 声明事务 6.通过formular 映射参数表,指定两个死的变量
原理过程 Springboot中会使用FilterRegistrationBean来注册Filter,Filter是Servlet规范里面的,属于容器范围,Springboot中没有web.xml,那Springboot中,不用管Filter是如何交给Ser...SpringBoot整合XssFilter,...
包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 18. spring-webmvc-4.1.1.RELEASE.jar 包含...
整合Druid用于数据库连接,并使用Druid对业务层监控spring jdbc 整合EhCache,对Mybatis的二级缓存进行管理和对spring进行缓存管理 整合FastJson对指定http类型的数据进行转换 整合hibernate.validator校验器对...
bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean"> 02 03 <!-- override these for application-specific URLs if you like:--> 04 <!-- ...
spring-web.jar(必须) :这个jar 文件包含Web 应用开发时,用到Spring 框架时所需的核心类,包括自动载入Web Application Context 特性的类、Struts 与JSF 集成类、文件上传的支持类、Filter 类和大量工具辅助类。...
虽然《Spring in Action》选择了 .html, 但是那是一种非常糟糕的作法, 特别是你整合 Apache 和 Tomcat 的时候. 配置 CharacterEncodingFilter (filter 标签), 否则你会发现中文乱码. 因为我的 jsp 和 html 文件都...
<filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name>*...
这个小程序还包括了一个解决中文问题的filter.和一个log4j日志工具 博文链接:https://cui.iteye.com/blog/109850
Spring框架是一系列应用框架的核心,也可以说是整合其他应用框架的基座。同时还是SpringBoot的基础。在当下的市场开发环境中,Spring占据的地位是非常高的,基本已经成为了开发者绕不过去的框架了。它里面包含了...
包含Springboot整合shiro安全框架+Redis+Swagger+Filter超详细 ssm整合shiro安全框架+Redis+Swagger+Filter超详细
一 服务启动 此项目集成了:Feign,Spring Cloud Bus,hystrix,swagger-ui,zuul-filter,配置中心功能 1)安装rabbitMQ 2)启动cloud—eureka :此时可访问 localhost:8761 3)启动 cloud-config 此处为配置中心 ...
没事了做一个demo 玩玩想学习的可以看一下。这是个初步的整合。里面什么功能都没有。有一个登录和filter 本人是做BI的 以后会把BI整合进去,同时也把权限也整合进去。