1 17 package org.alfresco.repo.security.authentication.ldap; 18 19 import javax.naming.NamingException ; 20 import javax.naming.directory.InitialDirContext ; 21 22 import org.alfresco.repo.security.authentication.AbstractAuthenticationComponent; 23 import org.alfresco.repo.security.authentication.AuthenticationException; 24 25 30 public class LDAPAuthenticationComponentImpl extends AbstractAuthenticationComponent 31 { 32 33 private String userNameFormat; 34 35 private LDAPInitialDirContextFactory ldapInitialContextFactory; 36 37 public LDAPAuthenticationComponentImpl() 38 { 39 super(); 40 } 41 42 43 public void setLDAPInitialDirContextFactory(LDAPInitialDirContextFactory ldapInitialDirContextFactory) 44 { 45 this.ldapInitialContextFactory = ldapInitialDirContextFactory; 46 } 47 48 49 public void setUserNameFormat(String userNameFormat) 50 { 51 this.userNameFormat = userNameFormat; 52 } 53 54 57 public void authenticate(String userName, char[] password) throws AuthenticationException 58 { 59 InitialDirContext ctx = null; 60 try 61 { 62 ctx = ldapInitialContextFactory.getInitialDirContext(String.format(userNameFormat, new Object []{userName}), new String (password)); 63 64 setCurrentUser(userName); 67 68 } 69 finally 70 { 71 if(ctx != null) 72 { 73 try 74 { 75 ctx.close(); 76 } 77 catch (NamingException e) 78 { 79 clearCurrentSecurityContext(); 80 throw new AuthenticationException("Failed to close connection", e); 81 } 82 } 83 } 84 } 85 86 87 @Override 88 protected boolean implementationAllowsGuestLogin() 89 { 90 InitialDirContext ctx = null; 91 try 92 { 93 ctx = ldapInitialContextFactory.getDefaultIntialDirContext(); 94 return true; 95 96 } 97 catch(Exception e) 98 { 99 return false; 100 } 101 finally 102 { 103 if(ctx != null) 104 { 105 try 106 { 107 ctx.close(); 108 } 109 catch (NamingException e) 110 { 111 throw new AuthenticationException("Failed to close connection", e); 112 } 113 } 114 } 115 } 116 117 118 } 119 | Popular Tags |