3

I access my sessions in Spring MVC controllers simply with @Autowired like this:

@Autowired
private HttpSession session;

The problem is, I have now access the session within a ClientHttpRequestInterceptor.

I tried it with RequestContextHolder.getRequestAttributes() but the result is (sometimes - and this is a real problem) null. I tried it also with RequestContextHolder.currentRequestAttributes() but than the IllegalStateException is thrown with the following message:

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/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

The RequestContextListener is registered in web.xml.

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

The same problem when I inject the session in the ClientHttpRequestInterceptor directly.

@Autowired
private HttpSession session;

My question is: how can I access the current HttpSession in the ClientHttpRequestInterceptor?

Thanks!

Robert Moszczynski
  • 1,081
  • 2
  • 16
  • 26

2 Answers2

1

You can access the HttpSession by using the following in your ClientHttpRequestInterceptor:

public class CustomInterceptor implements ClientHttpRequestInterceptor {
        @Override
        public ClientHttpResponse intercept(HttpRequest request,
                                            byte[] body,
                                            ClientHttpRequestExecution execution) throws IOException {
            HttpServletRequest httpServletRequest = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
            // get the current session without creating a new one
            HttpSession httpSession = httpServletRequest.getSession(false);
            // get whatever session parameter you want
            String sessionParam = httpSession.getAttribute("parameter")
                                             .toString();
        }
    }
Osama Sayed
  • 1,993
  • 15
  • 15
0

I think the usage of this ClientHttpInterceptor class is not in the right place, because as what I see in their doc

Intercepts client-side HTTP requests. Implementations of this interface can be registered with the RestTemplate

means that you can use this interceptor if your server behaves likes a client and for example you want to modify every request you send with RestTemplate. Sample usage for ClientHttpInterceptor here.

You should use HandlerInterceptor . See good article here.

Nikolay Rusev
  • 4,060
  • 3
  • 19
  • 29
  • Yes, my server is also a client of a REST-API. I need to modify every request, by adding some credential headers to it. Some header needs information from the session for example a special user token. – Robert Moszczynski Sep 30 '15 at 11:59
  • Sure, the `preHandle` method of the `HandlerInterceptor` has the `HttpServletRequest` from I can get the session. The problem is, I need to intercept every `RestTemplate` request with it. – Robert Moszczynski Sep 30 '15 at 12:05
  • just extract the needed auth header from the `HandlerInterceptor`, pass it to the `ClientHttpInterceptor` and register your RestTemplate with your `ClientHttpInterceptor`. – Nikolay Rusev Sep 30 '15 at 12:09
  • How can I pass data from one interceptor to another? – Robert Moszczynski Sep 30 '15 at 12:13
  • think about the interceptors like `classes` because they are classes. So your question is "how to pass data from one class to other". See here http://mph-web.de/01_10_2013_resttemplate_basic_auth – Nikolay Rusev Sep 30 '15 at 12:19
  • Yes, sure, but if i try to get the session from a not managed bean I get the exceptions described above. Is there really no way in Spring to inject a `HttpSession` in an interceptor? – Robert Moszczynski Sep 30 '15 at 13:22