【问题标题】:Apache Shiro + Guice - can't get the annotations to workApache Shiro + Guice - 无法让注释工作
【发布时间】:2012-04-27 07:39:45
【问题描述】:

所以我能够让 Apache Shiro 在 Vaadin 上与 Guice 一起工作(感谢 ShiroWebModule)。 Shiro 注释(@RequiresAuthentication@RequiresPermission)仅适用于主 Vaadin Application 类和自定义类内部。它们在 CustomComponent/Window 类中不起作用。

我尝试通过 injector.getInstance 将 Window 类与提供程序一起注入 Application 类,但它仍然无法正常工作...

我是 Guice 和 Shiro 的新手,所以也许我遗漏了什么?

为什么它适用于其他自定义类?这按预期工作(抛出异常)

public class TestClassImpl implements TestClass {

    @Override

    public void doSomeWork() {
        //this will throw an exception as expected
        test();
    }

    @RequiresAuthentication

    public void test() {

    }
}

这并没有按预期工作(方法被执行,Apache Shiro 注解被忽略):

  public class LoginView extends CustomComponent {

    public LoginWindow() {
        setCompositionRoot(mainLayout);
        //this will execture but it should not
        test();
    }

    @RequiresAuthentication

    public void test() {

    }
}

【问题讨论】:

    标签: apache guice vaadin shiro


    【解决方案1】:

    在运行时使用这样的注解通常会涉及到 AOP。

    使用 Spring AOP,您无法拦截对 self 的调用:这是因为 Spring AOP 生成代理类,并且拦截发生在这些代理上 -> 它无法拦截对 self 的调用。

    我怀疑 Guice AOP 的工作方式相同。

    注意:TestClass/Impl 和 LoginView 的区别之一是 TestClass 实现了一个接口; Guice 可能会以不同的方式处理接口代理和“普通类”代理 - 我会尝试更改 TestClass 以扩展抽象类,看看那里会发生什么。

    【讨论】:

    • 感谢您的回复。但是即使直接注入 TestClassImpl 也可以工作,无需接口。所以这不会抛出异常(但它应该)injector.getInstance(LoginView.class); 并且它按预期工作(抛出安全异常)injector.getInstance(TestClassImpl.class);。他们都在构造函数中调用了一个方法,该方法用 @RequiresAuthentification 注释。
    • 好的,这里还有一个更根本的问题。 Guice 不会在 CustomComponents 中注入任何类。我不知道为什么。这绝对应该有效,但我做错了什么。
    • 更新。我有一个解决这个问题的方法。只有构造函数注入在组件/Windows 中有效(不知道为什么)。所以我注入了我需要的服务类,这些服务类里面有 Shiro 注释,可以工作。我只需要以编程方式检查 Vaadin 组件/窗口类中的 Shiro 角色/权限。
    猜你喜欢
    • 2023-03-15
    • 2012-01-24
    • 2013-12-11
    • 2017-06-10
    • 2012-02-10
    • 2011-06-18
    • 1970-01-01
    • 2012-07-24
    • 1970-01-01
    相关资源
    最近更新 更多