Tag: playframework 2.4

在playframework中拦截请求和检查授权

我正在使用Java play framework 2.4.2 ,我想通过拦截所有请求并检查是否设置了会话值来validation用户是否已登录。 所以我扩展了DefaultHttpRequestHandler并重写了createAction方法来拦截所有请求。 但是,我还没有找到一种validation会话的好方法。 选项1 – 失败 当我尝试获取会话值时,我得到一个运行时exception: There is no HTTP Context available from here 以下是我正在使用的课程: public class RequestHandler extends DefaultHttpRequestHandler { @Override public Action createAction(Http.Request request, Method method) { session(“loggedIn”); // Throws runtime Exception: no HTTP Context } } 选项2 – 丑陋 由于会话在技术上是一个cookie,我可以从头部检索值,如下所示: for(String cookie : request.headers().get(“Cookie”)){ System.out.println(“cookie: “+cookie); } […]

配置取决于启动模式

可以在开发模式(通过run ),生产模式(通过start )或测试模式下start播放。 有没有办法提供不同的配置文件( conf/application.conf ),具体取决于它在哪个模式下启动?

如何将某些东西注入表单中

从2.4.0开始,我们可以使用DI框架。 我想在我的应用程序中使用DI。 我将jpa finders从我的模型类中的静态方法移动到我注入到控制器中的服务层中的方法。 我的主要问题是我有一些带有validation方法的表单,在我的validation方法中,我使用了一些查找程序。 例如,在登录表单中,我使用“User.authenticate”方法。 现在我已经将这个静态方法替换为我的UserSvc上的新方法,我想将我的服务注入到我的表单中,但它不起作用。 似乎不可能在表单中注入一些内容,以便如何解决我的问题 public class MyController { // Inject here can be used in controller methods but not in the form validate method @Inject UserSvc userSvc; public static class Login { // Inject here is not filled : NPE @Inject UserSvc userSvc; public String email; public String password; public String […]

在Play Framework 2.4.x中解决CORS问题

我有一个java play框架2.4.x Web应用程序,提供JSON / HTTP API。 当我运行我的前端HTML / JS file:///Users/nize/tmp/index.html在http://localhost:9000 chrome file:///Users/nize/tmp/index.html调用API XMLHttpRequest cannot load http://localhost:9000. No ‘Access-Control-Allow-Origin’ header is present on the requested resource. Origin ‘null’ is therefore not allowed access. The response had HTTP status code 403. 我已按照Play Framework 2.4.x CORS文档中的说明配置了Web应用程序: 更新到build.sbt 将类Filters.java添加到项目的根目录(也尝试过/app ) 将以下节添加到application.conf play.filters.cors {allowedOrigins = [“*”,“ http:// localhost ”] #allowedHttpMethods […]