【问题标题】:Granting an ADMIN role to a user in a ldif file在 ldif 文件中向用户授予 ADMIN 角色
【发布时间】:2017-06-14 21:52:55
【问题描述】:

对于测试环境,我有一个.ldif 文件来授予ben 用户ADMIN 角色,因为我的Spring 安全配置需要此角色:.hasRole("ADMIN").anyRequest()

这是.ldif文件内容:

dn: ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: groups

dn: ou=people,dc=springframework,dc=org
objectclass: top
objectclass: organizationalUnit
ou: people

dn: uid=ben,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Ben LeHeros
sn: Ben
uid: ben
userPassword: {SHA}nFCebWjxfaLbHHG1Qk5UU4trbvQ=

dn: uid=toto,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Toto LeHeros
sn: Toto
uid: toto
userPassword: totopass

dn: cn=adMIN,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfNames
cn: ADMin
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

dn: cn=user,ou=groups,dc=baeldung,dc=com
objectclass: top
objectclass: groupOfNames
cn: user
member: uid=toto,ou=people,dc=springframework,dc=org

测试环境Spring Security配置如下:

@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth)
        throws Exception {
    auth
    .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource().ldif("classpath:test-server.ldif");
}

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable().httpBasic()
            .authenticationEntryPoint(restAuthenticationEntryPoint).and()
            .authorizeRequests().antMatchers("/**")
            .hasRole("ADMIN").anyRequest()
            .authenticated();
}

有几点观察:

1- 测试成功,用户通过身份验证并被认可为具有管理员角色。

2- 如果不是将角色认证配置为.hasRole("ADMIN").anyRequest(),而是将其配置为.hasRole("admin").anyRequest(),那么测试中给出的用户角色将不被接受,并且测试失败并在认证时出现 403(不是 401)。

3- .ldif 文件中的 case 似乎无关紧要,因为 admin 组可以写成 adMINADMin 并且测试仍然成功。

4- 将admin 组替换为用户的user 组,导致测试失败并返回403,即用户需要管理员角色才能登录。

dn: cn=user,ou=groups,dc=springframework,dc=org
objectclass: top
objectclass: groupOfNames
cn: user
uniqueMember: uid=ben,ou=people,dc=springframework,dc=org

为什么case在.ldif文件中不重要而在configure方法中重要?

【问题讨论】:

    标签: spring-security apacheds spring-security-ldap ldif


    【解决方案1】:

    每个目录服务器架构(即定义的属性和对象类)用于构建组条目的可分辨名称 (DN) 的属性定义为忽略大小写(DN "spec" https://www.rfc-editor.org/rfc/rfc4514)。

    我个人认为这是 hasRole(..) 以区分大小写的方式匹配的错误。但是我认为是这种情况,因为 DefaultLdapAuthoritiesPopulator 默认为 convertToUpperCasetrue

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 2019-04-13
      • 1970-01-01
      • 2019-05-21
      • 2020-11-30
      • 1970-01-01
      • 2014-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多