Created ThreadLocalAuthenticationSource.

This commit is contained in:
Mattias Arthursson
2007-03-01 18:27:59 +00:00
parent 29ea8600fb
commit c4b6243995
2 changed files with 135 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package org.springframework.ldap.authentication;
/**
* Holder for ThreadLocal values of principal and credentials, used by
* {@link ThreadLocalAuthenticationSource} to get its values.
*
* @author Mattias Arthursson
*
*/
public class ThreadLocalAuthenticationHolder {
private static ThreadLocal threadPrincipal = new ThreadLocal();
private static ThreadLocal threadCredentials = new ThreadLocal();
/**
* Not to be instantiated.
*/
private ThreadLocalAuthenticationHolder() {
}
/**
* Set the ThreadLocal principal DN.
*
* @param principal
* the principal to be returned in this thread by
* {@link ThreadLocalAuthenticationSource#getPrincipal()}.
*/
public static void setPrincipal(String principal) {
threadPrincipal.set(principal);
}
/**
* Set the ThreadLocal credentials.
*
* @param credentials
* the credentials to be returned in this thread by
* {@link ThreadLocalAuthenticationSource#getCredentials()}.
*/
public static void setCredentials(String credentials) {
threadCredentials.set(credentials);
}
/**
* Get the currently bound principal.
*
* @return the currently bound principal.
*/
public static String getPrincipal() {
return (String) threadPrincipal.get();
}
/**
* Get the currently bound credentials.
*
* @return the currently bound credentials.
*/
public static String getCredentials() {
return (String) threadCredentials.get();
}
}

View File

@@ -0,0 +1,75 @@
package org.springframework.ldap.authentication;
import org.springframework.ldap.core.AuthenticationSource;
/**
* {@link AuthenticationSource} to be used if the principal and credentials
* should be returned on a ThreadLocal basis. With this
* <code>AuthenticationSource</code>, the {@link #getPrincipal()} and
* {@link #getCredentials()} methods return the values set using
* {@link ThreadLocalAuthenticationHolder#setPrincipal(String)} and
* {@link ThreadLocalAuthenticationHolder#setCredentials(String)} respectively,
* thus enabling use of a singleton ContextSource but still enabling the
* authentication information to be thread specific.
* <p>
* This <code>AuthenticationSource</code> can be used e.g. to implement a
* simple authentication mechanism using <code>ContextSource</code>, like so:
* <p>
* <b>Configuration</b>:
* <pre>
* &lt;bean id="abstractContextSource" abstract="true"
* class="org.springframework.ldap.core.support.LdapContextSource" &gt;
* ... Common configuration here ...
* &lt;/bean&gt;
*
* &lt;bean id="contextSource"
* parent="abstractContextSource" &gt;
* &lt;property name="userName" value="..." /&gt;
* &lt;property name="password" value="..." /&gt;
* &lt;/bean&gt;
*
* &lt;bean id="authenticationContextSource"
* parent="abstractContextSource" &gt;
* &lt;property name="authenticationSource"&gt;
* &lt;bean class="org.springframework.ldap.authentication.ThreadLocalAuthenticationSource" /&gt;
* &lt;/property&gt;
* &lt;/bean&gt;
* </pre>
* <p>
* <b>Authentication code</b>:
* <pre>
* public class MyDao {
* private ContextSource authenticationContextSource;
*
* public void setAuthenticationContextSource(ContextSource contextSource){
* this.authenticationContextSource = contextSource;
* }
*
* ...
*
* public boolean authenticate(String userDN, String credentials){
* ThreadLocalAuthenticationHolder.setPrincipal(userDN);
* ThreadLocalAuthenticationHolder.setCredentials(credentials);
*
* try{
* authenticationContextSource.getReadWriteContext();
* return true;
* }catch(Exception e){
* return false;
* }
* }
* }
* </pre>
*
* @author Mattias Arthursson
*
*/
public class ThreadLocalAuthenticationSource implements AuthenticationSource {
public String getCredentials() {
return ThreadLocalAuthenticationHolder.getCredentials();
}
public String getPrincipal() {
return ThreadLocalAuthenticationHolder.getPrincipal();
}
}