2017-10-09 1 views
1

LDAPサーバーに対して一連の資格情報を認証しようとしましたが、それらを正常に認証できました。今、サーバーにログインしているユーザーのフルネームまたは表示名を取得しようとしています。同じことができない。 LDAPの概念を初めて知っているので、ユーザーの完全な表示名を取得する方法を見つけることができません。 。いくつかを以下にADとApache shiroを使用してLDAPサーバーからフルネームまたは表示名を取得

がshiro.iniファイルでログインしているユーザーの完全な表示名を取得する方法を私を助けてくださいすることができます使用しています:

[main] 
activeDirectoryRealm = 
org.apache.shiro.realm.activedirectory.ActiveDirectoryRealm 
activeDirectoryRealm.systemUsername = adminusername 
activeDirectoryRealm.systemPassword = adminpswd 
activeDirectoryRealm.searchBase = "OU=User Accounts,DC=dmn,DC=net" 
activeDirectoryRealm.url = ldaps://localhost:389 

私のJavaコードは以下の通りです:

import org.apache.shiro.SecurityUtils; 
import org.apache.shiro.authc.*; 
import org.apache.shiro.config.IniSecurityManagerFactory; 
import org.apache.shiro.mgt.SecurityManager; 
import org.apache.shiro.subject.Subject; 
import org.apache.shiro.util.Factory; 
public class ExampleActiveDirectory { 


public static final String userName = "myusername"; 
public static final String password = "mypassword"; 

public static void main(String[] args) 
{ 
    //Factory<SecurityManager> factory = new IniSecurityManagerFactory("N:\\workspace\\LdapAuthentication\\src\\auth.ini"); 
    Factory<SecurityManager> factory = new IniSecurityManagerFactory("N:\\workspace\\LdapAuthentication\\src\\shiro.ini"); 
    SecurityManager securityManager = factory.getInstance(); 
    SecurityUtils.setSecurityManager(securityManager); 
    System.out.println("userName is : " +userName); 
    System.out.println("password is : " +password); 
    UsernamePasswordToken token = new UsernamePasswordToken(userName,password); 
    Subject currentUser = SecurityUtils.getSubject(); 
    try 
    { 
     //currentUser.login(token) ; 
     securityManager.login(currentUser,token).isAuthenticated(); 
     System.out.println("We've authenticated! :)"); 
    } 
    catch (AuthenticationException e) 
    { 
     System.out.println("We did not authenticate :("); 
     e.printStackTrace(); 
    } 

    } 
} 
+0

currentUser.getPrincipalは()あなたがなめらか与えるだろう。 john doeのようなj.doeのようなものです。それ以外の場合は、表示したい結果の例を提供してください。 –

+0

私は、次のような結果を探していました。jdoeとしてユーザー名を入力した場合、コードは次のような出力を提供する必要があります。 – Arjun

+1

次にShiroのActiveDirectoryRealmクラスから拡張し、 "getRoleNamesForUser" searchBaseとsearchFilterを使用して結果を取得すると、AD上で検索が実行されます。このような場合は、検索ベースとフィルタを使用して独自のメソッドを作成し、「memberOf」ではなく「name」属性を探します。この属性は、あなたが探している価値を与えるはずです。 –

答えて

1

ありがとうございます。 リンク - 以下のような解決策を見つけたhttp://www.deepakgaikwad.net/index.php/2009/09/24/retrieve-basic-user-attributes-from-active-directory-using-ldap-in-java.html

import java.util.Hashtable; 
import javax.naming.Context; 
import javax.naming.NamingEnumeration; 
import javax.naming.NamingException; 
import javax.naming.directory.Attributes; 
import javax.naming.directory.SearchControls; 
import javax.naming.directory.SearchResult; 
import javax.naming.ldap.InitialLdapContext; 
import javax.naming.ldap.LdapContext; 

import org.apache.shiro.web.tags.UserTag; 

public class RetrieveUserAttributes { 

public static void main(String[] args) { 
    RetrieveUserAttributes retrieveUserAttributes = new RetrieveUserAttributes(); 
    retrieveUserAttributes.getUserBasicAttributes("username", retrieveUserAttributes.getLdapContext()); 
} 

public LdapContext getLdapContext(){ 
    LdapContext ctx = null; 
    try{ 
     Hashtable<String, String> env = new Hashtable<String, String>(); 
     env.put(Context.INITIAL_CONTEXT_FACTORY, 
       "com.sun.jndi.ldap.LdapCtxFactory"); 
     env.put(Context.SECURITY_AUTHENTICATION, "Simple"); 
     env.put(Context.SECURITY_PRINCIPAL, "adminusername"); 
     env.put(Context.SECURITY_CREDENTIALS, "adminpswrd"); 
     env.put(Context.PROVIDER_URL, "ldaps://localhost:389"); 
     ctx = new InitialLdapContext(env, null); 
     System.out.println("Connection Successful."); 
    }catch(NamingException nex){ 
     System.out.println("LDAP Connection: FAILED"); 
     nex.printStackTrace(); 
    } 
    return ctx; 
} 

UserTag getUserBasicAttributes(String username, LdapContext ctx) { 
    UserTag user=null; 
    try { 

     SearchControls constraints = new SearchControls(); 
     constraints.setSearchScope(SearchControls.SUBTREE_SCOPE); 
     String[] attrIDs = { "distinguishedName", 
       "sn", 
       "givenname", 
       "mail", 
       "telephonenumber"}; 
     constraints.setReturningAttributes(attrIDs); 
     //First input parameter is search bas, it can be "CN=Users,DC=YourDomain,DC=com" 
     //Second Attribute can be uid=username 
     NamingEnumeration answer = ctx.search("DC=domain,DC=com", "sAMAccountName=" 
       + "username", constraints); 
     if (answer.hasMore()) { 
      Attributes attrs = ((SearchResult) answer.next()).getAttributes(); 
      System.out.println("distinguishedName "+ attrs.get("distinguishedName")); 
      System.out.println("givenname "+ attrs.get("givenname")); 
      System.out.println("sn "+ attrs.get("sn")); 
      System.out.println("mail "+ attrs.get("mail")); 
      System.out.println("telephonenumber "+ attrs.get("telephonenumber")); 
     }else{ 
      throw new Exception("Invalid User"); 
     } 

    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
    return user; 
} 

} 
関連する問題