zoukankan      html  css  js  c++  java
  • springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、

    流程:

    1.用户调登录接口,传用户名和密码2.用户名和密码在ad验证,验证通过后,返回当前用户的相关信息。(注:ldap为java自带的api不需要maven引入其他的)3.根据返回的用户信息,实现自己系统的业务逻辑

    @RequestMapping("/getMsg")
       @ResponseBody
        public String getAllPersonNamesWithTraditionalWay(@RequestParam String username,@RequestParam String password) {
            Hashtable env = new Hashtable();
    
            env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            //ldap://192.168.153.129:389/dc=contoso,dc=com
            env.put(Context.PROVIDER_URL, "ldap://192.168.153.129:389/dc=contoso,dc=com");
            env.put(Context.SECURITY_PRINCIPAL, username);
            env.put(Context.SECURITY_CREDENTIALS, password);
            DirContext ctx;
            String name="";
           NamingEnumeration results = null;
            try {
                ctx = new InitialDirContext(env);
                SearchControls controls = new SearchControls();
                controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
                results = ctx.search("", "(&(objectclass=person)(userprincipalname=" + username+ "))",controls);
                SearchResult searchResult = (SearchResult) results.next();
                Attributes attributes = searchResult.getAttributes();
                name = attributes.get("userprincipalname").get().toString().split("@")[0];
            }
            catch (AuthenticationException e)
            {
                String erroMsg=  e.toString();
                e.printStackTrace();
                return erroMsg;
            }
            catch (NameNotFoundException e) {
                String erroMsg=  e.toString();
                e.printStackTrace();
                return erroMsg;
            } catch (NamingException e) {
                e.printStackTrace();
                String erroMsg=  e.toString();
                return erroMsg;
            } finally {
                if (results != null) {
                    try {
                        results.close();
                    } catch (Exception e) {
                    }
                }
            }
            return name;
        }

    返回了登录用户的name字段。还有其他字段如下图)

     

    微软ad域样子:(我是通过虚拟机安装了windos sever 2008 r2 然后在其系统上,安装了AD域)

      
  • 相关阅读:
    IOS
    .net程序集强命名(签名)
    spring.net Corn表达式[转]
    SQLite日期类型【转】
    Use SQLite Instead of Local Storage In Ionic Framework【转】
    VS常用快捷键
    安装ASP.net mvc3 Installation failed with error code: (0x80070643), "安装时发生严重错误 "
    支付宝支付(沙箱环境相关配置)
    如何在Windows远程服务器中引入本地磁盘
    设计模式-策略模式
  • 原文地址:https://www.cnblogs.com/anlegou/p/10511683.html
Copyright © 2011-2022 走看看