Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get access to HttpServletRequest within a custom AuthenticationProvider

How do I get access to HttpServletRequest within a custom AuthenticationProvider. I have tried doing this

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();

HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
String username = (String) httpReq.getAttribute("j_username");

OR

RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
HttpServletRequest httpReq = ((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes()).getRequest();

String username = (String) httpReq.getAttribute("j_username");

i am getting username null

but RequestContextHolder.getRequestAttributes(); returns null.

I want to reference the requestcontext, pass it in, or have Spring do its Magic so I can reference it.

I am also giving the RequestContextListener in my web.xml

  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

I have searched the security forum but still havent found anything yet.

Thanks in advance.

like image 608
pradeep Avatar asked Jul 05 '12 16:07

pradeep


1 Answers

I just added the listener to my web.xml and RequestContextHolder.getRequestAttributes() returns the RequestAttributes instead of null.

<listener>
  <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

If you want to customize you login form, why you don't extend this filter: org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter

like image 164
Abel ANEIROS Avatar answered Sep 28 '22 05:09

Abel ANEIROS