2016-08-31 6 views
1

JNDIを使用している場合、「LDAPでユーザーを認証する」(付録にリストされています)xml構成ファイル、JAVA、Springセキュリティを使用してLDAPを使用してユーザーを認証する方法

要件によると、XML形式(IDLFファイルは使用できません)でspring-securityとconfigファイル(ldap infoを含む)を使用する必要があります。

私は、ユーザーを認証するためにJavaに関連するすべての情報をプルアップするために、このLdap.xmlファイルを使用すること(私は1.8と、スプリングを使用しています)JAVAのコードスニペットを探しています。春のセキュリティを使用する必要があります。

私はそれについて助けを得ることができますか?以下のようなものを探して

<?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> 

LDAP.xmlは次のようになります

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

任意の助けをappreciated.Pleaseは、より多くの情報が必要な場合は私に知らせています。それらのいずれかが仕事をすることができませんでした

が私からの例を試してみました。

付録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; 


     } 




    } 

答えて

関連する問題