现在的位置: 首页 > 综合 > 正文

过滤器检验用户是否登录

2013年02月05日 ⁄ 综合 ⁄ 共 3084字 ⁄ 字号 评论关闭

 

过滤器可以实现用户身份验证的功能,如果用户没有登录,则跳转到登陆页面:

本例实现:

 

过滤器类:

 

package com.start.util;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.IOException;

/**
* 用于检测用户是否登陆的过滤器,如果未登录,则重定向到指的登录页面

* 配置参数

* checkSessionKey 需检查的在 Session 中保存的关键字

* redirectURL 如果用户未登录,则重定向到指定的页面,URL不包括 ContextPath

* notCheckURLList 不做检查的URL列表,以分号分开,并且 URL 中不包括 ContextPath

*/
public class CheckLoginFilter implements Filter {
 
 protected FilterConfig filterConfig = null;
 private String redirectURL = null;
 private List notCheckURLList = new ArrayList();  
 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();
 }

 private boolean checkRequestURIIntNotFilterList(HttpServletRequest request)
 {
  String uri = request.getServletPath() + (request.getPathInfo() == null ? "" : request.getPathInfo());

  return notCheckURLList.contains(uri);
 }

 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中:

 <!-- servlet 过滤器 -->
 <filter>
  <filter-name>CheckLoginFilter</filter-name>
  <filter-class>com.start.util.CheckLoginFilter</filter-class>
  <init-param>
       <param-name>checkSessionKey</param-name>
       <param-value>forUser</param-value>
  </init-param>
  <init-param>
  <param-name>redirectURL</param-name>
  <param-value>/index.jsp</param-value>
  </init-param>
  <init-param>
       <param-name>notCheckURLList </param-name>
       <param-value>/error.jsp;/index.jsp;/m/file/login.action</param-value>
  </init-param>
 </filter>
  <filter-mapping>
  <filter-name>CheckLoginFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

大的格式相信大家都明白,解释下变量:

checkSessionKey:web下的session本身就是一个map,checkSessionKey就是取map的key,即你set进去的表面用户身份的session的key。

redirectURL:看这段代码 if((!checkRequestURIIntNotFilterList(request)) && session.getAttribute(sessionKey) == null)

  {
   response.sendRedirect(request.getContextPath() + redirectURL);
   return;
  } 都明白了吧?就是如果没有登录用户,则跳转到的页面。

notCheckURLList :这个就是不需要过滤的页面啦

抱歉!评论已关闭.