Play Framework 2.X:在 Global.java 中处理/请求和重定向
Play Framework 2.X : Dealing w/ onReqest and redirect in Global.java
我是 Play Framework 的新手。我正在做一个基础项目,现在正在研究身份验证功能。我想将未经授权的用户重定向到 /login 路由。
我发现 Global.java class 允许我控制项目中的操作,特别是使用 onRequest 函数。我打算用它来做重定向。
我在网上搜索了多种解决方案,但找不到有效的解决方案。
我的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);
}
}
我找到了这个,但我不明白为什么要玩!不想编译:
error: <anonymous Global> 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
我对 Play 并不随意,我也不太明白这个问题。有谁可以帮助我吗 ?谢谢!
我有一段时间没有使用 Play Framework,但我认为问题是在 2.2 中他们对 return Promise 而不仅仅是 Result 采取了行动。所以你的问题出现了。
检查您的 Action.Simple.call() 版本是否匹配
Result call(Context ctx) throws Throwable
查看
之间的区别
https://www.playframework.com/documentation/2.2.x/api/java/index.html
https://www.playframework.com/documentation/2.1.x/api/java/index.html
(看调用方法的return类型)
编辑
我不确定这是否是最好的方法,但它应该有效。
@Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
F.Promise.pure() 可用于将结果(或任何实现它的东西,例如 Results.Status)转换为 Promise。
示例,其中 ok() returns play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));
我是 Play Framework 的新手。我正在做一个基础项目,现在正在研究身份验证功能。我想将未经授权的用户重定向到 /login 路由。
我发现 Global.java class 允许我控制项目中的操作,特别是使用 onRequest 函数。我打算用它来做重定向。
我在网上搜索了多种解决方案,但找不到有效的解决方案。
我的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);
}
}
我找到了这个,但我不明白为什么要玩!不想编译:
error: <anonymous Global> 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
我对 Play 并不随意,我也不太明白这个问题。有谁可以帮助我吗 ?谢谢!
我有一段时间没有使用 Play Framework,但我认为问题是在 2.2 中他们对 return Promise 而不仅仅是 Result 采取了行动。所以你的问题出现了。
检查您的 Action.Simple.call() 版本是否匹配
Result call(Context ctx) throws Throwable
查看
之间的区别https://www.playframework.com/documentation/2.2.x/api/java/index.html https://www.playframework.com/documentation/2.1.x/api/java/index.html
(看调用方法的return类型)
编辑
我不确定这是否是最好的方法,但它应该有效。
@Override
public F.Promise<Result> call(Context ctx) throws Throwable {
return F.Promise.pure(redirect(controllers.routes.Application.index()));
}
F.Promise.pure() 可用于将结果(或任何实现它的东西,例如 Results.Status)转换为 Promise。
示例,其中 ok() returns play.mvc.Results.Status:
F.Promise.pure(ok("[No Preview Available]"));