Take the 2-minute tour ×

On login, a user can be redirected to the page that requested login using the url value set in flash.

Is is possible to have the same use case, redirect the user to the page that requested the logout action after logout?

share|improve this question
You have pages which request logout? Well, as you said, have those pages add an url-parameter to the request, ?page=/somepage.html), and use that for a redirect. –  Johan Sjöberg Feb 6 '11 at 12:21
Also you can store that url in cookies, through flash-javascipt bridge. –  kirilloid Feb 6 '11 at 12:24
What are using for login/logout? You own custom actions or are you using the secure module? –  Codemwnci Feb 6 '11 at 14:19
Why not use the referrer value in headers? X page contains logout link, click the link the reference came from X page so once the logout is done you can redirect to referrer, is that what you mean? –  allenskd Feb 6 '11 at 15:34

3 Answers 3

(Just elucidating allenskd's comment)

A lot depends on how your logout is implemented, but if it's just a controller or a jsp, you don't have to pass the url to it. You can use the referer header.

String refererUrl = request.getHeader("referer")
share|improve this answer
up vote 2 down vote accepted

thank you all for the comments on the question, going with Johan Sjöberg's suggestions, even stackoverflow is using it ;-), check the logout link.

On the template

<a href="@{Secure.logout2(request.url)}">logout</a>

In Secure.java i added logout2

public static void logout2(String returnUrl) throws Throwable {
    flash.put("returnUrl", returnUrl == null ? "/" : returnUrl);

and modified logout

public static void logout() throws Throwable {
    flash.success(Messages.get("secure.logout", "You have been successfully logged out"));

    String returnUrl = flash.get("returnUrl");
    redirect(returnUrl == null ? "/" : returnUrl);
share|improve this answer
I'm brand-spanking-new to the Play! framework (but absolutely loving it!) so maybe I'm off-base here but isn't it usually considered bad-practice to directly modify a framework's class??? I would have expected a solution that overrides a particular feature of a framework's class. For example, what happens when play#secure-module gets an upgrade that you want? You're going to have to make sure you police your code. See my solution for how I solved this problem. –  Bane Feb 25 '11 at 3:23

Having found the other answers here unacceptable (or just didn't quite fit my approach) I pinged-away until I came up with something I like better. Here's what I did:

public class Security extends Secure.Security {

    /* Overrides the same method from Secure.Security.
     * I only needed to redirect back the home-page so I just
     * hard-coded the redirect-url here; but you could also have
     * directed your logout-link to #logout2(String url) where you do a
     * flash#put() and then here do a flash#get() and redirect to
     * to that url instead (see n002213f's answer for more detail; 
     * but I thought it bad-practice to do the other half of his 
     * answer -- directly modifying the #logout() method of the
     * play#secure-module)
    static void onDisconnected() {
        redirect( "/" );


    (...) <a href="logout">Logout</a> (...)

So what happens here is that the default play#secure#logout() is called and at the end of the logout-process it fires Secure#Security#onDisconnected -- which is overridden and redirects to the home-page. This way play's own built-in security isn't being bypassed so you're not risking breaking your security, you also aren't directly modifying the play#secure-module code which means when it's updated you won't find yourself in a pickle, and yet you are achieving your result: not being redirected to the default play#secure-login page, but instead going straight back to the homepage.

If anyone more seasoned with Play! than I see a problem with this (or if my criticism of the other solutions is unfounded) I'd love to hear back!

Otherwise I think this is the most elegant and simplest way to achieve the goal.

share|improve this answer

Your Answer


