2

我正在编写一项服务,其中我使用 Shiro 来确保安全。我也将 Guice 与它结合在一起。我在以下位置创建了 GUICE 注入器GuiceServletContextListener

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

//Shiro annotations
new MyAOPModule(),

我还将 Guice Container 和 GuiceShiroFilter 绑定在JerseyServletModule

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");

在 JerseyServlet 模块中

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

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

4

1 回答 1

0

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

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);

}
}

问候

于 2014-10-30T15:31:24.810 回答