2011-02-06 108 views
0

login,用户可以被重定向到所请求的登录使用urlflash设置页面后。重定向到前网址注销

可能有相同的用例,将用户重定向到注销后请求注销操作的页面?

+0

您有需要注销的页面吗?那么,正如你所说的,让这些页面为请求添加一个url参数,并将其用于重定向。 – 2011-02-06 12:21:16

+0

您还可以通过flash-javascipt桥将该URL存储在cookie中。 – kirilloid 2011-02-06 12:24:51

回答

2

感谢大家对这个问题的意见,用Johan Sjöberg的建议,甚至stackoverflow正在使用它;-),检查注销链接。

在模板

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

Secure.java我加入logout2

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

和修改logout

public static void logout() throws Throwable { 
    session.clear(); 
    response.removeCookie("rememberme"); 
    Security.invoke("onDisconnected"); 
    flash.success(Messages.get("secure.logout", "You have been successfully logged out")); 

    String returnUrl = flash.get("returnUrl"); 
    redirect(returnUrl == null ? "/" : returnUrl); 
} 
8

(只是阐明allenskd的评论)

很大程度上取决于你的注销是如何实现的,但是如果它只是一个控制器或一个jsp,你不必将url传递给它。您可以使用referer标题。

String refererUrl = request.getHeader("referer") 
2

已经找到其他的答案在这里不可接受(或只是不太符合我的方法),我ping通-距离,直到我来到了我喜欢的东西更好。下面是我所做的:

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("/"); 
    } 

} 


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

所以这里所发生的是默认的播放#安全#注销()被调用,并在退出过程结束它触发安全#安全#onDisconnected - 这是覆盖并重定向到主页。这种方式发挥自己的内置安全性没有被绕过,所以你没有冒险打破你的安全性,你也不是直接修改play#secure-module代码,这意味着它在更新时不会发现自己处于泡菜,但你正在实现你的结果:没有被重定向到默认的播放#安全登录页面,而是直接回到主页。

如果有人使用Play更经验丰富!比我看到这个问题(或者如果我对其他解决方案的批评是​​没有根据的),我很乐意听到!

否则我认为这是实现目标的最优雅和最简单的方法。