Spring security 向ldif文件中的用户授予管理员角色
对于测试环境,我有一个Spring security 向ldif文件中的用户授予管理员角色,spring-security,apacheds,spring-security-ldap,ldif,Spring Security,Apacheds,Spring Security Ldap,Ldif,对于测试环境,我有一个.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 objectcla
.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安全配置如下所示:
@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
文件中似乎无关紧要,因为admin
组可以写成admin
和admin
,并且测试仍然成功
4-将用户的管理员
组替换为用户
组会导致测试失败,结果为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
为什么大小写在.ldif
文件中无关紧要,在configure
方法中无关紧要?每个目录服务器架构(即已定义的属性和对象类)用于构建组项的可分辨名称(DN)的属性被定义为大小写忽略(DN“spec”)
我个人认为这是一个错误,HasPin(..)以区分大小写的方式进行匹配。然而,我认为这是因为DefaultLdapAuthoritiesPopulator默认为convertToUpperCase为true