使用过滤器验证是否登录 博客分类: web StrutsfckeditorWebJSPServlet
程序员文章站
2024-03-16 08:26:52
...
以前过滤器可能都只是处理下编码,这次项目中发觉其他成员都是使用过滤器来验证是否登录,然后作相应的转向,发觉真的很不错,不用写一个页面用来判断,然后在每个页面进行包含,也不需要在action中先判断是否登录了再作相应的业务逻辑处理。。。看来还是项目经验不够啊。。。
过滤器
web.xml下的配置
过滤器
package com.lhcard.common; 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; /** * @author:陈伟 * @date:2008-5-23 * */ public class GeRenLoginFilter implements Filter { private final static String[] eixt_url = { "index.jsp", "userlogin.jsp", "relogin.jsp", "uholdcard.do", "/js", "/css", "/images", "cgoods.do", "keywordslist.jsp", "regprotocol.jsp", "register.jsp", "uarea.do" }; // 不用做权限判断的URL public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; boolean haveFind = true; // System.out.println("req.getRequestURI()==="+req.getRequestURI()); if (req.getRequestURI().indexOf(".jsp") >= 0 || req.getRequestURI().indexOf(".do") >= 0) haveFind = false; for (int i = 0; i < eixt_url.length; i++) { if (req.getRequestURI().indexOf(eixt_url[i]) >= 0) { haveFind = true; break; } } // System.out.println("haveFind==="+haveFind); if (!haveFind && req.getSession().getAttribute("user") == null) { res.sendRedirect(req.getContextPath() + "/GeRen/relogin.jsp"); // System.out.println("----redirect----"); return; } chain.doFilter(req, res); } public void init(FilterConfig filterConfig) throws ServletException { } public void destroy() { } }
web.xml下的配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <description>系统过滤器定义</description> <filter> <filter-name>Set Character Encoding</filter-name> <filter-class>com.lhcard.common.SetEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>gbk</param-value> </init-param> </filter> <filter> <filter-name>HibernateSessionFilter</filter-name> <filter-class> com.lhcard.common.HibernateSessionFilter </filter-class> </filter> <filter> <filter-name>FaKaRenLoginFilter</filter-name> <filter-class> com.lhcard.common.FaKaRenLoginFilter </filter-class> </filter> <filter> <filter-name>ShangHuLoginFilter</filter-name> <filter-class> com.lhcard.common.ShangHuLoginFilter </filter-class> </filter> <filter> <filter-name>YunYingLoginFilter</filter-name> <filter-class> com.lhcard.common.YunYingLoginFilter </filter-class> </filter> <filter> <filter-name>ChiKaRenFilter</filter-name> <filter-class> com.lhcard.common.ChiKaRenFilter </filter-class> </filter> <filter> <filter-name>GeRenLoginFilter</filter-name> <filter-class> com.lhcard.common.GeRenLoginFilter </filter-class> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>HibernateSessionFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <filter-mapping> <filter-name>FaKaRenLoginFilter</filter-name> <url-pattern>/FaKaRen/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>ShangHuLoginFilter</filter-name> <url-pattern>/ShangHu/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>YunYingLoginFilter</filter-name> <url-pattern>/YunYing/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>ChiKaRenFilter</filter-name> <url-pattern>/ChiKaRen/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>GeRenLoginFilter</filter-name> <url-pattern>/GeRen/*</url-pattern> </filter-mapping> <description>STRUTS的ActionServlet配置</description> <servlet> <servlet-name>action</servlet-name> <servlet-class> org.apache.struts.action.ActionServlet </servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/struts-config.xml, /WEB-INF/struts-config-chikaren.xml, /WEB-INF/struts-config-geren.xml, /WEB-INF/struts-config-shanghu.xml, /WEB-INF/struts-config-yunying.xml, /WEB-INF/struts-config-fakaren.xml </param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <servlet> <servlet-name>getalllist</servlet-name> <servlet-class>com.lhcard.common.GetAllList</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <!-- FCKeditor --> <servlet> <servlet-name>Connector</servlet-name> <servlet-class> com.fredck.FCKeditor.connector.ConnectorServlet </servlet-class> <init-param> <param-name>baseDir</param-name> <param-value>/UserFiles/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>SimpleUploader</servlet-name> <servlet-class> com.fredck.FCKeditor.uploader.SimpleUploaderServlet </servlet-class> <init-param> <param-name>baseDir</param-name> <param-value>/UserFiles/</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>enabled</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>AllowedExtensionsFile</param-name> <param-value></param-value> </init-param> <init-param> <param-name>DeniedExtensionsFile</param-name> <param-value> php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi </param-value> </init-param> <init-param> <param-name>AllowedExtensionsImage</param-name> <param-value>jpg|gif|jpeg|png|bmp</param-value> </init-param> <init-param> <param-name>DeniedExtensionsImage</param-name> <param-value></param-value> </init-param> <init-param> <param-name>AllowedExtensionsFlash</param-name> <param-value>swf|fla</param-value> </init-param> <init-param> <param-name>DeniedExtensionsFlash</param-name> <param-value></param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Connector</servlet-name> <url-pattern> /FCKeditor/editor/filemanager/browser/default/connectors/jsp/connector </url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>SimpleUploader</servlet-name> <url-pattern> /FCKeditor/editor/filemanager/upload/simpleuploader </url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <description>页面标签的加载</description> <jsp-config> <taglib> <taglib-uri>/WEB-INF/app.tld</taglib-uri> <taglib-location>/WEB-INF/app.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-nested.tld</taglib-uri> <taglib-location> /WEB-INF/struts-nested.tld </taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-template.tld</taglib-uri> <taglib-location> /WEB-INF/struts-template.tld </taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-tiles.tld</taglib-uri> <taglib-location>/WEB-INF/struts-tiles.tld</taglib-location> </taglib> <taglib> <taglib-uri>/FCKeditor</taglib-uri> <taglib-location>/WEB-INF/FCKeditor.tld</taglib-location> </taglib> </jsp-config> <session-config> <session-timeout>720</session-timeout> </session-config> </web-app>