使用过滤器验证是否登录

以前过滤器可能都只是处理下编码,这次项目中发觉其他成员都是使用过滤器来验证是否登录,然后作相应的转向,发觉真的很不错,不用写一个页面用来判断,然后在每个页面进行包含,也不需要在action中先判断是否登录了再作相应的业务逻辑处理。。。看来还是项目经验不够啊。。。
过滤器

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下的配置


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">
系统过滤器定义

Set Character Encoding
com.lhcard.common.SetEncodingFilter

encoding
gbk



HibernateSessionFilter

com.lhcard.common.HibernateSessionFilter



FaKaRenLoginFilter

com.lhcard.common.FaKaRenLoginFilter



ShangHuLoginFilter

com.lhcard.common.ShangHuLoginFilter



YunYingLoginFilter

com.lhcard.common.YunYingLoginFilter



ChiKaRenFilter

com.lhcard.common.ChiKaRenFilter



GeRenLoginFilter

com.lhcard.common.GeRenLoginFilter



Set Character Encoding
/*


HibernateSessionFilter
*.do


FaKaRenLoginFilter
/FaKaRen/*


ShangHuLoginFilter
/ShangHu/*


YunYingLoginFilter
/YunYing/*


ChiKaRenFilter
/ChiKaRen/*


GeRenLoginFilter
/GeRen/*


STRUTS的ActionServlet配置

action

org.apache.struts.action.ActionServlet


config

/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



debug
3


detail
3

0


action
*.do



getalllist
com.lhcard.common.GetAllList

2




Connector

com.fredck.FCKeditor.connector.ConnectorServlet


baseDir
/UserFiles/


debug
true

1




SimpleUploader

com.fredck.FCKeditor.uploader.SimpleUploaderServlet


baseDir
/UserFiles/


debug
true


enabled
true


AllowedExtensionsFile



DeniedExtensionsFile

php|php3|php5|phtml|asp|aspx|ascx|jsp|cfm|cfc|pl|bat|exe|dll|reg|cgi



AllowedExtensionsImage
jpg|gif|jpeg|png|bmp


DeniedExtensionsImage



AllowedExtensionsFlash
swf|fla


DeniedExtensionsFlash


1



Connector

/FCKeditor/editor/filemanager/browser/default/connectors/jsp/connector




SimpleUploader

/FCKeditor/editor/filemanager/upload/simpleuploader





index.jsp

页面标签的加载


/WEB-INF/app.tld
/WEB-INF/app.tld


/WEB-INF/struts-bean.tld
/WEB-INF/struts-bean.tld


/WEB-INF/struts-html.tld
/WEB-INF/struts-html.tld


/WEB-INF/struts-logic.tld
/WEB-INF/struts-logic.tld


/WEB-INF/struts-nested.tld

/WEB-INF/struts-nested.tld



/WEB-INF/struts-template.tld

/WEB-INF/struts-template.tld



/WEB-INF/struts-tiles.tld
/WEB-INF/struts-tiles.tld



/FCKeditor
/WEB-INF/FCKeditor.tld



720




你可能感兴趣的:(web)