java 使用过滤器控制用户访问权限

阅读更多
控制器类

package com.crm.filter;   
  
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("curUser")   
     && "" != session.getAttribute("curUser"))    
  
{   
    chain.doFilter(request, response);   
    return;   
   } else {   
    out.println("");   
    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("没有找到登录页面或主页");   
  }   
}   
  
}   

web.xml


  
   loginFilter  
   com.crm.filter.LoginFilter  
     
         LOGON_URI  
         /login.jsp  
  
  
     
         HOME_URI  
         /index.jsp  
  
  
  
  
  loginFilter  
  *.jsp  
  

你可能感兴趣的:(Java,Servlet,JSP,Web,XML)