【问题标题】:doFilter with HttpServletRequestdoFilter 与 HttpServletRequest
【发布时间】:2018-01-31 10:33:57
【问题描述】:

我坚持使用 HttpServletRequest 的 doFilter。

我正在尝试替换该请求的新 URL。

我的代码如下:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    throws IOException, ServletException {

     HttpServletRequest httpReq = (HttpServletRequest) req;
     HttpServletResponse httpRes = (HttpServletResponse) res;

     //If request resources ==> Continue
     if(httpReq.getContextPath().startsWith(httpReq.getContextPath()+"/resources")){
         chain.doFilter(req, res);
         return;
     }
     HttpSession session = httpReq.getSession();
     EmployeeDTO currentEmployee =(EmployeeDTO)session.getAttribute("currentEmployee");
    //If dont have session ==> Return login page
     if(currentEmployee == null){
        String requestURI = "";
         requestURI = httpReq.getRequestURI().replace(httpReq.getRequestURI(), httpReq.getContextPath()+ "/login");
         System.out.println(requestURI);
         //httpRes.reset();
         //httpRes.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
         //httpRes.setHeader("Location", requestURI);
         httpRes.sendRedirect(requestURI);
         chain.doFilter(req, res);
        return;
     }
     chain.doFilter(req, res);
     return;
}

但上面的代码仍然无法正常工作。我该怎么做?

提前致谢!

【问题讨论】:

  • 我试图通过两种方式重定向到 orther uri。首先:用户 HttpResponse 尝试重写 httpRes.reset(); //httpRes.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); //httpRes.setHeader("位置", requestURI);第二:我尝试重定向,但仍然无法正常工作。谁知道我该怎么办?
  • @AmitKBist 我想重定向到页面登录的 URL。如果尝试使用 sendRedirect 那意味着我将直接到 jsp 页面

标签: java spring-mvc servlets


【解决方案1】:

为什么在设置重定向响应后必须做一个chain.doFilter()?

private FilterConfig filterConfig;

public void init(FilterConfig config) throws ServletException {
        this.filterConfig = config;
}
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
    throws IOException, ServletException {

     HttpServletRequest httpReq = (HttpServletRequest) req;
     HttpServletResponse httpRes = (HttpServletResponse) res;
         HttpSession session = httpReq.getSession();
         boolean requestResources = false;

     //If request resources ==> Continue
     if(httpReq.getContextPath().startsWith(httpReq.getContextPath()+"/resources")){
        requestResources =  true;
     }     
     if(session != null){
            EmployeeDTO currentEmployee =(EmployeeDTO)session.getAttribute("currentEmployee");
     }
     if(requestResources || currentEmployee != null){
            chain.doFilter();
     }else if(currentEmployee == null){         
         String loginURI = //hardcode the loginURI here. 
         httpRes.sendRedirect(loginURI);
         /*alternatively use the following to do an internal forward rather than a redirect      
         RequestDispatcher rd = filterConfig.getServletContext().getRequestDispatcher(loginURI); //here the loginURI path should not have the context in the url.
                rd.forward(servletReq, response);
         */
     }     

}

我还重构了您的代码以删除冗余和多个“返回”语句

【讨论】:

    猜你喜欢
    • 2014-11-14
    • 2017-05-14
    • 2016-11-25
    • 1970-01-01
    • 2011-04-09
    • 2012-09-23
    • 1970-01-01
    • 2013-01-12
    • 2014-11-21
    相关资源
    最近更新 更多