75

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?

Jason Maskell
  • 1,716
  • 1
  • 17
  • 26

6 Answers6

189

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;
Chris Williams
  • 11,647
  • 15
  • 60
  • 97
Gerrit Brehmer
  • 2,077
  • 1
  • 14
  • 10
  • 7
    You can also use the @Resource annotation instead of Autowired. You don't need to require the autowiring explicitly then, and you're binding to Spring a little less. – pstobiecki Feb 11 '16 at 08:04
  • 2
    Autowired has `boolean required() default true;` – Ghandhikus Mar 18 '17 at 11:27
  • An Edit suggests to remove the info, that for singelton beans Spring >= 3 is required. There is a bug/missing feature in Spring 2.5, that injection of request scoped beans into singelton beans is not working correctly – Gerrit Brehmer Nov 01 '18 at 21:21
  • 2
    Getting Error : 'No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread In this case, use RequestContextListener or RequestContextFilter to expose the current request – Bhargav Patel Nov 30 '18 at 15:46
  • My IDE shows, `getRequestAttributes()` could be nullable here. Does it guarantee that this will not be null? – Yubaraj Aug 07 '22 at 12:43
  • yes thats right, in solution 1 a NPE is possible if the code snippet is used outside of an http request – Gerrit Brehmer Aug 08 '22 at 15:37
12

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.

Jason Maskell
  • 1,716
  • 1
  • 17
  • 26
10

@eeezyy's answer didn't work for me, although I'm using Spring Boot (2.0.4) and it may differ, but a variation here in 2018 works thus:

@Autowired
private HttpServletRequest request;
chrismacp
  • 3,834
  • 1
  • 30
  • 37
  • 3
    It'll throw fatal: `java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.` – Ahmad Asjad Mar 09 '22 at 16:23
7

this should do it

((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest().getRequestURI();
6

The @Context annotation (see answers in this question :What does context annotation do in Spring?) will cause it to be injected for you.

I had to use

@Context
private HttpServletRequest request;
Community
  • 1
  • 1
eeezyy
  • 2,109
  • 1
  • 15
  • 18
-2

Better way is to autowire with a constructor:

private final HttpServletRequest httpServletRequest;

public ClassConstructor(HttpServletRequest httpServletRequest){
      this.httpServletRequest = httpServletRequest;
}
Mateusz Niedbal
  • 326
  • 4
  • 15