Created ThreadLocalAuthenticationSource.
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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>
|
||||
* <bean id="abstractContextSource" abstract="true"
|
||||
* class="org.springframework.ldap.core.support.LdapContextSource" >
|
||||
* ... Common configuration here ...
|
||||
* </bean>
|
||||
*
|
||||
* <bean id="contextSource"
|
||||
* parent="abstractContextSource" >
|
||||
* <property name="userName" value="..." />
|
||||
* <property name="password" value="..." />
|
||||
* </bean>
|
||||
*
|
||||
* <bean id="authenticationContextSource"
|
||||
* parent="abstractContextSource" >
|
||||
* <property name="authenticationSource">
|
||||
* <bean class="org.springframework.ldap.authentication.ThreadLocalAuthenticationSource" />
|
||||
* </property>
|
||||
* </bean>
|
||||
* </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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user