【问题标题】:Shiro AOP with GUICE not working带有 GUICE 的 Shiro AOP 不起作用
【发布时间】:2014-11-17 09:27:33
【问题描述】:

我正在编写一个使用 Shiro 来确保安全的服务。我也将 Guice 与它结合在一起。我在 GuiceServletContextListener 中创建了 GUICE 注入器:

//Custom Shiro Web module with defined REALM
new MyShiroWebModule(this.servletContext, "/v1/*"),

//Shiro annotations
new MyAOPModule(),

我还在JerseyServletModule中绑定了Guice Container和GuiceShiroFilter:

serve("/v1/*").with(GuiceContainer.class, params);                  
//Adds Shiro filtering  
MyShiroWebModule.bindGuiceFilter(binder());

但是 Shiro 的注释似乎不起作用!

我在MyShiroWebModule中配置链:

addFilterChain("/v1/res/test", ANON);
addFilterChain("/v1/**", ROLES, AUTHC_BASIC);

所以如果我使用“ROLES”过滤器,那么它会以 AOP 方式扫描角色:

@RolesAllowed("SomeFancyRole")(见编辑)

但我想利用 GUICE Shiro AOP 功能。我已经尝试了基本 ShiroAOPModule 而不是我自己的 -> my is 用于调试以查看是否调用了配置。

@User, @Authenticated etc.

如果文档指出只有“添加” ShiroAOPModule 才能开箱即用,我该如何合并此功能? 提前谢谢你

编辑:

原来@RolesAllowed 正在工作,这要归功于添加:

params.put(PackagesResourceConfig.PROPERTY_RESOURCE_FILTER_FACTORIES, "com.sun.jersey.api.container.filter.RolesAllowedResourceFilterFactory");

在 JerseyServletModule 中

serve("/v1/*").with(GuiceContainer.class, params);

所以来自 Shiro 的 AOP 仍然没有被过滤。

【问题讨论】:

    标签: aop guice shiro guice-servlet


    【解决方案1】:

    您可以将其更改为标准 ShiroAopModule 类。它必须在您的 ShiroWebModule 子类之后初始化。 这是使 ServletModule 与 Jersey 1.18.1、Guice 3 和 Apache Shiro 1.2.3 一起工作的 sn-p

    public class BootstrapServletModule extends ServletModule{
    
    private static final String propertyPackages= GenericBootstrapConstants.JERSEY_PROPERTY_PACKAGES;
    
    @Override
    protected void configureServlets() {
        super.configureServlets();
    
        //get the bootstrapping Properties file
        install(new BootstrapPropertiesModule());
    
        //Initialize Persistence JPA Unit of Work if present
        //install(new MyUnitOfWorkModule());
        //Initialize Apache Shiro if present
        install(new BootstrapShiroModule(getServletContext()));
        //This allows Shiro AOP Annotations http://shiro.apache.org/java-authorization-guide.html
        install(new ShiroAnnotationsModule());
    
        Map<String, String> params = new HashMap<String, String>();
        params.put(PackagesResourceConfig.PROPERTY_PACKAGES, propertyPackages);
        //if you had a Persistence Service like JPA Unit of Work you would need to add this PersistFilter also.
        //filter("/*").through(PersistFilter.class);
        //if you had a ShiroWebModule installed above you would need to add this GuiceShiroFilter also.
        filter("/*").through(GuiceShiroFilter.class);
        serve("/rest/*").with(GuiceContainer.class, params);
    
    }
    }
    

    问候

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    • 2014-10-29
    • 2012-02-15
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多