简体   繁体   English

使用Spring Security检索记录的用户组

[英]Retrieving logged user group(s) with Spring Security

I'm using Spring Security to improve access on my web app. 我正在使用Spring Security来改善对Web应用程序的访问。 All works fine but I need to retrieve the user group where my user is contained. 一切正常,但我需要检索包含我的用户的用户组。 I've implemented the JdbcDaoImpl bean in my security xml and I've setting the properties groupAuthoritiesByUsernameQuery and allowing the query by usergroup. 我已经在安全性xml中实现了JdbcDaoImpl bean,并且设置了属性groupAuthoritiesByUsernameQuery并允许按用户组进行查询。 So, the app logins! 因此,该应用程序登录! But I don't know how to get the group. 但是我不知道该如何组队。 I need something like: SecurityContextHolder.getContext().getGroups()... Thanks! 我需要类似的东西:SecurityContextHolder.getContext()。getGroups()...谢谢!

You can use the following which will return a list of GrantedAuthority 您可以使用以下代码来返回GrantedAuthority列表

List<GrantedAuthority> grantedAuthorities = SecurityContextHolder.getContext()
                                            .getAuthentication()
                                            .getAuthorities()

GrantedAuthority.getAuthority() will return your role. GrantedAuthority.getAuthority()将返回您的角色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM