简体   繁体   English

如何使用局部变量注释进行Wicket授权?

[英]How do I use Local Variable Annotations for Wicket Authorization?

I'm rolling my own IAuthorizationStrategy for Wicket 1.5.x I've setup type annotation for pages to use with isInstantiationAuthorized() . 我正在为Wicket 1.5.x滚动我自己的IAuthorizationStrategy ,我为与isInstantiationAuthorized()一起使用的页面设置了类型注释。 It works well and I'd like to use annotations for isActionAuthorized() as well. 它运作良好,我也想对isActionAuthorized()使用注释。 Ideally I'd like to be able annotate local variables and then check the annotations in my AuthStrategy. 理想情况下,我希望能够注释局部变量,然后在我的AuthStrategy中检查注释。 From what I've read Local variable Annotation doesn't work that way. 从我读过的内容来看,局部变量注释不能那样工作。

Is there any kind of known work around, maybe some sort of Compile time annotation processing to turn an annotated local variable into an "anonymous" subclass with the annotation as a type annotation? 是否有任何已知的解决方法,也许是某种编译时注释处理,以将带注释的局部变量转换为以该注释作为类型注释的“匿名”子类?

For the record, the annotation I'm trying to use looks like this: 作为记录,我尝试使用的注释如下所示:

@Retention(RetentionPolicy.Runtime)
@Target(ElementType.Type, ElementType.LOCAL_VARIABLE)
public @interface AdminOnly
{
  int isVisible() default 0;
  int isEnabled() default 1;
}

UPDATE UPDATE

So based on @Xavi López'es answer what I was hoping to do isn't exactly possible. 因此,基于@XaviLópez的答案,我希望做的事情并非完全可行。 Annotated LocalVariables should be available at compile time though. 不过,带注释的LocalVariables 应该在编译时可用。 Is there some way maybe I could use them as a shortcut for boiler-plating the meta-data code examples that are available in Wicket Examples or the excellent Apache Wicket Cookbook? 有什么办法可以将它们用作简化镀覆Wicket示例或出色的Apache Wicket Cookbook中可用的元数据代码示例的快捷方式吗?

I've struggled with a similar issue some time ago with Wicket 1.3.x, and didn't find any way to achieve this with annotations. 一段时间以前,我在Wicket 1.3.x上也遇到过类似的问题,并且找不到任何方法可以通过注释来实现。 Annotations on local variables can't be retained at run-time, as explained in the JLS ( 9.6.3.2. @Retention ): 如JLS( 9.6.3.2。@Retention )中所述,无法在运行时保留局部变量的注释:

An annotation on a local variable declaration is never retained in the binary representation. 本地变量声明的注释永远不会保留在二进制表示中。

In this related question: How can I create an annotation processor that processes a Local Variable? 在这个相关的问题中: 如何创建处理局部变量的注释处理器? they talked about LAPT-javac , a patched javac version to allow this. 他们谈论了LAPT-javac ,这是一个修补的javac版本,允许这样做。 On their site there's a link to the Type Annotations Specification (JSR 308) , which will hopefully address this subject ( JDK 8 ?). 在他们的网站上,有一个指向类型注释规范(JSR 308)的链接 ,希望可以解决这个问题( JDK 8 ?)。

I ended up defining a plain old interface with a related functionality code: 我最终用一个相关的功能代码定义了一个简单的旧界面:

public interface RestrictedComponent {
    Integer getFunction();
}

The main problem with this approach is that it's not possible to make instant anonymous subclasses of a specific class implement other interfaces (such as Component c = new TextField() implements AdminOnly { } ) , but you can always define Component extensions that just implement RestrictedComponent in a class: 这种方法的主要问题在于,不可能使特定类的即时匿名子类实现其他接口(例如Component c = new TextField() implements AdminOnly { } ),但是您始终可以定义仅实现RestrictedComponent Component扩展。在课堂上:

public abstract class RestrictedTextField extends TextField implements RestrictedComponent { } 

Finally, I ended up implementing a RestrictedContainer that just subclassed WebMarkupContainer and put every secured component inside one, modelling it with a <wicket:container> in the markup. 最后,我最终实现了一个RestrictedContainer ,该子类只是WebMarkupContainer子类,并将每个受保护的组件放入一个组件中,并在标记中使用<wicket:container>对其进行建模。

public class RestrictedContainer extends WebMarkupContainer implements RestrictedComponent {
    private final Integer function;
    public RestrictedContainer(String id, IModel model, final Integer function) {
        super(id, model);
        this.function = function;
    }
    public RestrictedContainer(String id, final Integer funcionalitat) {
        super(id);
        this.function = function;
    }
    public Integer getFunction() {
        return function;
    }
}

And then in the Authorization Strategy checked for component instanceof RestrictedComponent and returned true or false depending on user permissions on the associated function. 然后在“授权策略”中检查component instanceof RestrictedComponent并根据相关功能的用户权限返回truefalse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM