[英]Play Framework 2.X : Dealing w/ onReqest and redirect in Global.java
I'm new to Play Framework. 我是Play框架的新手。 I'm working on a basic project and i'm working now on the authentication function.
我正在做一个基础项目,现在正在做身份验证功能。 I want to redirect the unauthorized user to the /login route.
我想将未经授权的用户重定向到/ login路由。
I discover the Global.java class that allows me to control actions accross my project, in particular with onRequest function. 我发现Global.java类使我可以控制整个项目中的动作,尤其是onRequest函数。 I'm planning of using it to do the redirection.
我正计划使用它来进行重定向。
I search several solutions on the web but I couldn't find a working one. 我在网上搜索了几种解决方案,但找不到可行的解决方案。
My class: 我的课:
import play.*;
import play.mvc.Action;
import play.mvc.*;
import play.mvc.Http.*;
import play.mvc.Result.*;
import play.libs.F.*;
import static play.mvc.Results.*;
import play.mvc.Http.Request;
import java.lang.reflect.Method;
public class Global extends GlobalSettings {
@Override
public Action onRequest(Request request, Method actionMethod) {
//Check if the user is connected
if (request.cookie("PLAY_SESSION") == null && !request.path().startsWith("/login")) {
System.out.println("UNAUTHORIZED");
return new Action.Simple() {
@Override
public Result call(Context ctx) throws Throwable {
return redirect(controllers.routes.Application.index());
}
};
}
return super.onRequest(request, actionMethod);
}
}
I found this and i don't understand why Play! 我发现了这个,但我不明白为什么要玩! doesn't want to compile :
不想编译:
error: <anonymous Global$1> is not abstract and does not override abstract method call(Context) in Action error: method does not override or implement a method from a supertype
I'm not casual with Play and i don't really understand the problem. 我对Play并不随意,而且我不太了解这个问题。 Can someone help me please ?
有人能帮助我吗 ? Thanks !
谢谢 !
I haven't used Play Framework for a while now but I think that the problem is that in 2.2 they made Action to return Promise and not just Result. 我已经有一段时间没有使用Play Framework了,但是我认为问题是在2.2版本中他们使Action可以返回Promise而不是Result。 Hence there is your problem.
因此,有您的问题。
Check your version of Action.Simple.call() that it matches 检查您匹配的Action.Simple.call()版本
Result call(Context ctx) throws Throwable
See the difference between 看到之间的区别
https://www.playframework.com/documentation/2.2.x/api/java/index.html https://www.playframework.com/documentation/2.1.x/api/java/index.html https://www.playframework.com/documentation/2.2.x/api/java/index.html https://www.playframework.com/documentation/2.1.x/api/java/index.html
(look at the return type of the call method) (查看调用方法的返回类型)
EDIT 编辑
I am not sure whether this is the best approach but it should work. 我不确定这是否是最好的方法,但它是否应该起作用。
@Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
F.Promise.pure() can be used to convert a Result (or anything that implements it, like Results.Status for example) to a Promise. F.Promise.pure()可用于将Result(或实现它的任何东西,例如Results.Status)转换为Promise。
Example, where ok() returns play.mvc.Results.Status: 例如,ok()返回play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.