【问题标题】:Add Principal/authority dynamically for anonymous user为匿名用户动态添加委托人/权限
【发布时间】:2010-12-14 12:10:14
【问题描述】:

我正在使用 spring security 2.x (+spring + struts2) 并希望在用户提交表单后启用动态添加权限。

我有一个受 ROLE_USER 保护的受保护目录 (/protected/dir/)

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

哪些用户登录后可以访问。

我想通过向主体添加一个临时 ROLE_TEMP(它甚至可能不存在,因为用户尚未登录,所以我可能必须添加securityContext 也是如此)

我尝试访问 SecurityContext 并在我的控制器/动作类中添加新的 Principal。但我无法获得 SecurityContext。 (我认为 SecurityContext 只在它自己的线程上运行,你不能传递它,这就是我得到 NPE 的原因)

那么最好的方法是什么?

请指教 谢谢

【问题讨论】:

  • 如果用户未登录,则没有安全上下文。
  • 哦..好的。那么如何自动登录用户并授予他对文件夹的临时访问权限?

标签: java spring struts2 spring-security


【解决方案1】:

支持匿名用户的一种方法是添加此过滤器:

/**
* 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-25
    • 2010-11-01
    • 2010-11-01
    • 2011-05-25
    • 2019-04-17
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    相关资源
    最近更新 更多