天天看点

Filter 对未登录用户访问地址的控制

首先我们应该明白java过滤器的作用原理,他到底是干什么的?什么原理?

顾名思义,过滤器即起到过滤的作用。大家可以把它根过滤网联想一下。这是我画的过滤器示意图:

Filter 对未登录用户访问地址的控制

1  过滤器对用户的‘请求’和服务器的‘响应’做了一层过滤,即进行了预处理。

(1)    当用户在客户端向服务器发出请求时,首先在请求进入服务器之前要经过过滤器Filter的处理(修改请求信息—包括请求的头信息、数据、url地址等信息);

(2)    当服务器接收到用户的请求之后,会响应客户端反馈信息回去,同样在服务器发送响应信息到客户端之前,过滤器也会进行相应的过滤处理;

2  写一个java过滤器,必须要实现javax.servlet.Filter接口。

过滤重启定义了3个必须实现的方法:

(1)      public void init(FilterConfig config) throwsServletException {}

(2)      public void doFilter(ServletRequest request,ServletResponse response, FilterChain chain) throws IOException,ServletException{}

(3)      public void destroy(){}

方法 (1) 用来初始化过滤器配置信息;

方法 (2) 进行相应的过滤处理;

方法 (3) 在取消执行之前进行过滤资源释放。

3        下面我们来实现一个用户通过浏览器访问资源的过滤器,要求:

(1)     如果用户未登录则跳转至登录页面,登陆之后跳转到原来输入地址对应的页面;

(2)     如果用户已登录则直接进入输入的地址对应的页面;

(3)     实现原理:

主要是对于未登录用户的处理,在跳转至登录页面之前将用户原来的访问的地址截取项目名称后面的部分,然后作为一个参数写入session中保存;

当用户登录时,将session中保存的地址取出进行跳转。

4        代码实现与配置:

(1)     过滤器代码

package adam.bp.workflow.filter;

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 hsy
 *
 */
public class LoginFilter implements Filter {
	private static final String SHOW_LOGIN_PATH = "SHOW_LOGIN_PATH";	//显示登录页面
	private static final String DO_LOGIN_PATH = "DO_LOGIN_PATH";		//登录操作不能过滤掉
	private static final String LOGIN_PERSONID = "LOGIN_PERSONID";		//登录用户在session中的属性key -- session.setAttribute(key,value)
	private String showloginPath;
	private String dologinPath;
	private String loginPersonId;
	
    public void init(FilterConfig config) throws ServletException {
    	showloginPath = config.getInitParameter(SHOW_LOGIN_PATH);
    	dologinPath = config.getInitParameter(DO_LOGIN_PATH);
    	loginPersonId = config.getInitParameter(LOGIN_PERSONID);
    	if(showloginPath==null || showloginPath.equals("") || showloginPath.equals("null") ){
    		throw new ServletException("登录页面配置出错...");
    	}
    }
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    	 HttpServletRequest  httpReq  = (HttpServletRequest) request;
         HttpServletResponse httpResp = (HttpServletResponse) response;
         httpResp.setContentType("text/html");
         //判断是否是登陆页面
         String servletPath = httpReq.getServletPath();
         
         //flag:若为登陆页面的action路径 showloginPath/nologinPath,则赋值true,否则赋值false
         boolean flag = false;
         if(servletPath.equals(showloginPath) || servletPath.equals(dologinPath)){
        	 chain.doFilter(request, response);
        	 flag = true;
        	 return;
         }
         else
         {   //如果不是登录页面,则需先判断用户是否已登录
        	 //String url = servletPath;
        	 Object loginId = httpReq.getSession().getAttribute(loginPersonId);
        	 if(loginId != null){//如果不为空,则进行已登录处理
        		 chain.doFilter(request, response);
        	 }else{//如果为空,则进行未登录处理
        		 if ( httpReq.getQueryString() != null )
            	 {
            		 servletPath += "?"+httpReq.getQueryString();
            	 }
            	 httpReq.getSession().setAttribute("returnUri", servletPath);
                 if ( flag == false )
                 {	
                	httpReq.getRequestDispatcher(showloginPath).forward(httpReq,httpResp);
                 } 
        	 }
         }
    }
    
    public void destroy(){
    	//do something
    }
    
}
           

(2)     web.xml中的配置

<!-- 用户登录过滤 -->
	<filter>
	 <filter-name>loginFilter</filter-name>
	 <filter-class>adam.bp.workflow.filter.LoginFilter</filter-class>
	 <init-param>
	     <param-name>LOGIN_PERSONID</param-name>
	     <param-value>loginPersonId</param-value>
	 </init-param>
	 <init-param>
	     <param-name>SHOW_LOGIN_PATH</param-name>
	     <param-value>/showLogin.do</param-value>
	 </init-param>
	 <init-param>
	     <param-name>DO_LOGIN_PATH</param-name>
	     <param-value>/doLogin.do</param-value>
	 </init-param>
	</filter>
	<filter-mapping>
		<filter-name>loginFilter</filter-name>
		<url-pattern>*.do</url-pattern>
	</filter-mapping>
	<filter-mapping>
		<filter-name>loginFilter</filter-name>
		<url-pattern>*.jsp</url-pattern>
	</filter-mapping>
           

(3) 相应的页面登录的servlet或者action代码,就不写了,相信对大家来说这个不难!