java - 玩! java 。将请求参数传递给@With 注解

标签 java playframework annotations playframework-2.2

我有这样的路线:

  GET     /job$id<[0-9]+>/        controllers.Jobs.index(id)
  POST    /job$id<[0-9]+>/done    controllers.Jobs.done(id)
  POST    /job$id<[0-9]+>/update  controllers.Jobs.update(id)
  DELETE  /job$id<[0-9]+>/remove  controllers.Jobs.remove(id)

我想保护它。每个作业都有一个所有者。所以我做了

public class Secured extends Security.Authenticator {
...
}

然后我尝试通过“@With()”注释来保护我所有的操作,但我需要将“Id”参数传递给我的安全方法,所以我写得像:

@With(IsOwner.class)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Owner {
    long value() default 0;
}

public static class IsOwner extends Action<Owner>{
    @Override
    public Promise<SimpleResult> call(Context ctx) throws Throwable {

        if(!isOwner(configuration.value())){
            return Promise.pure(redirect(..));
        }
        return delegate.call(ctx);
    }
}

但我无法将我的操作参数传递给注释。

@Security.Authenticated(Secured.class)

@Owner(id) //**this part I want to work**
public class Jobs extends Controller {

  //@Owner(id) - or at list there
  public static Result index(Long Id){
    //I have too many Actions and don't want to do every time this
    /*if(!Secured.isOwnerMethod(Id)){
      return forbidden();
    }*/         
    return ok();        
  }

我看到的其他方法是在 IsOwner.call() 方法中从 Context 获取参数...

对于这种情况,请给我一个建议或好的做法。

谢谢。

最佳答案

我遇到了这个问题并提出了一个 Play 扩展,它将所有 url 参数公开为公共(public)请求参数...您可以在此处查看 => https://github.com/asouza/reverse-route-plugin

基本上,您必须从我的 Global 类进行扩展。

干杯,

阿尔贝托

关于java - 玩! java 。将请求参数传递给@With 注解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23698339/

相关文章:

运行时出现 java.lang.ClassCastException

java - hadoop中的分区文件是如何创建的

java - Play Framework/Java : A good way to send x-www-form-urlencoded data back

javascript - @JavaScriptInterface 与 ICS 和 JB 项目一起工作

java - 声明类型不能为 null

java - OnLongCLickListener 无法按我的需要工作

java - 接口(interface) 拥有内部类会影响接口(interface)的抽象 - OOP

playframework - 玩! "models"中的framework子文件夹之一?

junit - 作为 IntelliJ IDEA JUnit 运行配置运行 playframework 测试

python - 如何标记 matplotlib 中两条线之间的百分比变化?