web中filter需要注入bean(如service,dao等bean)--- DelegatingFilterProxy
2016-06-30 01:27
701 查看
DelegatingFilterProxy就是一个对于servlet filter的代理,用这个类的好处主要是通过spring容器来管理servlet filter的生命周期,还有就是如果filter中需要一些Spring容器的实例,可以通过spring直接注入,另外读取一些配置文件这些便利的操作都可以通过Spring来配置实现。
(2)配置方法二:
注: No WebApplicationContext found: no ContextLoaderListener registered 错误解决就是在web中加入context-param和listener俩个配置(可能顺序也是有要求的)
<context-param>是让web应用启动时加载spring bean 容器。
在Spring中配置的bean的name要和web.xml中的<filter-name>一样
或者在DelegatingFilterProxy的filter配置中配置初始参数:targetBeanName,对应到Spring配置中的beanname
如果要保留Filter原有的init,destroy方法的调用,还需要配置初始化参数targetFilterLifecycle为true,该参数默认为false
在web.xml中配置如下:
(1)配置方法一:<!-- 在使用DelegatingFilterProxy代理filter的时候必须加上如下俩个web配置,否则报错 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置过滤器 --> <filter> <filter-name>LoginFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>LoginFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
(2)配置方法二:
<!-- 在使用DelegatingFilterProxy代理filter的时候必须加上如下俩个web配置,否则报错 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 设置过滤器 --> <filter> <filter-name>LoginFilter1</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <init-param> <param-name>targetBeanName</param-name> <param-value>LoginFilter</param-value> </init-param> <init-param> <param-name>targetFilterLifecycle</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>LoginFilter1</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
注: No WebApplicationContext found: no ContextLoaderListener registered 错误解决就是在web中加入context-param和listener俩个配置(可能顺序也是有要求的)
<context-param>是让web应用启动时加载spring bean 容器。
在applicationContext.xml中配置如下:
<!-- 可注入bean的filter --> <bean id="LoginFilter" class="com.byepimples.interceptor.LoginFilter"> </bean>
在Spring中配置的bean的name要和web.xml中的<filter-name>一样
或者在DelegatingFilterProxy的filter配置中配置初始参数:targetBeanName,对应到Spring配置中的beanname
如果要保留Filter原有的init,destroy方法的调用,还需要配置初始化参数targetFilterLifecycle为true,该参数默认为false
三.Filte中的代码
import java.io.IOException; import java.io.PrintWriter; import javax.servlet.FilterChain; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.filter.OncePerRequestFilter; import com.byepimples.entity.User; import com.byepimples.service.UserService; public class LoginFilter extends OncePerRequestFilter{ @Autowired private UserService userService; @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { //不拦截的url String[] notFilter=new String[]{"/register.do","/post/respost.do","/restopic.do","/login.jsp","login.do"}; //请求的url String uri=request.getRequestURI(); //url中包含byepimples(这是我工程名)才进行过滤 if(uri.indexOf("byepimples")!=-1){ //是否过滤 boolean doFilter=true; for(String s:notFilter){ //如果uri中包含不过滤的uri,则不进行过滤 if(uri.indexOf(s)!=-1){ doFilter=false; break; } } if (doFilter) { //执行过滤 /* //方法一.普通web从session中获取登录者实体 Object obj=request.getSession().getAttribute("token"); //写入session中的key-value System.out.println("--------getSession().getAttribute(token)--"+request.getSession().getAttribute("token")); if(obj == null){ //如果session中不存在所需要的实体,则需要重新登录(或提醒) //设置request和response的字符集,防止乱码 request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); //跳转到登陆页面 // request.getRequestDispatcher("/login.jsp").forward(request, response); //这俩种都可以,一个是重定向,一个是请求转发 response.sendRedirect("/byepimples/login.jsp"); } */ //方法二.此方法用于android和web交互,因为安卓每次请求的session值都会改变,用第一种方法值为null,每次都需要登录,达不到效果 String tokenApp=request.getParameter("token"); System.out.println("app中token值 =----"+tokenApp); String usernameApp=request.getParameter("username"); System.out.println("App中username---------"+usernameApp); User user=userService.getUserByUserName(usernameApp); System.out.println("数据库中user----"+user.toString()); System.err.println("数据库中user.getToken()----"+user.getToken()); if(!tokenApp.equals(user.getToken())){ request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); out.write("0"); out.flush(); out.close(); return; // response.sendRedirect("/byepimples/login.jsp"); //web端直接用这个跳转到指定页面 } else{ //如果session中存在 登录者/所需 实体,则继续请求 PrintWriter out=response.getWriter(); out.write("1"); out.flush(); out.close(); return; // filterChain.doFilter(request, response); //web端直接用这个,使用上边的方法是因为android需要 } }else{ //如果不执行过滤,则可以直接请求 filterChain.doFilter(request, response); } }else{ //如果uri中不包含byepimples,则继续 filterChain.doFilter(request, response); } } }
相关文章推荐
- jQuery plugin items filter
- 全国哀悼日网站页面变成灰色的filter方法
- 防御SQL注入攻击时需要注意的一个问题
- 用css filter做鼠标滑过图片效果
- Sql2005注入辅助脚本[修改版]
- MySQL注入中导出字段内容的研究通过注入导出WebShell
- 浅析MySQL的注入安全问题
- sql server注入建立虚拟目录的方法
- PHP中怎样防止SQL注入分析
- ASP 使用Filter函数来检索数组的实现代码
- C#使用带like的sql语句时防sql注入的方法
- JavaScript 数组some()和filter()的用法及区别
- Vue.js报错Failed to resolve filter问题的解决方法
- ASP.NET MVC:Filter和Action的执行介绍
- jQuery遍历DOM节点操作之filter()方法详解
- jQuery中each()、find()和filter()等节点操作方法详解(推荐)
- mssql注入躲避IDS的方法
- JSP Filter的应用方法
- Spring中属性注入详解
- ASP注入详细命令40条第1/2页