2009-11-09 53 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);