Take the 2-minute tour ×

When a user who is not logged in clicks on a link containing a remoteFunction like:

    <a href="#" onclick="${remoteFunction(
           controller: 'book',
           action: 'count',
        id: book.id,
        onSuccess: 'updateBookCount(data,textStatus);'
        add count to book

for an action:

def count() { ... }

How can I redirect the user to the login page, if he is not logged in?

share|improve this question

1 Answer 1

Maybe there is a better solution than what I currently have, but I ran into that in the past, where ajax calls don't call the normal redirect that spring security does when a session has expired. I assign contextPath to a javascript variable in main.gsp. This will log a user out 5 seconds before the session expires.

In main.gsp in the header I have:

<script type="text/javascript">
    var contextPath = "${contextPath}";
    var maxTimeout = ${session.getMaxInactiveInterval()};

In my main.js I have:

if ( contextPath + "/" != window.location.pathname )
    setInterval('checkTimeOut()', 1000 );

function checkTimeOut()
    maxTimeout -= 1;
    if (maxTimeout <= 5)
        window.location = contextPath+"/logout"

share|improve this answer
Can you explain this more detailed? When a logged out user tries to access the above link to a secured page the JavaScript function updateBookCount(data,textStatus) will get an error I guess. How do I have to handle it? –  confile Nov 13 '12 at 15:04
They will be redirected to the logout page before their session times out. So that will never happen ( if they have scripts turned on ). –  James Kleeh Nov 13 '12 at 20:44
What I want is that the user will be redirected to the login page, if he is not logged in and tries to access a secured function over ajax. How can I do that? –  confile Nov 13 '12 at 23:12
Short answer: you can't. –  James Kleeh Nov 14 '12 at 13:15

Your Answer


