问题
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.Write custom action
public class Authorization extends play.mvc.Action.Simple {
public Promise<Result> call(Context ctx) throws Throwable {
//check access
return delegate.call(ctx);
}
}
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.
Thanks
回答1:
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.
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)
来源:https://stackoverflow.com/questions/25646335/play-framework-2-2-how-to-get-current-controller-and-action-in-java