Java 春季安全哪些部门?SecurityContextHolder';s认证机构与主体的对比';政府当局

Java 春季安全哪些部门?SecurityContextHolder';s认证机构与主体的对比';政府当局,java,spring-security,authorization,principal,Java,Spring Security,Authorization,Principal,我正在使用Spring Security为我的webapp提供身份验证和授权 我使用标准方法UserDetails loadUserByUsername(字符串username)抛出UsernameNotFoundException使用DaoAuthenticationProvider登录 成功登录后,我可以通过两种不同的方式访问用户权限: 1#SecurityContextHolder.getContext().getAuthentication().GetAuthority() 2.((用户

我正在使用Spring Security为我的webapp提供身份验证和授权

我使用标准方法
UserDetails loadUserByUsername(字符串username)抛出UsernameNotFoundException
使用
DaoAuthenticationProvider登录

成功登录后,我可以通过两种不同的方式访问用户权限:

1#
SecurityContextHolder.getContext().getAuthentication().GetAuthority()

2.
((用户详细信息)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).GetAuthority()

它们应该是相同的,因为身份验证实例权限列表是从UserDetails的user.getAuthories()构造的,但实际上它们是两个不同的集合实例。主体的一个是不可修改集,身份验证实例中的一个是不可修改的RandomAccessList。 2个包含相同权限列表的不同集合实例

当我试图找到一个解决方案来动态地向当前经过身份验证的主体添加权限时,我突然想到了这一点。这不是一件好事,安全层不应该允许我这么做,但这仍然是我需要的

我发现一个可以接受的解决方案是以编程方式使用当前通过身份验证的用户登录
newusernamePasswordAuthenticationToken(authentication.getPrincipal()、authentication.getCredentials()、dbAuthsSet)

authentication
是当前的身份验证实例,而
dbAuthSet
是当前的权限列表+我需要添加的权限列表

这个很好用。现在唯一奇怪的是

1#
SecurityContextHolder.getContext().getAuthentication().GetAuthority()

2.
((用户详细信息)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).GetAuthority()

不再返回相同的权限列表

如果我在应用程序中对照身份验证列表检查权限,我很好,这是完全合理的。但如果我对照校长的名单,我会面临不一致的情况

你觉得怎么样?这是Spring的安全性3.2中应该解决的问题吗

您认为使用
new UsernamePasswordAuthenticationToken()
以编程方式登录是否合理,以实现我的目标,即为当前经过身份验证的用户授予会话的额外权限

使用Spring的特性是一个好的例子吗?
有人能分享一下这方面的经验吗?

我认为最明显的问题是——你想实现什么?您的用例是什么?业务级别的用例是管理员能够模拟用户。我的解决方案是在它选择要模拟的用户之后动态地给它必要的权限。然后系统将其视为用户,并可以执行用户的操作。检查一条非常好的建议,它在Spring 3.1文档中几乎没有提到,我很高兴您将其指向我。非常感谢你!