过滤器类
package baseinfo.com.windtec.offline.server.baseinfo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
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;
/**
* 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面<p>
* @author CaoXiang
* @version 1.0
*/
public class UserFilter implements Filter {
//过滤器配置对象
protected FilterConfig filterConfig = null;
//导向到该页面
private String redirectURL = null;
//不接受过滤的地址
private List notCheckURLList = new ArrayList();
//session对象中的key值
private String sessionKey = null;
/**
* 过滤
*/
public void doFilter(
ServletRequest servletRequest,
ServletResponse servletResponse,
FilterChain filterChain)
throws IOException,servletexception {
HttpServletRequest request = (HttpServletRequest) servletRequest;
HttpServletResponse response = (HttpServletResponse) servletResponse;
HttpSession session = request.getSession();
if (sessionKey == null) {
filterChain.doFilter(request,response);
return;
}
if ((!checkRequestURIIntNotFilterList(request))
&& session.getAttribute(sessionKey) == null) {
response.sendRedirect(request.getcontextpath() + redirectURL);
return;
}
filterChain.doFilter(servletRequest,servletResponse);
}
/**
* 对象的销毁
*/
public void destroy() {
notCheckURLList.clear();
}
/**
* 判断地址
* @param request
* @return
*/
private boolean checkRequestURIIntNotFilterList(HttpServletRequest request) {
String uri =
request.getServletPath()
+ (request.getPathInfo() == null ? "" : request.getPathInfo());
return notCheckURLList.contains(uri);
}
/**
* filter初始化
*/
public void init(FilterConfig filterConfig) throws servletexception {
this.filterConfig = filterConfig;
redirectURL = filterConfig.getinitParameter("redirectURL");
sessionKey = filterConfig.getinitParameter("checkSessionKey");
String notCheckURLListStr =
filterConfig.getinitParameter("notCheckURLList");
if (notCheckURLListStr != null) {
StringTokenizer st = new StringTokenizer(notCheckURLListStr,";");
notCheckURLList.clear();
while (st.hasMoretokens()) {
notCheckURLList.add(st.nextToken());
}
}
}
}
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems,Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="WebApp">
<!-- WebService服务管理程序 -->
<display-name>offlineserver</display-name>
<filter>
<filter-name>UserFilter</filter-name>
<filter-class>baseinfo.com.windtec.offline.server.baseinfo.UserFilter</filter-class>
<!-- 需检查的在 Session 中保存的关键字 -->
<init-param>
<param-name>checkSessionKey</param-name>
<param-value>user</param-value>
</init-param>
<!-- 不做检查的URL列表,以分号分开,并且 URL 中不包括 contextpath -->
<init-param>
<param-name>notCheckURLList</param-name>
<param-value>/index.jsp</param-value>
</init-param>
<!-- 如果用户未登录,则重定向到指定的页面,URL不包括 contextpath -->
<init-param>
<param-name>redirectURL</param-name>
<param-value>/index.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>UserFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 用户登陆验证服务 -->
<servlet>
<servlet-name>user_com_windtec_offline_server_user_LoginServiceImp</servlet-name>
<servlet-class>user.com.windtec.offline.server.user.LoginServiceImp</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 默认显示的首页 -->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
代码如上,希望对大家有帮助
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。