如何使用xml配置文件,JAVA,Spring Security使用LDAP对用户进行身份验证

肉饼

使用JNDI时,我的程序“使用LDAP验证用户身份”(附录中列出)可以正常工作。

根据要求,我必须使用xml格式的spring-security和config文件(包含ldap信息)(不允许使用idlf文件)。

我正在寻找JAVA中的代码片段(我正在使用1.8和spring),它将使用此Ldap.xml文件提取Java的所有相关信息以验证用户身份。需要使用弹簧安全装置。

我可以在这方面得到任何帮助吗?

LDAP.xml看起来像:

    <?xml version='1.0'?>


      <!-- The  Security Module.  This module will authenticate against AD
           and determine authorization against the SECURITY_OWNER schema
      -->

      <application-policy name="something-targeting">
        <authentication>
           <login-module code="com.et.security.ETLoginModule" flag="required" >        
              <module-option name="java.naming.provider.url">ldap://pcocpwdom01.corp.something.com:389</module-option>
              <module-option name="bindDN">CN=SVCLdapQry,OU=ServiceAccounts_Admins,OU=Data Services,DC=corp,DC=something,DC=com</module-option>
              <module-option name="bindCredential">+byZB0ocHUQL0MDhd2mN3dSjskf2S7ff2hiCcCDThSE=</module-option>
              <module-option name="baseCtxDN">DC=corp,DC=something,DC=com</module-option>
              <module-option name="baseFilter">(samaccountname={0})</module-option>
              <module-option name="allowEmptyPasswords">false</module-option>

           </login-module>
        </authentication>
      </application-policy>

寻找类似的东西:

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

感谢您的帮助。如果需要更多信息,请告诉我。

我尝试了以下示例:

无法使他们中的任何一个工作。

