Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6677737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:09:30+00:00 2026-05-26T04:09:30+00:00

I am trying to implement a filter that uses MockHttpServletRequest to add a header

  • 0

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..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:09:30+00:00Added an answer on May 26, 2026 at 4:09 am
    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement a query in LINQ that uses a left outer join
I am trying to implement a Filter/Pipeline pattern so that I can take an
I'm trying to implement chained filter dropdown using just javascript - jquery. The solution
when trying to implement an Aspect, that is responsible for catching and logging a
im trying to implement a custom error page, what i want to be able
I am trying to implement Custom Error handling via Action Filter Attributes. My code
I'm trying to implement row-based security checks for Django models. The idea is that
I'm trying my hand in designing an application in JavaScript that is object oriented
I am trying to filter the jqGrid data by date range - that is,
I'm about to implement a logging class in C++ and am trying to decide

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.