在我的玩!应用程序,我已经编写了controllers.Security作为登录playframework后页面
class Security extends controllers.Secure.Security {
...
static void onDisconnected() {
Application.index();
}
static void onAuthenticated() {
User user = User.find("byEmail",Security.connected()).first();
if(user.isAdmin()) {
Admin.index();
}else {
System.out.println("onAuthenticated()::user not admin");
}
}
我已经设置了路线为
GET /admin/? Admin.index
* /admin module:crud
GET / Application.index
当我在页面上说pageX属性,然后单击登录链接,将显示登录表单和我可以登录。如果我以管理员身份登录,它会带我到Admin.index(),从而进入Admin/index.html视图。到目前为止很好
但是,当我在pageX上,然后单击在登录链接上,我期望回到pageX。相反,Application.index()方法被调用,我被带到Application.index.ht毫升.. 这是预期的行为? 登录后需要做什么才能到达pageX?
更新:
我试图存储使用@Before在安全控制器
class Security extends controllers.Secure.Security {
@Before
static void storeCurrentUrl() {
System.out.println("storeCurrentUrl()");
flash.put("url", "GET".equals(request.method) ? request.url : "/");
}
static boolean authenticate(String username, String password) {
...
}
static void onAuthenticated() {
...
String url = flash.get("url");
System.out.println("url="+url);
if(!user.isAdmin()) {
if(url!=null) {
System.out.println("url not null");
redirect(url);
}else {
System.out.println("url null ..go to /");
redirect("/");
}
}
}
当我登录,我得到这些终端输出闪存中的URL
url=null
url null ..go to/
index()
我已经把main.html模板中的所有其他页面继承的登录/注销链接
<div id="main">
<div class="auth">
<a href="@{Admin.index()}">Go to Admin Area</a><br/><br/>
<a href="@{Secure.login()}">Login</a><br/><br/>
<a href="@{Secure.logout()}">Log out</a>
</div>
感谢reply..however当我尝试这样做,我收到网址为null.It似乎@Before不executed.I已经更新了question..Will请你看看? –
要调用@Before方法,它应该是定义传入操作的同一个Controller的一部分。这里storeCurrentUrl()不在控制器中,其中login()动作被定义。因此,storeCurrentUrl()永远不会被调用。 – sojin
你的情况会有点棘手,因为Play是无状态的,它不会将url存储到当前页面(pageX),以便在下一次请求时引用它。一种方式可能是(尽管我觉得它有点偏离轨道),当页面交付时,你可以保持当前页面的URL为flash,以便在下一次请求时(在失败的认证上)重定向。 – sojin