title: Get access to HttpServletRequest within a custom AuthenticationProvider
body: <p>How do I get access to HttpServletRequest within a custom AuthenticationProvider. I have tried doing this</p>
  
  <pre><code>RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  
  HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
  String username = (String) httpReq.getAttribute("j_username");
  </code></pre>
  
  <p>OR</p>
  
  <pre><code>RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  HttpServletRequest httpReq = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();
  
  String username = (String) httpReq.getAttribute("j_username");
  </code></pre>
  
  <p>i am getting username null</p>
  
  <p>but <code>RequestContextHolder.getRequestAttributes();</code> returns <code>null</code>.</p>
  
  <p>I want to reference the requestcontext, pass it in, or have Spring do its Magic so I can reference it.</p>
  
  <p>I am also giving the RequestContextListener in my web.xml</p>
  
  <pre><code>  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
    &lt;/listener&gt;
  </code></pre>
  
  <p>I have searched the security forum but still havent found anything yet.</p>
  
  <p>Thanks in advance. </p>