Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a HttpServletRequest in my spring beans?

I'm developing an app with a Flex-based front end and a Spring/Hibernate back-end.

To get Facebook integration working in the way I've got it currently, I need to read the cookies set in javascript on the front end on the back-end and do some validation during login to see whether the user is attempting to spoof his Facebook login.

This would be pretty easy, but I can't figure out how to get the HttpServletRequest. I'm using a pretty basic Spring config (this is my first real Spring app, and I'm pretty familiar with it now, but there's lots to it I've never looked at.)

I'm not using Spring MVC or Spring WebFlow or anything like that. I can get the ServletContext, but I haven't yet figured out how to get the request.

Any help?

like image 733
Jason Maskell Avatar asked Feb 17 '09 23:02

Jason Maskell


People also ask

How do I get HttpServletRequest in Spring?

HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder. getRequestAttributes()) . getRequest();

How do I get HttpServletRequest in Webflux?

The code to get it is as follows. ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder. getRequestAttributes(); // get the request HttpServletRequest request = requestAttributes. getRequest();

Can we Autowire HttpServletRequest?

Request-scoped beans can be autowired with the request object. Warning for Spring <=3.1 users the autowiring will not work running tests.

What is the use of HttpServletRequest in Spring boot?

Spring Boot HttpServletRequest Logging Filter. A Request Logging Filter for Spring Boot applications, that allows you to read the request body multiple times. The request body is cached using an overridden HttpServletRequestWrapper. Then a reader that allows the client to read the body multiple times is returned.


2 Answers

If FlexContext is not available:

Solution 1: inside method (>= Spring 2.0 required)

HttpServletRequest request =          ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes())                 .getRequest(); 

Solution 2: inside bean (supported by >= 2.5, Spring 3.0 for singelton beans required!)

@Autowired private HttpServletRequest request; 
like image 128
Gerrit Brehmer Avatar answered Sep 21 '22 14:09

Gerrit Brehmer


This is kind of Flex/BlazeDS specific, but here's the solution I've come up with. Sorry if answering my own question is a faux pas.

    HttpServletRequest request = flex.messaging.FlexContext.getHttpRequest();      Cookie[] cookies = request.getCookies();      for (Cookie c:cookies)     {         log.debug(String.format("Cookie: %s, %s, domain: %s",c.getName(), c.getValue(),c.getDomain()));     } 

It works, I get the cookies. My problem was looking to Spring - BlazeDS had it. Spring probably does too, but I still don't know how to get to it.

like image 30
Jason Maskell Avatar answered Sep 19 '22 14:09

Jason Maskell