0

Here I am make one application which is fully remote based.

only once all js and css loaded. So I want need solution for session expired.

Means when ajax request come to controller, it will be authenticated first, for session if session expired then I want to redirect to login page relogin.

[EDIT - @Brad]

Let's try and clarify your requirement..

1) make request to server via Ajax

2) server responds with HTTP 401, not authorized

3) Ajax recognises 401 status and redirects to Login page

4) login success,

5) then old request executed. means step 1 request executes.

6) but I want dont want to execute that step-1 request but want to execute home page everytime.

sanghavi7
  • 758
  • 1
  • 15
  • 38
  • Is the controller javascript function? If so you can have a timer which will be reset on every request. If it is timed out then you can redirect to login page on window.location.href="loginpage" – Sudhakar B May 25 '12 at 07:56
  • there is one problem , I tried it but on next login it goes to that action directly and renders all nonformated data. – sanghavi7 May 25 '12 at 07:59
  • You mean login page itself is rendering as your data ? If that is the case render javascript which redirects to your login page. – Sudhakar B May 25 '12 at 08:47
  • no, not login page, but on login successful it executes that action directly, and render its outputs. Means in cotext that method path is there and directly goes there. – sanghavi7 May 25 '12 at 08:55
  • I am not understanding your problem clearly, can you please elaborate with example or sample code how you are doing and what you want. I think we can do a work around for that. – Sudhakar B May 25 '12 at 08:59
  • I edited your question @ankit5607san. Can you please amend step 4 if this is not what you are wanting – Brad May 25 '12 at 09:06

2 Answers2

2

You could use a filter interceptor before the request comes on to your action every time.

A better way to do this is by using the apache-shiro authentication plugin available for grails.

See this : Apache Shiro Integration for Grails

Install this in your application and configure it. Its simple and takes care of most of your authentication requirements.

COD3BOY
  • 11,964
  • 1
  • 38
  • 56
srjit
  • 526
  • 11
  • 25
  • but I have used spring security pluing. In that plug in where I have to change plz tell me!! – sanghavi7 May 25 '12 at 08:02
  • I haven't used spring security but it seems fairly easy. Please read the section **Securing AJAX requests** http://blog.springsource.com/2010/08/11/simplified-spring-security-with-grails/ – srjit May 25 '12 at 08:21
2

One approach is to have your Client side AJAX code check for a HTTP 401 response code which means that your users session has timeout and they are no longer logged in.

Using JQuery...

$.ajax({
    type: "post", url: "/SomeController/SomeAction",
    success: function (data, text) {
        //...
    },
    error: function (request, status, error) {
        // look for status of 401 and redirect to login
    }
});

Of course you just have to make sure your web app is returning the appropriate status codes. I found this article on the subject to be thought provoking.

There is also some good information on a similar question

[EDIT]

OK, from your updated question at step 3) this is occuring over an ajax request and I am expecting that your user does not see a login page because your ajax handling code will receive the 401 response and you can decide not to display it. You can then redirect the users browser to your login URL of choice. In this case you would redirect to the home page, or configure your login page to accept an additional URL parameter to tell it where you want the user to be sent to once they have logged in.

Community
  • 1
  • 1
Brad
  • 15,186
  • 11
  • 60
  • 74
  • yeah that I know but plz read above discussion and comments then you will understand problem... – sanghavi7 May 25 '12 at 08:57
  • @ankit5607san - you havent clarified weather you are using shiro or spring security or some thing of your own ? – Sudhir N May 25 '12 at 15:02