日期:2014-05-16 浏览次数:20611 次
package com.yihaodian.pis.util;
import java.io.IOException;
import java.io.PrintWriter;
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 javax.servlet.http.HttpSession;
public class LoginFilter implements Filter {
private static final String LOGON_URI = "LOGON_URI";
private static final String HOME_URI = "HOME_URI";
private String logon_page;
private String home_page;
public void destroy() {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
resp.setContentType("text/html;");
resp.setCharacterEncoding("utf-8");
HttpSession session = req.getSession();
PrintWriter out = resp.getWriter();
// 得到用户请求的URI
String request_uri = req.getRequestURI();
// 得到web应用程序的上下文路径
String ctxPath = req.getContextPath();
// 去除上下文路径,得到剩余部分的路径
String uri = request_uri.substring(ctxPath.length());
// 判断用户访问的是否是登录页面
if (uri.equals(logon_page) || uri.equals(home_page)) {
chain.doFilter(request, response);
return;
} else {
// 如果访问的不是登录页面,则判断用户是否已经登录
if (null != session.getAttribute("admin")
&& "" != session.getAttribute("admin"))
{
chain.doFilter(request, response);
return;
} else {
out.println("<script language=\"javaScript\">"
+ "parent.location.href='" +
ctxPath + logon_page + "'"
+ "</script>");
return;
}
}
}
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
// 从部署描述符中获取登录页面和首页的URI
logon_page = config.getInitParameter(LOGON_URI);
home_page = config.getInitParameter(HOME_URI);
// System.out.println(logon_page);
if (null == logon_page || null == home_page) {
throw new ServletException("没有找到登录页面或主页");
}
}
}
?
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>pcs</display-name>
<welcome-file-list>
<welcome-file>webpage/login/login.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/spring/spring-*.xml</param-value>
</context-param>
<!-- log4j在spring中的配置 -->
<!--然后加上这个Spring的Log4j侦听类,注意在JBOSS里面,不需要这个类,注释掉,不然会冲突, -->
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<!--webAppRootKey 属性为webApp.root 代表webApp的根目录,这样就能在properties文件中定义 -->
<!-- 这里的webApp是http://localhost:8080/webApp/ -->
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>pcs.WebContent</param-value>
</context-param>
<!--log4jConfigLocation 属性代表log4j.properties文件的地址 -->
<!-- log4j.appender.file.File=${webApp.root}/WEB-INF/logs/subject.log -->
<context-param>
<par