2011-07-28 166 views
2

我一直在爲需要AOP知識的配置而苦苦掙扎。Spring AOP和apache shiro的配置。註釋未被掃描

我必須承認,AOP是我試圖得到一段時間沒有成功的那部分。 似乎我的shiro註釋不被掃描,因此被忽略。

我試過使用shiro 1.1.0+ maven3 + spring 3.0.5.RELEASE,hibernate 3.6.1.Final與ZK 5.0.6。 我有我的hibernaterealm工作,與數據庫交談,我得到了認證工作,我成功(我相信)獲得角色和權限加載。

所以測試授權方我在我的代碼,這個地方:

Subject currentUser = SecurityUtils.getSubject(); 
    if (!currentUser.isPermitted("businessaccount:list")) { 
    throw new AuthorizationException("User not authorized"); 
    } 

,它工作正常。
所以我知道我的權限被loaded.i'll方便我使用註釋我已經把它放在實現類,因爲我沒有計劃在第一個地方使用接口與我的控制器類擴展ZK GenericForwardController 。

我已經看到了這個bug,我決定嘗試一個接口與方法@RequiresPersmissions。

顯然它仍然沒有工作在它給訪問未經授權的subject.there是我log.Maybe沒有錯誤我做錯了這裏的代碼片段:

@Component("layouteventhandler") 
public class LayoutEventHandlerImpl extends GenericForwardComposer implements  LayoutEventHandler { 

Logger logger = Logger.getLogger(LayoutEventHandlerImpl.class); 
Menuitem logout; 

//... 


@Override 
public void onClick$pAccounts() { 
    try { 
     execution.sendRedirect("/accounts/personal/list"); 
    } catch (Exception ex) { 
     logger.info("Error redirecting to personal accounts", ex); 
    } 
} 


@Override 
public void onClick$bAccounts() { 
    try { 
     execution.sendRedirect("/accounts/business/list"); 
    } catch (Exception ex) { 
     logger.info("Error redirecting to business accounts", ex); 
    } 
} 
//..... 
} 

它的接口它:

public interface LayoutEventHandler { 

@RequiresPermissions(value="personalaccount:list") 
public void onClick$pAccounts(); 

@RequiresPermissions(value="businessaccount:list") 
public void onClick$bAccounts(); 
//..... 

} 

這裏是我的四郎的ApplicationContext

<bean id="hibernateRealm" class="com.personal.project.admin.webapp.security.DatabaseRealm" /> 
<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager"> 
    <property name="realm" ref="hibernateRealm" /> 
</bean> 

<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" /> 

<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" 
     depends-on="lifecycleBeanPostProcessor"> 
<!--   <property name="proxyTargetClass" value="true" />--> 
</bean> 
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor"> 
    <property name="securityManager" ref="securityManager"/> 
</bean> 

<!-- Secure Spring remoting: Ensure any Spring Remoting method invocations can be associated 
    with a Subject for security checks. --> 
<bean id="secureRemoteInvocationExecutor" class="org.apache.shiro.spring.remoting.SecureRemoteInvocationExecutor"> 
    <property name="securityManager" ref="securityManager"/> 
</bean> 
<!-- ... --> 

是否有我應該做的事情?感謝您的閱讀和幫助

+0

你能提供更多的信息嗎?你的Shiro註釋如何被忽略?他們在你的課程中是否爲空,你將它們連接到? – Caps

+0

你好,我已經添加了更多的細節,如您requested.thanks –

+0

這仍然感興趣?使用中是否還有其他AOP方面(例如交易處理)? – rudolfson

回答

2

我不知道Shiro,但我猜你已經在你的bean類上實現了接口的註釋,然後代理他們的安全性,交易和/或別的東西。當發生這種情況時,返回的對象是一個JDK動態代理,它不是您的bean的具體類的實例,而只是它實現的接口的實例。因此,依賴於具體類中的註釋的任何註釋掃描都不會找到它們。

0

要擴大瑞恩·斯圖爾特的答案,你需要

@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) 

添加到實現類(而不是接口),移動四郎註解吧。

0

我在運行兩個spring上下文時遇到了類似的問題。有一個父級根上下文定義了包含我想要代理的控制器的Spring MVC REST api的Database,Service,Security和非SpringMVC Web bean以及一個子Web對象。每個上下文的配置是類路徑掃描單獨的包。

在這種情況下,請確保在子Web上下文中定義了需要的DefaultAdvisorAutoProxyCreator和AuthorizationAttributeSourceAdvisor bean。因爲在父上下文中定義它們是行不通的(DefaultAdvisorAutoProxyCreate上的文檔在事後很清楚!)。

發佈此信息以防其他人遇到相同問題。