ssh框架做登陆拦截的时候,页面不经过action直接访问jsp页面使用拦截器无法拦截,我使用servlet的过滤器过滤

1.在弄ssh框架时,无法对不经过action的jsp页面进行拦截。
2.使用servlet的过滤器进行拦截。
3.使用servlet的时候没有session无法对session进行判断
ssh拦截器就不说了,老掉牙的框架。
3.在web。xml配置过滤器。

<filter>
         <filter-name>URLfilter</filter-name>
          <filter-class>com.itheima.crm.web.MyFile.MyFiles</filter-class>
     </filter>
   <filter-mapping>
          <filter-name>URLfilter</filter-name>
           <url-pattern>*.jsp</url-pattern>
      </filter-mapping>

都应该看得懂,只说思路。
4.实现过滤器。

public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2)
   throws IOException, ServletException {
   HttpServletRequest httpreq = (HttpServletRequest) arg0;
    HttpServletResponse httpres = (HttpServletResponse) arg1;
   StringBuffer url = httpreq.getRequestURL();
   String uri = httpreq.getRequestURI();
   System.out.println(url+"==="+url.indexOf("login") );
   HttpSession session = httpreq.getSession();
indexOf("login") >0){
      arg2.doFilter(arg0, arg1); 
   }else if (url.indexOf("regist") >0) {
      arg2.doFilter(arg0, arg1);
   }else {
    
   if(httpreq.getSession(false)==null){
     httpres.sendRedirect(httpreq.getContextPath()+"/login.jsp"); 
     return;
    }
    User pid = (User)session.getAttribute("exisUser");
    if(pid == null){
     httpres.sendRedirect(httpreq.getContextPath()+"/login.jsp"); 
     return;
    }
    arg2.doFilter(arg0, arg1); 
   }
}

5.先判断是否有session:httpreq.getSession(false)若存在会话则返回该会话,否则返回NULL
如果有执行,没有返回首页。j
6.简单登陆拦截实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值