AuthFilter过滤器阻断通过转发的WEB-INF/jsp页面失败

AuthFilter过滤器阻断通过转发的WEB-INF/jsp页面失败

您好,

       我的问题是如何使AuthFilter起效果。限制不登录不能进管理页面。

      我的login.jsp放在WEB-CONTENT的根目录下,因为我不想阻断login.jsp,也不想阻断非JSP文件。而管理页面都放在WEB-INF/jsp目录下,管理页面通过SERVLET controller跳转到JSP页面,在eclipse中没有输出 AuthFilter 的阻 断信息。我看了其他人同样的问题,通过controller跳转到JSP页面不会被filter 拦截,我想问下怎么在WEB.XML里面配置才能使这种 跳转过去的也被拦截到???

相关截图:

http://img1.sycdn.imooc.com//climg/6008d80a096d7ed413490611.jpg

相关截图:

http://img1.sycdn.imooc.com//climg/6008d6d20940346506710611.jpg

相关截图:

       WEB.xml配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">

  <display-name>library</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

  </welcome-file-list>

  <filter>

     <filter-name>EncodeFilter</filter-name>

     <filter-class>com.imooc.library.filter.EncodeFilter</filter-class>

     <init-param>

        <param-name>Encoding</param-name>

        <param-value>UTF-8</param-value>

     </init-param>

  </filter>

  <filter-mapping>

     <filter-name>EncodeFilter</filter-name>

     <url-pattern>/*</url-pattern>

  </filter-mapping>

  <filter>

       <filter-name>AuthFilter</filter-name>

       <filter-class>com.imooc.library.filter.AuthFilter</filter-class>

  </filter>

    <filter-mapping>

     <filter-name>AuthFilter</filter-name>

     <url-pattern>/WEB-INF/jsp/*</url-pattern>

<!--      <url-pattern>/*</url-pattern> -->

  </filter-mapping>

</web-app>

   

相关代码:

package com.imooc.library.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;

import com.imooc.library.entity.User;
/*
*
*/
public class AuthFilter implements Filter {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
User user=(User)req.getSession().getAttribute("user");
System.out.println("====== authfilter过滤user结果username="+user.getUserName());
if(user!=null) {
chain.doFilter(request, response);
return;
}else {
res.sendRedirect(request.getServletContext()+"/login.jsp");
}


}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
慕UI9436362 提问者 2021-01-21 15:38:47

解决了。

package com.imooc.library.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;


import com.imooc.library.entity.User;

/*

 * 

 */

public class AuthFilter implements Filter {


@Override

public void destroy() {

// TODO Auto-generated method stub


}


@Override

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

throws IOException, ServletException {

// TODO Auto-generated method stub

HttpServletRequest req=(HttpServletRequest)request;

HttpServletResponse res=(HttpServletResponse)response;

StringBuffer URL=req.getRequestURL();

System.out.println("filter URL="+URL);

String url[]={".js",".html","login",".css","png","jpg"};

boolean flag=false;

String method=req.getParameter("method");

if(method!=null) {

if(method.equals("login")) {

flag=true;

}

}

for(String str:url) {

System.out.println("index="+URL.indexOf(str));

if(URL.indexOf(str)>0) {

flag=true;

break;

}

}

if(!flag) {

System.out.println("====== authfilter过滤user结果username=");

    User user=(User)req.getSession().getAttribute("user");

         if(user!=null) {

    chain.doFilter(request, response);

    }else {

   res.sendRedirect(req.getContextPath()+"/login.jsp");

   //chain.doFilter(request, response);

     }

}else 

  {

chain.doFilter(request, response);

  }

        

}


@Override

public void init(FilterConfig filterConfig) throws ServletException {

// TODO Auto-generated method stub


}


}


  • 同学解决了就好,棒棒哒!继续加油!

    祝学习愉快!

    2021-01-21 16:06:04
好帮手慕小尤 2021-01-21 14:08:38

同学你哈哦,1、建议同学将 <url-pattern>/WEB-INF/jsp/*</url-pattern>修改为 <url-pattern>/*</url-pattern>,如下图所示:

http://img1.sycdn.imooc.com//climg/600913c309702c6f08760232.jpg

/* 会匹配所有url(只匹配当前文件夹下文件,不匹配子文件夹下文件),路径型的和后缀型的url(包括/login,*.jsp,*.js和*.html等)。

2、建议在AuthFilter过滤器中放行js、css、login等页面。如下图所示:

http://img1.sycdn.imooc.com//climg/600915e409f2fcf211350775.jpg

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
// 判断是否能通过
boolean flag = false;
String reqURI = req.getRequestURI();
// 不需要过滤的url
String[] url = { ".js", ".css", ".html", ".png", "/login" };
for (String str : url) {
// 判断uri是否需要过滤
if (reqURI.indexOf(str) != -1) {
flag = true;
break;
}
}
// 如果为flag为false,则判断是否登录,如果登录,则放行,没登录则跳转到登录页面
if (!flag) {
User user = (User) req.getSession().getAttribute("user");
if (user != null) {
// 如果user为null,则直接调用getUserName()方法,会出现空指针异常,所以建议在if判断中进行输出
System.out.println("====== authfilter过滤user结果username=" + user.getUserName());
chain.doFilter(request, response);
return;
} else {
res.sendRedirect(request.getServletContext() + "/login.jsp");
}
}
chain.doFilter(request, response);
}

祝学习愉快!

  • 提问者 慕UI9436362 #1

    不行啊, 我修改了以后

    虽然修改后 转向了login.jsp 但login.jsp却提交不了。因为我的登录验证方法 是:

    http://localhost:8080/library/task?method=login

    控制台拉截到的URI或URL只有http://localhost:8080/library/task ,因此也将登录提交拦截了。就一直在登 录页面,跳不到管理页面了。还是没解决。

    2021-01-21 15:30:08
  • 提问者 慕UI9436362 #2

    最后修改好了。,谢谢。

    package com.imooc.library.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;


    import com.imooc.library.entity.User;

    /*

     * 

     */

    public class AuthFilter implements Filter {


    @Override

    public void destroy() {

    // TODO Auto-generated method stub


    }


    @Override

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

    throws IOException, ServletException {

    // TODO Auto-generated method stub

    HttpServletRequest req=(HttpServletRequest)request;

    HttpServletResponse res=(HttpServletResponse)response;

    StringBuffer URL=req.getRequestURL();

    System.out.println("filter URL="+URL);

    String url[]={".js",".html","login",".css","png","jpg"};

    boolean flag=false;

    String method=req.getParameter("method");

    if(method!=null) {

    if(method.equals("login")) {

    flag=true;

    }

    }

    for(String str:url) {

    System.out.println("index="+URL.indexOf(str));

    if(URL.indexOf(str)>0) {

    flag=true;

    break;

    }

    }

    if(!flag) {

    System.out.println("====== authfilter过滤user结果username=");

        User user=(User)req.getSession().getAttribute("user");

             if(user!=null) {

        chain.doFilter(request, response);

        }else {

       res.sendRedirect(req.getContextPath()+"/login.jsp");

       //chain.doFilter(request, response);

         }

    }else 

      {

    chain.doFilter(request, response);

      }

            

    }


    @Override

    public void init(FilterConfig filterConfig) throws ServletException {

    // TODO Auto-generated method stub


    }


    }


    2021-01-21 15:37:33
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师