我在 play 2.4 中编写应用程序,在执行操作之前,我想确保用户有权执行该操作。
我可以使用请求 header 中的信息检索当前用户,但如果可以像这样传递操作名称,这将很有用。
@With(Authorization.class , action="create_account")
public static Result createAccount(){
return ok("Account created");
}
然后授权可以做这样的事情。
public class Authorization extends Action.Simple{
@Override
public F.Promise<Result> call(Http.Context context) throws Throwable{
if(action == "zoom")
throw new UnauthorizedExcption("You can't do that!");
else
return delegate.call(context);
}
所以基本上我的问题是我需要将数据传递给授权类。有解决此类问题的方法吗?
最佳答案
自己创建一个接受参数的自定义注解:
@With(Authorization.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Auth {
String value() default "";
}
如下注释您的 Controller 方法:
@Auth("create-account")
public static Result createAccount(){
return ok("Account created");
}
要从注释中检索值,使Authorization
类扩展Action<Auth>
:
public class Authorization extends Action<Auth> {
String value = configuration.value();
// ...
}
关于java - 将信息传递给@With注解 Play Framework 进行用户授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31910606/