【发布时间】:2021-06-23 11:13:26
【问题描述】:
需要检索“memberOf”属性-使用了搜索方法但出现以下错误。请建议我如何检索代码。
java.lang.UnsupportedOperationException: Not implemented.
at org.springframework.ldap.core.DirContextAdapter.search(DirContextAdapter.java:1055) ~[spring-ldap-core-2.3.3.RELEASE.jar:2.3.3.RELEASE]
at com.example.authenticatingldap.WebSecurityConfig$1.mapUserFromContext(WebSecurityConfig.java:85) ~[classes/:na]
at org.springframework.security.ldap.authentication.AbstractLdapAuthenticationProvider.authenticate(AbstractLdapAuthenticationProvider.java:82) ~[spring-security-ldap-5.4.5.jar:5.4.5]
这是我写的代码,这里我需要获取详细信息的成员并为用户设置权限/角色。帮助将不胜感激 //代码
@Configuration
@EnableWebSecurity
public class SecurityConfiguration2 extends WebSecurityConfigurerAdapter {``
@Autowired
ServerDetailsRepository serverDetailsRepository;
@Autowired
RoleRepository roleRepository;
@Value("${login_admin_role_api_list}")
private String loginAdminRoleApiList;``
@Value("${login_all_role_api_list}")
private String loginAllrolesApiList;
@Value("${login_admin_readwrite_role_api_list}")``
private String loginAdminReadWriteRoleApiList;
/*
* @Override protected void configure(HttpSecurity http) throws Exception {
* System.out.println("inside HttpSecurity Method"); http .authorizeRequests()
* .anyRequest().fullyAuthenticated() .and() .formLogin(); }
*/
@Autowired
public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception {
authBuilder.ldapAuthentication().
userSearchBase("ou=people,dc=sdnlab,dc=com").
userSearchFilter("(uid={0})")
.userDnPatterns("uid={0},ou=people").userDetailsContextMapper(userDetailsContextMapper())
.contextSource().root("dc=sdnlab,dc=com")
.url("ldap://10.168.160.104:389/dc=sdnlab,dc=com");
}
@Bean
public UserDetailsContextMapper userDetailsContextMapper() {`
return new LdapUserDetailsMapper() {
@SuppressWarnings("unused")
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username,
Collection<? extends GrantedAuthority> authorities) {
Attributes attributes = ctx.getAttributes();//receiving attributes but no memberof values
Object[] groups2 = new Object[100];
groups2 = ctx.getObjectAttributes("memberOf");//Null values returning
SearchControls cons = new SearchControls();
cons.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<SearchResult> answer2;
try {
answer2 = ctx.search("dc=sdnlab,dc=com", String.format("(uid=%s)", username),
new String[] { "memberOf", "uid", "givenName", "mail", "sn" }, cons);
if (answer2.hasMore()) {
Attributes attrs = answer2.next().getAttributes();
System.out.println("Member of details:::" + attrs.get("memberOf").getAll());
}
} catch (NamingException e) {
e.printStackTrace();
}
Set<GrantedAuthority> authority = new HashSet<GrantedAuthority>();
authority.add(new SimpleGrantedAuthority("Read Only"));
User userDetails = new User(username, "", false, false, false, false, authority);
return userDetails;
}
};
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable().httpBasic().and().authorizeRequests()
.antMatchers(loginAdminRoleApiList.split(",")).hasRole("Admin")
.antMatchers(loginAllrolesApiList.split(",")).hasAnyRole("Read Only", "Approver", "Read Write", "Admin")
.antMatchers(loginAdminReadWriteRoleApiList.split(",")).hasAnyRole("Admin", "Read Write")
.anyRequest().authenticated();
}
【问题讨论】:
-
试过 String[] memberOfs = ctx.getStringAttributes("memberOf");但没有帮助
-
/* * * 在 LDAP 中搜索查询:ldapsearch -x -LLL -H ldap:/// -b "uid=praveen,ou=people,dc=sdnlab,dc=com" dn memberof 输出:dn: uid=praveen,ou=people,dc=sdnlab,dc=com memberOf: cn=tafadmin,ou=groups,dc=sdnlab,dc=com */
-
建议我哪里出错了?为什么我不能?
-
你确定有有任何
memberOf属性吗?memberOf是一个操作属性。除非您特别要求,否则您不会得到它,或者使用"+"要求所有操作属性。注意Object[] groups2 = new Object[100];:在下一行分配变量时不需要初始化变量。
标签: java spring-boot spring-security openldap