2009-11-09 59 views
0

我正在使用spring security 2.x(+ spring + struts2),並希望在用戶提交表單後動態爲用戶啓用添加權限。爲匿名用戶動態添加主體/權限

我有一個由ROLE_USER

<sec:intercept-url pattern="/protected/dir/**" access="ROLE_USER, ROLE_ADMIN" /> 

保護哪些用戶可以訪問他們登錄後,受保護的目錄(/保護/ DIR /)。

我想通過向委託人添加一個臨時ROLE_TEMP(這可能不存在,因爲用戶還沒有登錄,因此我可能不得不添加到securityContext)

我試圖訪問SecurityContext並在我的控制器/操作類中添加新的委託人。但我無法獲得SecurityContext。 (我認爲SecurityContext只能在自己的線程上運行,並且你不能通過它,這就是爲什麼我得到NPE)

那麼這樣做的最佳方式是什麼?

請告知 感謝

支持匿名用戶
+0

如果用戶未登錄,則不存在安全上下文。 – rodrigoap 2009-11-09 14:12:29

+0

哦..好的。那麼我如何自動登錄用戶並授予他對文件夾的臨時訪問權? – 2009-11-09 18:50:23

回答

0

一種方法是添加此過濾器:

/** 
* Detects if there is no Authentication object in the SecurityContextHolder, 
* and populates it with one if needed. 
*/ 
org.springframework.security.providers.anonymous.AnonymousProcessingFilter 

濾波器具有這樣的屬性,將迫使過濾後的要求是刪除匿名會議完成:

public void setRemoveAfterRequest(boolean removeAfterRequest);