2013-03-13 50 views
0

我在我的項目中使用了spring security annotations。有些情況下,我想調用註釋對象的無安全版本。默認情況下,Spring爲註釋對象創建一個啓用安全性的代理並將其用於代碼中的自動裝配,有沒有什麼方法可以使用spring來實現?
 這樣做的一個顯而易見的方法是手動創建對應於每個類的代理類,我希望此功能對這些方法進行了註釋,並且這些方法的實現只是將其委託給實際對象。如何在使用spring安全註解時調用不安全的代理?

回答

0

正如在JDK代理的情況下的一個選項,你可以在運行時得到實際的bean:

MyBean proxy;  
if(AopUtils.isJdkDynamicProxy(proxy)) { 
    MyBean actualInstance = (MyBean) ((Advised)proxy).getTargetSource().getTarget() 
} 

actualInstance.doSomethingSecured(); // no advice related to this method will be called 
// so your security annotation will be ignored (transactions, cache, and everething that requires AOP too...) 

但是從視圖具有手動代理方式的建築點看起來更小的誤差phrone(除非你絕對相信你不需要安全性和所有其他可能的方面)。

您可以使用泛型提高可讀性:

MyBean actualInstance = extractProxyTarget(proxy, proxy.getClass()); 
actualInstance.doSomethingSecured();