📄 authenticationschemesequence.java
字号:
/*
* SSL-Explorer
*
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package com.sslexplorer.security;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.sslexplorer.core.CoreServlet;
import com.sslexplorer.policyframework.AbstractResource;
import com.sslexplorer.policyframework.PolicyConstants;
import com.sslexplorer.policyframework.Resource;
/**
* This is the default implementation of an <i>Authentication Scheme</i> that
* loads the scheme from the <i>System Database</i> as if it were a {@link com.sslexplorer.policyframework.Resource}.
*
* @author Brett Smith <brett@3sp.com>
*/
public class AuthenticationSchemeSequence extends AbstractResource implements AuthenticationScheme {
final static Log log = LogFactory.getLog(AuthenticationSchemeSequence.class);
// Private instance variables
private List modules;
private HttpSession servletSession;
private int current;
private List authenticationModules;
private User user;
private String username;
private List allCredentials;
private AccountLock lock;
private boolean enabled;
/**
* Constructor
*
* @param resourceId
* @param resourceName
* @param resourceDescription
* @param dateAmended
* @param dateCreated
* @param enabled enabled
*/
public AuthenticationSchemeSequence(int resourceId, String resourceName, String resourceDescription, Calendar dateAmended, Calendar dateCreated, boolean enabled) {
super(PolicyConstants.AUTHENTICATION_SCHEMES_RESOURCE_TYPE, resourceId, resourceName, resourceDescription, 0, dateAmended, dateCreated);
current = -1;
modules = new ArrayList();
allCredentials = new ArrayList();
this.enabled = enabled;
}
/**
* Add a new authentication module to this scheme
*
* @param module name of module to add
*/
public void addModule(String module) {
if(!modules.contains(module)) {
modules.add(module);
}
}
/**
* Get if this scheme contains the specified module
*
* @param name name of module to test for
* @return has module
*/
public boolean hasModule(String name) {
return modules.contains(name);
}
/**
* Remove a module from this scheme
*
* @param module module to remove
*/
public void removeModule(String module) {
modules.remove(module);
}
/**
* Get an iterator of modules contained within this scheme.
*
* @return modules
*/
public Iterator modules() {
return modules.iterator();
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#init(javax.servlet.http.HttpSession)
*/
public void init(HttpSession servletSession) throws Exception {
this.servletSession = servletSession;
authenticationModules = new ArrayList();
for(Iterator i = modules.iterator(); i.hasNext(); ) {
String moduleId = (String)i.next();
AuthenticationModule module = AuthenticationModuleManager.getInstance().createModule(moduleId);
authenticationModules.add(module);
module.init(this);
}
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getCurrentModuleIndex()
*/
public int getCurrentModuleIndex() {
return current;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getUser()
*/
public User getUser() {
return user;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#setUser(com.sslexplorer.security.User)
*/
public void setUser(User user) {
this.user = user;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getServletSession()
*/
public HttpSession getServletSession() {
return servletSession;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#nextAuthenticationModule()
*/
public AuthenticationModule nextAuthenticationModule() {
if( ( current + 1 ) < authenticationModules.size()) {
AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(++current);
return mod;
}
return null;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#currentAuthenticationModule()
*/
public AuthenticationModule currentAuthenticationModule() {
if(current != -1) {
AuthenticationModule mod = (AuthenticationModule)authenticationModules.get(current);
return mod;
}
return null;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#authenticationComplete(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void authenticationComplete(HttpServletRequest request, HttpServletResponse response) throws Exception {
for(Iterator i = authenticationModules.iterator();i.hasNext(); ) {
AuthenticationModule mod = (AuthenticationModule)i.next();
if (log.isDebugEnabled())
log.debug("Informing module " + mod.getName() + " that authentication is complete");
mod.authenticationComplete();
// Only inform the first module when the session is locked
if(request.getSession().getAttribute(Constants.SESSION_LOCKED) != null) {
break;
}
}
CoreServlet servlet = CoreServlet.getServlet();
LogonController logonController = servlet.getLogonController();
logonController.logon(request, response, this);
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getUsername()
*/
public String getUsername() {
return username;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#setUsername(java.lang.String)
*/
public void setUsername(String username) {
this.username = username;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#addCredentials(com.sslexplorer.security.Credentials)
*/
public void addCredentials(Credentials credentials) {
allCredentials.add(credentials);
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#credentials()
*/
public Iterator credentials() {
return allCredentials.iterator();
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#setAccountLock(com.sslexplorer.security.AccountLock)
*/
public void setAccountLock(AccountLock lock) {
this.lock = lock;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getAccountLock()
*/
public AccountLock getAccountLock() {
return lock;
}
/**
* Move the specified module up one in the list. If the module is
* already at the top of the scheme no action will occur.
*
* @param module module to move up in the scheme
*/
public void moveUp(String module) {
int idx = modules.indexOf(module);
if(idx > 0) {
String swap = (String)modules.get(idx - 1);
modules.remove(idx - 1);
modules.add(idx, swap);
}
}
/**
* Remove all modules from this sequence
*/
public void clearModules() {
modules.clear();
}
/**
* Move the specified module down one in the list. If the module is
* already at the bottom of the scheme no action will occur.
*
* @param module module to move down in the scheme
*/
public void moveDown(String module) {
int idx = modules.indexOf(module);
if( ( idx + 1 ) < modules.size() ) {
String swap = (String)modules.get(idx + 1);
modules.remove(idx + 1);
modules.add(idx, swap);
}
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getModuleCount()
*/
public int getModuleCount() {
return modules.size();
}
/**
* Get the module at the specified index.
*
* @param index index of module
* @return module
*/
public String getModule(int index) {
return (String)modules.get(index);
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getSessionLocked()
*/
public boolean getSessionLocked() {
return getServletSession() != null ? ( getServletSession().getAttribute(Constants.SESSION_LOCKED) != null ) : false;
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getSchemeName()
*/
public String getSchemeName() {
return getResourceName();
}
/* (non-Javadoc)
* @see com.sslexplorer.security.AuthenticationScheme#getEnabled()
*/
public boolean getEnabled() {
return enabled;
}
/**
* Set whether this scheme is enabled
*
* @param enabled enabled
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
/**
* Get if the scheme contains only system authentication modules
*
* @return system authentication modules only
*/
public boolean isSystemScheme() {
if(modules.size() == 0) {
return false;
}
for(Iterator i = modules(); i.hasNext(); ) {
String mod = (String)i.next();
if(!AuthenticationModuleManager.getInstance().getModuleDefinition(mod).getSystem()) {
return false;
}
}
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -