我想在这部分代码中添加当前的URL(returnTo - string类型) :href="@routes.AuthenticationController.login(**returnTo**)"
。我想用它在登录后重定向到请求的URL。现在,如果用户没有登录,页面上会显示一条消息,显示未授权的403,并且必须使用登录按钮将他们重定向到登录页面。例如,如果我在http://localhost:9000/getByProductId/43上,在登录后我想要进入这个页面。
登录函数如下所示:
public static Result login(String returnTo) {
return ok(views.html.forms.loginForm.render(form(Login.class), returnTo));
}
包含受限制消息的页面如下所示
<div class="hero-unit center">
<h1>Access Restricted <small><font face="Tahoma" color="red">Unauthorized 403</font></small></h1>
<br />
@if(User.findCurrentUser() == null || User.findCurrentUser().getIdentifier() == null) {
<p>You have to login first to access this functionality.</p>
<p>
<a href="@routes.AuthenticationController.login(**returnTo**)" class="btn btn-large btn-info" ><i class="icon-home icon-white"></i> Log in</a>
}
发布于 2020-07-17 21:22:36
您可以从RequestHeader对象中获取当前URL
public static Result login() {
String uri = request().uri();
return ok(views.html.forms.loginForm.render(form(Login.class), uri));
}
关于RequestHeader的文档:https://www.playframework.com/documentation/2.7.x/api/java/play/mvc/Http.RequestHeader.html
https://stackoverflow.com/questions/62913922
复制相似问题