Play framework 2.2:如何在java中获取当前控制器和动作 [英] Play framework 2.2: How to get current controller and action in java

查看:241
本文介绍了Play framework 2.2:如何在java中获取当前控制器和动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,身份验证方案基于用户和角色。具有特定角色的用户可以访问特定的操作方法。

In my application authentication scheme is based on user and role. users with particular role have access to particular action method.

为了实现这一点,我已经完成了以下操作

To implement this I have done following

1。编写自定义操作

public class Authorization extends play.mvc.Action.Simple {
    public Promise<Result> call(Context ctx) throws Throwable {
        //check access
        return delegate.call(ctx);
    }
}

2.使用操作注释控制器

2.Annotate controller with action

@With(actions.Authorization.class)
public class Upload extends Controller {
    ....
}

在我的操作中,我可以访问正在进行会话的用户。我想获得当前的控制器和操作,以便我可以验证用户。有没有办法解决这个问题?

In my action I have access to user which is in session. I want to get current controller and action so that I can authenticate user. Is there any way to this?

我已经阅读了有关使用参数创建自定义注释的信息,并在每个控制器中传递参数以识别操作。但是如果错误的话我会在复制粘贴时写错了动作名称,这似乎有很多工作和容易出错。

I have read about creating custom annotation with params and in each controller pass parameters to identify the action. But it seems to much work and error prone if by mistake i write wrong action name while copy paste.

谢谢

推荐答案

您可以使用 context.request()。path()来获取包含所用路径的字符串。这将不包括任何查询参数,因此如果您需要它们,您可以使用 getQueryParameter uri (完整网址)方法同一个对象。

You can use context.request().path() to get a string with the path used. This will not include any query parameters, so if you want them you can use getQueryParameter or uri (full url) methods of the same object.

如果您需要更多信息,可以使用 context.args 。这将为您提供一个包含以下键的地图(至少在Play 2.2中):

If you need more information than this you can use context.args. This will give you a map with the following keys (in Play 2.2 atleast):

ROUTE_VERB          -> The HTTP verb used (ex: GET)
ROUTE_ACTION_METHOD -> The method called (ex: getUserFavorites)
ROUTE_CONTROLLER    -> Controller class (ex: controllers.api.UsersController)
ROUTE_COMMENTS      -> ???
ROUTE_PATTERN       -> THE URL used (ex: users/favorites)

这篇关于Play framework 2.2:如何在java中获取当前控制器和动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