首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java安全+ Active属性

Java安全+ Active属性
EN

Stack Overflow用户
提问于 2020-10-08 03:01:08
回答 1查看 44关注 0票数 0

我正在使用Java + SpringBoot安全在我的web应用程序自动化。贝娄是工作配置,没有索赔)

我的问题:

  1. 可以用这种方式连接到AD,以便从AD中获取用户的信息(例如。sAMAccountName,

)?

  1. 有可能由AD组?

设置访问页面。

如果我正确理解AuthenticationManagerBuilder,只需连接到AD。

代码语言:javascript
复制
@Configuration
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .antMatchers("/home", "/logout/**","/logout-success","/login/**").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .and()
                .logout()
                .permitAll();
    }
    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder authBuilder) throws Exception {
        authBuilder
                .ldapAuthentication()
                .userSearchFilter("(sAMAccountName={0})")
                .userSearchBase("OU=Active,OU=Users,OU=nsk,DC=regions,DC=office,DC=ru")
                .groupSearchBase("OU=Groups,OU=nsk,DC=regions,DC=office,DC=ru")
                .groupSearchFilter("member={0}")
                .contextSource()
                .url("ldap://regions.office.ru:389")
                .managerDn("CN=ldap_user_ro,OU=Service,OU=Users,OU=nsk,DC=regions,DC=office,DC=ru")
                .managerPassword("passw");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-12 05:30:32

通过属性(displayName)搜索域的所有用户。

代码语言:javascript
复制
public class LdapSearch {
public List<String> getAllPersonNames() {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://regions.office.ru:389");
    env.put(Context.SECURITY_PRINCIPAL, "CN=ldap_user_ro,OU=Service,OU=Users,OU=nsk,DC=regions,DC=office,DC=ru");
    env.put(Context.SECURITY_CREDENTIALS, "password");

    DirContext ctx;
    try {
        ctx = new InitialDirContext(env);
    } catch (NamingException | javax.naming.NamingException e) {
        throw new RuntimeException(e);
    }

    List<String> list = new LinkedList<String>();
    NamingEnumeration results = null;
    try {
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        results = ctx.search("OU=Active,OU=Users,OU=nsk,DC=regions,DC=office,DC=ru", "(objectclass=user)", controls);

        while (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            Attribute attr = attributes.get("displayName");
            String cn = attr.get().toString();
            list.add(cn);
        }
    } catch (NameNotFoundException e) {
    } catch (NamingException | javax.naming.NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
            }
        }
    }
    return list;
}
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64255176

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档