...
Code Block | ||
---|---|---|
| ||
/* * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license * distributed with this file and available online at * http://www.ja-sig.org/products/cas/overview/license/ */ package org.jasig.cas.adaptors.ldap.util; import org.springframework.ldap.core.support.LdapContextSource; import javax.naming.Context; import javax.naming.directory.DirContext; import javax.naming.directory.InitialDirContext; import java.util.Enumeration; import java.util.Hashtable; public class AuthenticatedLdapDigestMd5ContextSource extends LdapContextSource { private DirContext context; public Hashtable<String, String> environment; public DirContext getDirContext(final String principal, final String credentials) { environment = (Hashtable) getAnonymousEnv().clone(); String authenMethod = environment.get(Context.SECURITY_AUTHENTICATION); if (authenMethod == null) { environment.put(Context.SECURITY_AUTHENTICATION, "DIGEST-MD5"); } String providerUrl = environment.get(Context.PROVIDER_URL); if (providerUrl == null) { environment.put(Context.PROVIDER_URL, assembleProviderUrlString(getUrls())); } Enumeration keys = environment.keys(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); System.setProperty(key, environment.get(key)); } environment.put(Context.SECURITY_PRINCIPAL, principal); environment.put(Context.SECURITY_CREDENTIALS, credentials); try { // Create the initial directory context context = new InitialDirContext(environment); } catch (Exception e) { System.err.println("Authentication failed: " + e); context = null; } return context; } } |
...