3

We are using SocialAuth for authentication with Spring 3 and having issue with sessions between redirects.

When we do return "redirect:myURL"; in controller (after authentication) redirection happening properly but, very frequently redirect creating new session instance instead of using my current session which results in NullPointerException when I look for certain variables I have stored in session.

I have added few System.out statements to prove that new session is being created.

Session id is before authentication...E7557680F892C6776F36691E9DF93CF5
session ID after authentication and redirct....     726C2EE5C240BDE4D217CBFDB9C47A0F

How can I store current session in Spring application context before redirect and use it after redirect based on session ID? Is there any other way we can work around this in Spring?

kosa
  • 65,990
  • 13
  • 130
  • 167

1 Answers1

1

Have a look at RedirectAttributes Interface in Spring, it' is built for the same purpose you are trying to implement. This basically preserve values between redirects and hence will eliminate your need of putting attributes in session and then getting them.

Here's the link

http://docs.spring.io/spring/docs/3.1.x/javadoc-api/org/springframework/web/servlet/mvc/support/RedirectAttributes.html

Santosh Joshi
  • 3,290
  • 5
  • 36
  • 49
  • What is the scope of these RedirectAttributes? Is it session? If so, this may not work in my case. – kosa Feb 19 '14 at 17:34
  • 1
    If you look at the API, you will generally get two method `addFlashAttribute ` and `addAttribute `. `addFlashAttribute` stores the object flashmap which is internally stored in session, these varaibles get removed once the redirect is complete while the `addAttribute` create the request parameters from your attributes – Santosh Joshi Feb 19 '14 at 17:53