2014-09-23 97 views
2

我正在寫一個服務,我使用Shiro作爲安全性。我也將Guice與它結合在一起。我在GuiceServletContextListener創建吉斯注射器:Shiro AOP with GUICE not working

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

//Shiro annotations 
new MyAOPModule(), 

我也綁定吉斯容器和GuiceShiroFilter在JerseyServletModule

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

但是從四郎的註解似乎只是不工作!

我配置MyShiroWebModule鏈:

@RolesAllowed("SomeFancyRole")(請參閱編輯)

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

所以,如果我使用了 「角色」 過濾器然後將它的AOP方式掃描角色

但我想利用GUICE Shiro AOP功能。我試過基本的ShiroAOPModule而不是我自己的 - >我的調試是用來查看配置是否被調用。

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

所以AOP

從四郎仍然沒有過濾。

回答

0

您可以將其更改爲標準ShiroAopModule類。它必須在ShiroWebModule子類後初始化。 這是一個片段,使servlet模塊工作與新澤西1.18.1,吉斯3和Apache四郎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); 

} 
} 

問候

+0

你可以在這裏查看示例項目 [https://開頭github上。 COM/pabiagioli /四郎-吉斯-球衣的自舉(https://github.com/pabiagioli/shiro-guice-jersey-bootstrap) – pampanet 2014-10-30 15:55:15

相關問題