MockHttpServletRequest 没有将 URL 传递给链中的下一个过滤器

发布于 2024-12-09 11:52:44 字数 2390 浏览 0 评论 0原文

我正在尝试实现一个使用 MockHttpServletRequest 向请求对象添加标头的过滤器。我想使用该标头进行预身份验证。这是过滤器类..

public class MockAuthFilter implements Filter{

   private FilterConfig filterConfig = null;
   private static String loggedInUserName = "myId";
   private static String httpRequestHeaderName = "SM_ID";
   private Logger logger = Logger.getLogger(MockAuthFilter.class);

   @Override
   public void destroy() {
     this.filterConfig = null;
   }

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

      if(this.filterConfig.getServletContext() == null){}

      HttpServletRequest httpRequest = (HttpServletRequest) request;        
      MockHttpServletRequest mRequest = new MockHttpServletRequest(this.filterConfig.getServletContext());
      if(mRequest.getHeader(httpRequestHeaderName)==null ||
            !mRequest.getHeader(httpRequestHeaderName).equalsIgnoreCase(loggedInUserName))
        mRequest.addHeader(httpRequestHeaderName, loggedInUserName);

      mRequest.setMethod("GET");
      mRequest.setRequestURI(httpRequest.getRequestURL().toString());
      logger.debug("**********************exiting doFilter() method*****************");
      chain.doFilter(mRequest, response);
     }

@Override
public void init(FilterConfig config) throws ServletException {
    this.filterConfig = config;
}

}

但是当请求到达 Spring Security 过滤器链中的下一个过滤器时,没有填充 url。我在日志文件中看到以下几行。

  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [com.app.filter.MockAuthFilter:doFilter:55] - **********************exiting doFilter() method*****************
  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [com.app.filter.MockAuthFilter:doFilter:55] - **********************exiting doFilter() method*****************
  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [org.springframework.security.web.util.AntPathRequestMatcher:matches:103] - Checking match of request : ''; against '/static/**'
   [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1][org.springframework.security.web.util.AntPathRequestMatcher:matches:103] - Checking match of request : ''; against '/static/**'

正如您所看到的,请求对象中没有 url 传递到 AntPathrequestMatcher 的 matches 方法。 我在 chain.doFilter() 方法之前检查了模拟请求对象,它在其 requestURI 字段中包含 url 值。如果 URI 和 URL 在这里不是同一件事,我应该在这里进行哪些更改,以便 url 保留在请求对象中。

I am trying to implement a filter that uses MockHttpServletRequest to add a header to the request object. I want to use that header for preauthentication. This is the filter class..

public class MockAuthFilter implements Filter{

   private FilterConfig filterConfig = null;
   private static String loggedInUserName = "myId";
   private static String httpRequestHeaderName = "SM_ID";
   private Logger logger = Logger.getLogger(MockAuthFilter.class);

   @Override
   public void destroy() {
     this.filterConfig = null;
   }

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

      if(this.filterConfig.getServletContext() == null){}

      HttpServletRequest httpRequest = (HttpServletRequest) request;        
      MockHttpServletRequest mRequest = new MockHttpServletRequest(this.filterConfig.getServletContext());
      if(mRequest.getHeader(httpRequestHeaderName)==null ||
            !mRequest.getHeader(httpRequestHeaderName).equalsIgnoreCase(loggedInUserName))
        mRequest.addHeader(httpRequestHeaderName, loggedInUserName);

      mRequest.setMethod("GET");
      mRequest.setRequestURI(httpRequest.getRequestURL().toString());
      logger.debug("**********************exiting doFilter() method*****************");
      chain.doFilter(mRequest, response);
     }

@Override
public void init(FilterConfig config) throws ServletException {
    this.filterConfig = config;
}

}

but there is not url populated when the request reaches the next filter in the filter chain of Spring Security. I see following lines in the log file..

  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [com.app.filter.MockAuthFilter:doFilter:55] - **********************exiting doFilter() method*****************
  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [com.app.filter.MockAuthFilter:doFilter:55] - **********************exiting doFilter() method*****************
  [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1] [org.springframework.security.web.util.AntPathRequestMatcher:matches:103] - Checking match of request : ''; against '/static/**'
   [2011-10-13 16:52:35,114] [DEBUG] [http-8080-1][org.springframework.security.web.util.AntPathRequestMatcher:matches:103] - Checking match of request : ''; against '/static/**'

As you can see there was no url in the request object passed onto AntPathrequestMatcher's matches method..
I have checked mockrequest object right before chain.doFilter() method and it contains the url value in its requestURI field. if URI and URL aren't the same thing here, what changes should I make here so that url is maintained in the request object..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春如此纠结 2024-12-16 11:52:44
  1. 不要使用 MockHttpServletRequest像这样。它用于测试,而不是用于生产代码。将请求和/或响应包装在过滤器中以稍后修改其行为的适当方法是使用 HttpServletRequestWrapperHttpServletResponseWrapper
  2. 如果您只想添加标头,为什么还要尝试包装或删除原始请求?只需使用 addHeader() 对传入的请求。
  3. 尝试在过滤器中更改请求方法和请求 URI 可能会产生意想不到的后果,并且几乎肯定不会执行您可能期望的操作。当请求到达过滤器时,过滤器链已经根据请求的原始状态构建起来。现在改变它不会影响它最终的去向。
  1. Don't use MockHttpServletRequest like that. It's for testing, not for production code. The appropriate way to wrap a request and/or response in a filter to modify its behavior later on is with HttpServletRequestWrapper and HttpServletResponseWrapper.
  2. Why are you even trying to wrap or remove the original request if all you want is to add a header? Just use addHeader() on the incoming request.
  3. Trying to change the request method and request URI in the filter as you're doing may have unexpected consequences and will almost certainly not do what you probably expect it to do. By the time the request hits your filter, the filter chain has already been built based on the original state of the request. Changing it now won't affect where it ends up going.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文