登录过滤器

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

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

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) 过滤器代码

[java] view plain copy

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  
    }  
      
}  


1

(2) web.xml中的配置

[html] view plain copy

<!– 用户登录过滤 –>  
<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>  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值