NIHVIVO-2694 Change how the AuthenticatorFactory is structured, so it will be easier to change on the fly.
This commit is contained in:
parent
cb5e2362b1
commit
0a19ed7d86
5 changed files with 65 additions and 57 deletions
|
@ -8,6 +8,7 @@ import java.util.List;
|
|||
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
|
@ -27,23 +28,33 @@ public abstract class Authenticator {
|
|||
// ----------------------------------------------------------------------
|
||||
// The factory
|
||||
//
|
||||
// Unit tests can replace the factory to get a stub class instead.
|
||||
// Note: this can only work because the factory value is not final.
|
||||
// Each Authenticator instance is used for a single request, so we store
|
||||
// a factory in the context that can create these instances.
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
public static interface AuthenticatorFactory {
|
||||
Authenticator newInstance(HttpServletRequest request);
|
||||
private static final String FACTORY_ATTRIBUTE_NAME = AuthenticatorFactory.class
|
||||
.getName();
|
||||
|
||||
public interface AuthenticatorFactory {
|
||||
Authenticator getInstance(HttpServletRequest request);
|
||||
}
|
||||
|
||||
private static AuthenticatorFactory factory = new AuthenticatorFactory() {
|
||||
@Override
|
||||
public Authenticator newInstance(HttpServletRequest request) {
|
||||
return new BasicAuthenticator(request);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Ask the currently configured AuthenticatorFactory to give us an
|
||||
* Authenticator for this request.
|
||||
*
|
||||
* If there is no factory, configure a Basic one.
|
||||
*/
|
||||
public static Authenticator getInstance(HttpServletRequest request) {
|
||||
return factory.newInstance(request);
|
||||
ServletContext ctx = request.getSession().getServletContext();
|
||||
Object attribute = ctx.getAttribute(FACTORY_ATTRIBUTE_NAME);
|
||||
if (! (attribute instanceof AuthenticatorFactory)) {
|
||||
attribute = new BasicAuthenticator.Factory();
|
||||
ctx.setAttribute(FACTORY_ATTRIBUTE_NAME, attribute);
|
||||
}
|
||||
AuthenticatorFactory factory = (AuthenticatorFactory) attribute;
|
||||
|
||||
return factory.getInstance(request);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
|
|
|
@ -37,6 +37,21 @@ import edu.cornell.mannlib.vitro.webapp.search.indexing.IndexBuilder;
|
|||
public class BasicAuthenticator extends Authenticator {
|
||||
private static final Log log = LogFactory.getLog(BasicAuthenticator.class);
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// The factory
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
public static class Factory implements AuthenticatorFactory {
|
||||
@Override
|
||||
public Authenticator getInstance(HttpServletRequest req) {
|
||||
return new BasicAuthenticator(req);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------
|
||||
// The authenticator
|
||||
// ----------------------------------------------------------------------
|
||||
|
||||
private final HttpServletRequest request;
|
||||
|
||||
public BasicAuthenticator(HttpServletRequest request) {
|
||||
|
@ -134,10 +149,11 @@ public class BasicAuthenticator extends Authenticator {
|
|||
setSessionTimeoutLimit(userAccount, session);
|
||||
recordInUserSessionMap(userAccount.getUri(), session);
|
||||
notifyOtherUsers(userAccount.getUri(), session);
|
||||
|
||||
|
||||
if (IsRootUser.isRootUser(RequestIdentifiers
|
||||
.getIdBundleForRequest(request))) {
|
||||
IndexBuilder.checkIndexOnRootLogin(request); }
|
||||
IndexBuilder.checkIndexOnRootLogin(request);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue