doFilter 与 HttpServletRequest

Posted

技术标签:

【中文标题】doFilter 与 HttpServletRequest【英文标题】:doFilter with 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);第二:我尝试重定向,但仍然无法正常工作。谁知道我该怎么办? 检查***.com/questions/2725102/…或***.com/questions/5308801/… @AmitKBist 我想重定向到页面登录的 URL。如果尝试使用 sendRedirect 那意味着我将直接到 jsp 页面 【参考方案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);
         */
          


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

【讨论】:

以上是关于doFilter 与 HttpServletRequest的主要内容,如果未能解决你的问题,请参考以下文章

Servlet过滤器重定向:如何在chain.doFilter(request,resp)之后重定向;

在 doFilter 中设置后获取 Cookie

FacesContext#getCurrentInstance() 在 Filter#doFilter() 中返回 null

doFilter() 重写方法 httpRequest.getUserPrincipal() 总是返回 NullPoint 异常

Java filter中的chain.doFilter详解

在网关级别的 dofilter 中为请求添加自定义标头