Java 在AuthenticationSuccessHandler中添加权限
我正在使用SpringMVC网站,并在LDAP上使用Active Directory添加身份验证。该公司不想使用广告授权来映射该网站的权限,我们有一个列出每个用户权限的数据库,因此我尝试连接到该数据库,获取权限,并将其添加到用户的身份验证令牌中 当我第一次开始工作时,我正在用一个GrantedAuthoritiesMapper映射广告用户组的权限,我已经开始工作了。看起来是这样的:Java 在AuthenticationSuccessHandler中添加权限,java,spring,spring-security,Java,Spring,Spring Security,我正在使用SpringMVC网站,并在LDAP上使用Active Directory添加身份验证。该公司不想使用广告授权来映射该网站的权限,我们有一个列出每个用户权限的数据库,因此我尝试连接到该数据库,获取权限,并将其添加到用户的身份验证令牌中 当我第一次开始工作时,我正在用一个GrantedAuthoritiesMapper映射广告用户组的权限,我已经开始工作了。看起来是这样的: public class ActiveDirectoryGrantedAuthoritiesMapper impl
public class ActiveDirectoryGrantedAuthoritiesMapper implements GrantedAuthoritiesMapper {
private static final String ROLE_ADMIN = "adminUserGroup";
public ActiveDirectoryGrantedAuthoritiesMapper()
{ }
public Collection<? extends GrantedAuthority> mapAuthorities(
final Collection<? extends GrantedAuthority> authorities)
{
Set<CustomAuthority> roles = EnumSet.noneOf(CustomAuthority.class);
for (GrantedAuthority authority : authorities)
{
if (ROLE_ADMIN.equals(authority.getAuthority()))
{
roles.add(CustomAuthority.ROLE_ADMIN);
}
//Default role for all users.
roles.add(CustomAuthority.ROLE_EMPLOYEE);
}
return roles;
}
}
我已经尝试了所有我能想到的东西。。我已将列表更改为CustomAuthority对象列表。我尝试使用addAllroles,而不是添加单个角色。。每次我得到相同错误的一些变化:
addcapture1的方法是什么?类型集合中的extends GrantedAuthority不适用于参数GrantedAuthority
以及海关总署代码:
public class CustomAuthority implements GrantedAuthority
{
private String name;
public CustomAuthority(String name)
{
this.name = name;
}
public String getAuthority() {
return name;
}
}
任何帮助都将不胜感激
从相关问题来看,authentication.getName在这里可能不起作用,但我想找出为什么在解决该问题之前,我无法向用户权限中添加要添加的权限。您无法从SecurityContext获取当前用户名的原因是,您尚未填充该用户名我们仍在努力弄清楚其中应该包含哪些角色 一个选项是使用LdapAuthenticationProvider和LdapAuthoritiesPopulator。以下是常见问题解答中的示例
public class MyAuthoritiesPopulator implements LdapAuthoritiesPopulator {
@Autowired
JdbcTemplate template;
public List<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
List<GrantedAuthority> = template.query("select role from roles where username = ?",
new String[] {username},
new RowMapper<GrantedAuthority>() {
/**
* We're assuming here that you're using the standard convention of using the role
* prefix "ROLE_" to mark attributes which are supported by Spring Security's RoleVoter.
*/
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
return new GrantedAuthorityImpl("ROLE_" + rs.getString(1));
}
});
}
}
这应该对您有效,因为它会为您传入用户名。使用该用户名进行查询以获取用户的角色
如常见问题解答中所述,您需要使用自定义LdapAuthoritiesPopulator将其连接到LdapAuthenticationProvider
另一个选项是注入自定义UserDetailsContextMapper。默认值为LdapUserDetailsMapper,它应该让您了解如何实现逻辑。这不是对您最初问题的回答。这是一个关于如何以另一种方式实现的命题 对我来说,最不寻常的是,您使用AuthenticationSuccessHandler处理本应由AuthenticationManager和AuthenticationProviders完成的事情。假设有两个AuthenticationProvider,一个用于LDAP,另一个用于DB。问题在于,如果通过默认的AuthenticationManager将它们合并,则只会使用第一个提供程序。您可以使用稍微不同的逻辑准备自定义AuthenticationManager: 它知道两个供应商 它按正确的顺序调用它们 它将来自两个提供者的两个身份验证结果组合成一个全局结果,从LDAP获取用户凭据,从DB获取权限。 缺点:想要添加第三个身份验证提供程序的新开发人员可能会对自定义AuthenticationManager感到惊讶 优点:我认为th代码将适用于正确的位置
希望这能有所帮助。我在SuccessHandler中找到了一种黑客作业方法,但这似乎是正确的方法。谢谢你指给我看。事实上。。似乎无法将LdapAuthoritiesPopulator与ActiveDirectoryLdapAuthenticationProvider一起使用?如果您想使用LdapAuthoritiesPopulator,我的回答是使用LdapAuthenticationProvider是正确的。或者,如果要使用ActiveDirectoryLdapAuthenticationProvider,请使用UserDetailsContextMapper。
public class MyAuthoritiesPopulator implements LdapAuthoritiesPopulator {
@Autowired
JdbcTemplate template;
public List<GrantedAuthority> getGrantedAuthorities(DirContextOperations userData, String username) {
List<GrantedAuthority> = template.query("select role from roles where username = ?",
new String[] {username},
new RowMapper<GrantedAuthority>() {
/**
* We're assuming here that you're using the standard convention of using the role
* prefix "ROLE_" to mark attributes which are supported by Spring Security's RoleVoter.
*/
public GrantedAuthority mapRow(ResultSet rs, int rowNum) throws SQLException {
return new GrantedAuthorityImpl("ROLE_" + rs.getString(1));
}
});
}
}