2

I am using Spring MVC, Spring security for our web application. After successful login the request gets redirected to a HTML page (login.html). We have about 4 AJAX calls that are executed then after using Ext-JS.

I have a requirement to send JSON data after successful login. What this means is the JSON data has to be passed on to the client along with the login page. And on the login page we need to access this JSON data and render it. Frankly this is the same data which requested by the Ext-JS code on the login.html page. The goal is to avoid additional requests by AJAX call.

I tried out various examples on stackoverflow and other websites (just avoiding mention, to prevent cluttering) and created a custom authentication handler, and made changes appropriately. Everything works fine but not a way to send JSON/any data to client.

The below are the configurations of application-context.xml

    <http auto-config="false" use-expressions="true">
        <form-login login-page="/login"
                    login-processing-url="/static/j_spring_security_check"                    
                    authentication-failure-url="/login?login_error=t"
                    authentication-success-handler-ref="customAuthHandler"
                    authentication-failure-handler-ref="customAuthHandler"
                    default-target-url="/html/index.html"  
                    always-use-default-target="true"/>

    <beans:bean id="customAuthHandler"
           class="com.mycompany.security.CustomAuthenticationHandler"/>

On the onAuthenticationSuccess method in CustomAuthenticationHandler I have the following code to see if I could get this value, but I could not. What I understand is since its a HTML/static content I can't pass session value to client and that needs to be a JSP. (Please correct me if wrong!) I can't change the HTML to JSP for bureaucratic reasons:

Test 1:

   request.getSession(true).setAttribute("ssntest", "hello:how:are:you");

Test 2:

    HttpServletResponseWrapper responseWrapper = new HttpServletResponseWrapper(response);

    Writer out = responseWrapper.getWriter();

    String targetUrl = "/login";
    out.write("{success:true, targetUrl : \'" + targetUrl + "\'}");
    out.close();

What would be a way to pass JSON value on successful authentication along with HTML.

Rene Saarsoo
  • 13,580
  • 8
  • 57
  • 85
oneworld
  • 770
  • 4
  • 17
  • 31
  • I am not sure if the question is unclear, please suggest so I cold update it in a better way. Thanks. – oneworld Mar 15 '12 at 17:19
  • Anybody, please help me on this. Thanks in advance. – oneworld Mar 16 '12 at 06:17
  • I think it's a bit unclear indeed. I didn't really understand your problem besides the fact that you are trying to pass JSON data to login.html but aren't succeeding. But I didn't really understand how exactly are you trying to pass along this JSON data (in terms of HTTP/HTML/JavaScript). But I don't know Spring and all this Java-stuff, so I'm probably not much of a help here. – Rene Saarsoo Mar 16 '12 at 12:05
  • @Rene what you understood is right perfectly. When the request is forwarded to loing.html by Spring MVC I want the JSON data to be passed along. The test program mentioned is what being used to pass the JSON data. Please ignore the data which I claim as JSON but not strictly JSON. Please consider it as any data to be passed to HTML. – oneworld Mar 17 '12 at 09:59

1 Answers1

0

Keeping this kind of data in session isnt a good approach, but you can try the @SessionAttributes annotation, provided by SpringMVC. See another answer

The point is to save your JSON value into session in the login page, and then return it back in another AJAX request after successful authentication.

Community
  • 1
  • 1
vacuum
  • 2,273
  • 3
  • 20
  • 32