将信息传递给@With注解播放框架进行用户授权
Passing information to @With annotation play framework for user authorization
我正在 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);
}
基本上我的问题是我需要将数据传递给授权 class。有解决此类问题的方法吗?
我通过在会话之间传递数据来完成类似的事情。看看下面的链接!希望这可以帮助! :)
- Play framework 2: Passing java objects between controllers
- https://www.playframework.com/documentation/2.0/JavaSessionFlash
自己创建一个接受参数的自定义注释:
@With(Authorization.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Auth {
String value() default "";
}
按如下方式注释您的控制器方法:
@Auth("create-account")
public static Result createAccount(){
return ok("Account created");
}
要从注释中检索值,使 Authorization
class 扩展 Action<Auth>
:
public class Authorization extends Action<Auth> {
String value = configuration.value();
// ...
}
我正在 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);
}
基本上我的问题是我需要将数据传递给授权 class。有解决此类问题的方法吗?
我通过在会话之间传递数据来完成类似的事情。看看下面的链接!希望这可以帮助! :)
- Play framework 2: Passing java objects between controllers
- https://www.playframework.com/documentation/2.0/JavaSessionFlash
自己创建一个接受参数的自定义注释:
@With(Authorization.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Auth {
String value() default "";
}
按如下方式注释您的控制器方法:
@Auth("create-account")
public static Result createAccount(){
return ok("Account created");
}
要从注释中检索值,使 Authorization
class 扩展 Action<Auth>
:
public class Authorization extends Action<Auth> {
String value = configuration.value();
// ...
}