1 28 29 package com.caucho.server.security; 30 31 import com.caucho.config.types.InitParam; 32 import com.caucho.security.BasicPrincipal; 33 34 import javax.annotation.PostConstruct; 35 import javax.naming.Context ; 36 import javax.naming.NamingException ; 37 import javax.naming.directory.Attribute ; 38 import javax.naming.directory.Attributes ; 39 import javax.naming.directory.InitialDirContext ; 40 import javax.servlet.ServletContext ; 41 import javax.servlet.ServletException ; 42 import javax.servlet.http.HttpServletRequest ; 43 import javax.servlet.http.HttpServletResponse ; 44 import java.security.Principal ; 45 import java.util.Hashtable ; 46 import java.util.logging.Level ; 47 48 51 public class LdapAuthenticator extends AbstractAuthenticator { 52 private String _userAttribute = "uid"; 53 private String _passwordAttribute = "userPassword"; 54 private String _dnPrefix; 55 private String _dnSuffix; 56 57 private Hashtable <String ,String > _jndiEnv = 58 new Hashtable <String ,String >(); 59 60 public LdapAuthenticator() 61 { 62 _jndiEnv.put(Context.INITIAL_CONTEXT_FACTORY, 63 "com.sun.jndi.ldap.LdapCtxFactory"); 64 _jndiEnv.put(Context.PROVIDER_URL, 65 "ldap://localhost:389"); 66 } 67 68 public void setDNPrefix(String prefix) 69 { 70 _dnPrefix = prefix; 71 } 72 73 public void setDNSuffix(String suffix) 74 { 75 _dnSuffix = suffix; 76 } 77 78 public void addJNDIEnv(InitParam init) 79 { 80 _jndiEnv.putAll(init.getParameters()); 81 } 82 83 public void setUserAttribute(String user) 84 { 85 _userAttribute = user; 86 } 87 88 public void setPasswordAttribute(String password) 89 { 90 _passwordAttribute = password; 91 } 92 93 96 @PostConstruct 97 public synchronized void init() 98 throws ServletException 99 { 100 super.init(); 101 } 102 103 106 protected Principal loginImpl(HttpServletRequest request, 107 HttpServletResponse response, 108 ServletContext application, 109 String userName, String password) 110 throws ServletException 111 { 112 try { 113 Hashtable env = new Hashtable (); 114 115 env.putAll(_jndiEnv); 116 117 InitialDirContext ic = new InitialDirContext (env); 118 119 String query = _userAttribute + '=' + userName; 120 121 if (_dnPrefix != null && ! _dnPrefix.equals("")) 122 query = _dnPrefix + ',' + query; 123 124 if (_dnSuffix != null && ! _dnSuffix.equals("")) 125 query = query + ',' + _dnSuffix; 126 127 Attributes attributes = ic.getAttributes(query); 128 129 if (log.isLoggable(Level.FINE)) 130 log.fine("ldap-authenticator: " + query + "->" + (attributes != null)); 131 132 if (attributes == null) 133 return null; 134 135 Attribute passwordAttr = attributes.get(_passwordAttribute); 136 137 if (passwordAttr == null) 138 return null; 139 140 String ldapPassword = (String ) passwordAttr.get(); 141 142 if (! password.equals(ldapPassword)) 143 return null; 144 145 return new BasicPrincipal(userName); 146 } catch (NamingException e) { 147 log.log(Level.FINE, e.toString(), e); 148 149 return null; 150 } catch (Throwable e) { 151 throw new ServletException (e); 152 } 153 } 154 155 162 public boolean isUserInRole(HttpServletRequest request, 163 HttpServletResponse response, 164 ServletContext application, 165 Principal principal, String role) 166 throws ServletException 167 { 168 return principal != null; 169 } 170 } 171 | Popular Tags |