Spring过滤器filter拦截服务
来源:互联网 发布:数据录取 编辑:程序博客网 时间:2024/05/21 07:59
web.xml中元素执行的顺序是listener->filter->structs拦截器->servlet.
1.过滤器的概念
Java中的filter并不是一个标准的servlet,它不能处理用户请求,也不能对客户端生成响应。主要用于对HttpServletRequest进行预处理,也可以对HttpServletResponse进行后处理,是个典型的处理链。
优点:过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别主要过滤链的执行顺序问题。
2.过滤器的作用描述:
- 在HttpServletRequest到达Servlet之前,拦截客户的HttpServletRequest。
- 根据需要检查HttpServletRequest,也可以修改HttpServletRequest头和数据。
- 在HttpServletResponse到达客户端之前,拦截HttpServletResponse。
- 根据需要检查HttpServletResponse,可以修改HttpServletResponse头和数据。
3.过滤器的执行流程
4.Filter接口
- 如何驱动:在web应用程序启动时,web服务器将根据web.xml文件中的配置信息来创建每个注册的Filter实例对象,并将其保存在服务器的内存中。
方法介绍:
public void init(FilterConfig filterConfig) throws ServletException.
当开始使用servlet过滤器服务时,web容器调用此方法一次,为服务准备过滤器;然后在需要使用过滤器的时候调用doFilter(),传送给此方法的FilterConfig对象,包含servlet过滤器的初始化参数。public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws java.io.IOException,ServletException.
每个过滤器都接受当前的请求和响应,且FilterChain过滤器链中的过滤器(应该都是符合条件的)都会被执行。doFilter方法中,过滤器可以对请求和响应做它想做的一切,通过调用他们的方法收集数据,或者给对象添加新的行为。过滤器通过传送至此方法的FilterChain参数,调用chain.doFilter()将控制权传送给下一个过滤器。当这个调用返回后,过滤器可以在它的 Filter方法的最后对响应做些其他的工作。如果过滤器想要终止请求的处理或得到对响应的完全控制,则可以不调用下一个过滤 器,而将其重定向至其它一些页面。当链中的最后一个过滤器调用chain.doFilter()方法时,将运行最初请求的Servlet。public void destroy()
一旦doFilterO方法里的所有线程退出或已超时,容器调用 此方法。服务器调用destoryO以指出过滤器已结束服务,用于释 放过滤器占用的资源。
Filter实例:
web.xml配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app> <!-- 配置Spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/bean.xml</param-value> </context-param> <!-- 配置Spring的web Context监听器,将Spring与web工程集成在一起 --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <!-- 配置CXF --> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class> org.apache.cxf.transport.servlet.CXFServlet </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <!-- 使用CXF发布服务 --> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/service/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- filter过滤器 --> <filter> <filter-name>Logger</filter-name> <filter-class>test.Logger</filter-class> </filter> <filter-mapping> <filter-name>Logger</filter-name> <url-pattern>/service/*</url-pattern> </filter-mapping> </web-app>
通过web.xml配置文件设置要过滤的服务,这里设置为service/*。
bean.xml配置不变:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd"> <!-- 定时器开关 --> <task:annotation-driven/> <task:scheduled-tasks> <task:scheduled ref="hello" method="say" cron="5 * * * * ?"/> </task:scheduled-tasks> <!-- 将Bean托管给Spring --> <bean id="hello" class="test.HelloImpl"> </bean> <!-- 配置需要暴露的BeanService --> <jaxrs:server id="restContainer2" address="/resthello"> <jaxrs:serviceBeans> <ref bean="hello" /> </jaxrs:serviceBeans> </jaxrs:server> <jaxws:endpoint id="helloservice" address="/helloworld" implementor="test.HelloImpl"></jaxws:endpoint></beans>
Hello.java:
package test;import javax.jws.WebMethod;import javax.jws.WebService;import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.QueryParam;@Path("/hello")@WebServicepublic interface Hello { @WebMethod(operationName = "output", action = "urn:Output") @GET @Path("/getName") String output(); void say();}
HelloImpl.java:
package test;import java.util.Date;import javax.jws.WebService;public class HelloImpl implements Hello{ @Override public String output() { // TODO Auto-generated method stub System.out.println("output"); return "hello world"; } @Override public void say() { // TODO Auto-generated method stub System.out.println("say say say"+new Date()); }}
日志拦截器Logger.java:
package 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;public class Logger implements Filter{ @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub System.out.println("log->doFilter"); HttpServletRequest httpRequest = (HttpServletRequest)request; StringBuffer buf=httpRequest.getRequestURL(); String url=buf.toString(); System.out.println(url); long startTime=System.currentTimeMillis(); //执行方法 chain.doFilter(request, response); long endTime=System.currentTimeMillis(); System.out.println("执行时间 ->"+(endTime-startTime)); } @Override public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub }}
测试:调用服务可以得到:
- Spring过滤器filter拦截服务
- spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制(转)
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制(转)
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Spring Filter过滤器,Spring拦截未登录用户权限限制
- Filter(过滤器/拦截器)
- Spring filter 字符串过滤器
- Filter过滤器周期、Filter拦截过滤、Filter执行链
- 浅谈:Filter(过滤器)与Interceptor(拦截器)和Spring AOP的区别?
- Servlet 过滤器(filter)和Spring 拦截器(Interceptor)区别
- Filter(过滤器/拦截器)的作用
- filter过滤器 和拦截器的区别?
- cif安装错误异常处理过程记录
- 蛮力法求解旅行商
- 150.按规定输出
- 2015.6.27_记录自己学到的和自己容易忘记的有关编写MapReduce程序的要点
- 2015062706 - EffactiveJava笔记 - 第39条 必要时进行保护性拷贝
- Spring过滤器filter拦截服务
- Linux远程连接putty出现Network Erroe: Software caused connection abort
- 使用AsyncTask实现网络数据的异步访问
- 需求调研报告(用户需求说明书)编写规范
- 多路归并使用败者树的性能分析
- leetcode 33 Search in Rotated Sorted Array
- iOS开发——CoreBluetooth蓝牙开发(二)
- C++13.3 析构函数destructor
- eclipse中hibernate配置文件自动提示