附录A:

    package com.something.online.ice.ui.authentication;

    import java.util.Hashtable;
    import java.util.Properties;

    import javax.annotation.Resource;
    import javax.naming.Context;
    import javax.naming.NamingEnumeration;
    import javax.naming.NamingException;
    import javax.naming.directory.Attributes;
    import javax.naming.directory.DirContext;
    import javax.naming.directory.InitialDirContext;
    import javax.naming.directory.SearchControls;
    import javax.naming.directory.SearchResult;
    import javax.naming.ldap.InitialLdapContext;
    import javax.naming.ldap.LdapContext;


    /**
     * 

     * This is a solution that can be used to authenticate a user with something else than the DN, for example with a uid or sAMAccountName.

        The steps to do are:

        -Connect to the LDAP server
        -Authenticate with a service user of whom we know the DN and credentials
        -Search for the user you want to authenticate, search him with some attribute (for example sAMAccountName)
        -Get the DN of the user we found
        -Open another connection to the LDAP server with the found DN and the password
        -If the user is found and authentication works, you are fine

     *
     */

    public class LdapAuthManagerJNDI 
    {
        public static void main(String[] args)
        {
            LdapAuthManagerJNDI mgr = new LdapAuthManagerJNDI();
            System.out.println(mgr.authenticateUsr("svc_oapusr", "pswd"));

        }

        public boolean authenticateUsr(String usrName, String pswd)
        {


            Hashtable<String, String> serviceEnv = new Hashtable<String, String>();
            boolean authenticationresullt = false;


            serviceEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            serviceEnv.put(Context.PROVIDER_URL, "ldap://pcocpwdom01.corp.something.com:389");

            serviceEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
            serviceEnv.put(Context.SECURITY_PRINCIPAL, "CN=SVCLdapQry,OU=ServiceAccounts_Admins,OU=Data Services,DC=corp,DC=something,DC=com"); 
            serviceEnv.put(Context.SECURITY_CREDENTIALS, "ADR0cks!~");

            // Create the initial context

            DirContext serviceCtx;
            try 
            {
                serviceCtx = new InitialDirContext(serviceEnv);
            } 
            catch (NamingException e) 
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

            boolean serviceConnectionResult = serviceCtx != null;

            if(serviceConnectionResult)
            {
                System.out.println("LDAP basic authorization is successful");
            }

            // user to authenticate
            String identifyingAttribute = "samaccountname";
            String ldapUrl = "ldap://pcocpwdom01.corp.something.com:389";
            String base = "DC=corp,DC=something,DC=com";

            // we don't need all attributes, just let it get the identifying one
            String[] attributeFilter = { identifyingAttribute };
            SearchControls sc = new SearchControls();
            sc.setReturningAttributes(attributeFilter);
            sc.setSearchScope(SearchControls.SUBTREE_SCOPE);

         // use a search filter to find only the user we want to authenticate
            String searchFilter = "(" + identifyingAttribute + "=" + usrName + ")";

            NamingEnumeration<SearchResult> results = null;

            try 
            {
                results = serviceCtx.search(base, searchFilter, sc);
            } 
            catch (NamingException e1) 
            {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            DirContext usrCtx = null;
            try {
                if (results.hasMore()) {
                    // get the users DN (distinguishedName) from the result
                    SearchResult result = results.next();
                    String distinguishedName = result.getNameInNamespace();

                    // attempt another authentication, now with the user
                    Properties authEnv = new Properties();
                    authEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
                    authEnv.put(Context.PROVIDER_URL, ldapUrl);
                    authEnv.put(Context.SECURITY_PRINCIPAL, distinguishedName);
                    authEnv.put(Context.SECURITY_CREDENTIALS, pswd);
                    usrCtx = new InitialDirContext(authEnv);

                    System.out.println("Authentication successful");
                    authenticationresullt =  true;
                }
            } catch (NamingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }


          //close the service context
            if(usrCtx != null)
                try 
                {
                    usrCtx.close();
                } 
                catch (NamingException e) 
                {
                    e.printStackTrace();
                    return false;
                }


            //close the service context
            if(serviceCtx != null)
                try 
                {
                    serviceCtx.close();
                } 
                catch (NamingException e) 
                {
                    e.printStackTrace();
                    return false;
                }

            return authenticationresullt;


        }




    }
肉饼

我跟着 :

实现“ Spring Security Active Directory LDAP示例Spring Security Active Directory LDAP”。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何使用spring Security通过基于邮件和uid的LDAP对用户进行身份验证?

来自分类Dev

使用SearchGuard对ldap用户进行身份验证以进行Elasticsearch

来自分类Dev

使用LDAP InMemoryDirectoryServer通过Scala对用户进行身份验证

来自分类Dev

使用 ldap 进行身份验证,无需用户密码

来自分类Dev

使用PHP进行LDAP身份验证

来自分类Dev

使用 Django 进行 LDAP 身份验证

来自分类Dev

Apache shiro:使用ldap进行用户身份验证,使用数据库进行角色/权限?

来自分类Dev

LDAP:无法使用用户名中的句点对用户进行身份验证

来自分类Dev

如何在使用LDAP进行身份验证的项目中正确配置Spring Security的“记住我”选项?

来自分类Dev

如何使用邮件ID而不是用户名对ldap服务器进行身份验证

来自分类Dev

如何使用邮件ID而不是用户名对ldap服务器进行身份验证

来自分类Dev

使用ldap配置文件在django admin中进行身份验证

来自分类Dev

使用ldap配置文件在django admin中进行身份验证

来自分类Dev

使用 Bcrypt 进行用户身份验证

来自分类Dev

在Tomcat 7中使用智能卡和LDAP对用户进行身份验证

来自分类Dev

Symfony2:使用LDAP(活动目录)和DB进行用户身份验证

来自分类Dev

使用 LDAP 和 Windows Active Directory 进行用户身份验证 (Windows Server 2016)

来自分类Dev

使用用户名的Java LDAP身份验证

来自分类Dev

使用Scala或Java进行LDAP身份验证

来自分类Dev

使用JWT Auth对LDAP服务进行身份验证

来自分类Dev

Gerrit无法使用LDAP进行身份验证

来自分类Dev

使用ldap.js进行Active Directory身份验证

来自分类Dev

使用Symfony 2.8进行LDAP身份验证

来自分类Dev

Perl Net :: LDAP:使用当前登录进行身份验证

来自分类Dev

使用Django REST进行LDAP身份验证

来自分类Dev

使用Cakephp 3.x进行LDAP身份验证

来自分类Dev

Samba-仅使用LDAP进行身份验证吗?

来自分类Dev

使用Tomcat LDAP和PHP进行身份验证

来自分类Dev

无法使用Django和ldap进行身份验证

Related 相关文章

  1. 1

    如何使用spring Security通过基于邮件和uid的LDAP对用户进行身份验证?

  2. 2

    使用SearchGuard对ldap用户进行身份验证以进行Elasticsearch

  3. 3

    使用LDAP InMemoryDirectoryServer通过Scala对用户进行身份验证

  4. 4

    使用 ldap 进行身份验证,无需用户密码

  5. 5

    使用PHP进行LDAP身份验证

  6. 6

    使用 Django 进行 LDAP 身份验证

  7. 7

    Apache shiro:使用ldap进行用户身份验证,使用数据库进行角色/权限?

  8. 8

    LDAP:无法使用用户名中的句点对用户进行身份验证

  9. 9

    如何在使用LDAP进行身份验证的项目中正确配置Spring Security的“记住我”选项?

  10. 10

    如何使用邮件ID而不是用户名对ldap服务器进行身份验证

  11. 11

    如何使用邮件ID而不是用户名对ldap服务器进行身份验证

  12. 12

    使用ldap配置文件在django admin中进行身份验证

  13. 13

    使用ldap配置文件在django admin中进行身份验证

  14. 14

    使用 Bcrypt 进行用户身份验证

  15. 15

    在Tomcat 7中使用智能卡和LDAP对用户进行身份验证

  16. 16

    Symfony2:使用LDAP(活动目录)和DB进行用户身份验证

  17. 17

    使用 LDAP 和 Windows Active Directory 进行用户身份验证 (Windows Server 2016)

  18. 18

    使用用户名的Java LDAP身份验证

  19. 19

    使用Scala或Java进行LDAP身份验证

  20. 20

    使用JWT Auth对LDAP服务进行身份验证

  21. 21

    Gerrit无法使用LDAP进行身份验证

  22. 22

    使用ldap.js进行Active Directory身份验证

  23. 23

    使用Symfony 2.8进行LDAP身份验证

  24. 24

    Perl Net :: LDAP:使用当前登录进行身份验证

  25. 25

    使用Django REST进行LDAP身份验证

  26. 26

    使用Cakephp 3.x进行LDAP身份验证

  27. 27

    Samba-仅使用LDAP进行身份验证吗?

  28. 28

    使用Tomcat LDAP和PHP进行身份验证

  29. 29

    无法使用Django和ldap进行身份验证

热门标签

归档