📄 authenticate.java
字号:
/*------------------------------------------------------------------------------Name: Authenticate.javaProject: xmlBlaster.orgCopyright: xmlBlaster.org, see xmlBlaster-LICENSE fileComment: Login for clients------------------------------------------------------------------------------*/package org.xmlBlaster.authentication;import java.util.logging.Logger;import java.util.logging.Level;import org.xmlBlaster.authentication.plugins.CryptDataHolder;import org.xmlBlaster.authentication.plugins.PluginManager;import org.xmlBlaster.authentication.plugins.I_Manager;import org.xmlBlaster.authentication.plugins.I_Session;import org.xmlBlaster.authentication.plugins.I_Subject;import org.xmlBlaster.engine.qos.ConnectQosServer;import org.xmlBlaster.engine.qos.DisconnectQosServer;import org.xmlBlaster.util.def.Constants;import org.xmlBlaster.util.def.MethodName;import org.xmlBlaster.util.IsoDateParser;import org.xmlBlaster.util.MsgUnitRaw;import org.xmlBlaster.util.Timestamp;import org.xmlBlaster.util.XmlBlasterException;import org.xmlBlaster.util.def.ErrorCode;import org.xmlBlaster.util.dispatch.ConnectionStateEnum;import org.xmlBlaster.util.SessionName;import org.xmlBlaster.engine.qos.ConnectReturnQosServer;import org.xmlBlaster.util.qos.storage.CbQueueProperty;import org.xmlBlaster.engine.XmlBlasterImpl;import org.xmlBlaster.engine.ServerScope;import org.xmlBlaster.engine.runlevel.RunlevelManager;import org.xmlBlaster.engine.runlevel.I_RunlevelListener;import org.xmlBlaster.protocol.I_Authenticate;import org.xmlBlaster.protocol.I_XmlBlaster;import java.util.*;/** * Authenticate a client via login. * <p> * The login method serves as a factory for a xmlBlaster.Server Reference */final public class Authenticate implements I_RunlevelListener{ final private String ME; private final PluginManager plgnLdr;// private Hashtable sessions = new Hashtable(); /** Unique counter to generate IDs */ private long counter = 1; private final ServerScope glob; private static Logger log = Logger.getLogger(Authenticate.class.getName()); /** * With this map you can find a client using a sessionId. * * key = sessionId A unique identifier * value = SessionInfo object, containing all data about a client */ final private Map sessionInfoMap = new HashMap(); /** * With this map you can find a client using his login name. * * key = loginName, the unique login name of a client * value = SessionInfo object, containing all data about a client */ final private Map loginNameSubjectInfoMap = new HashMap(); /** * For listeners who want to be informed about login/logout */ final private Set clientListenerSet = new HashSet(); /** The singleton handle for this xmlBlaster server */ private final I_XmlBlaster xmlBlasterImpl; private boolean acceptWrongSenderAddress; // My security delegate layer which is exposed to the protocol plugins //private final AuthenticateProtector encapsulator; /** */ public Authenticate(ServerScope global) throws XmlBlasterException { this.glob = global; this.ME = "Authenticate" + glob.getLogPrefixDashed(); if (log.isLoggable(Level.FINER)) log.finer("Entering constructor"); /*this.encapsulator = */new AuthenticateProtector(glob, this); // my security layer (delegate) glob.getRunlevelManager().addRunlevelListener(this); plgnLdr = new PluginManager(global); plgnLdr.init(this); xmlBlasterImpl = new XmlBlasterImpl(this); // TODO: Decide by authorizer, see SessionInfo.java with specific setting this.acceptWrongSenderAddress = glob.getProperty().get("xmlBlaster/acceptWrongSenderAddress", false); } /** * Just to testing sync * @return */ public Map getSessionInfoMap() { return this.sessionInfoMap; } public ServerScope getGlobal() { return this.glob; } /** * Access the xmlBlaster singleton. */ public I_XmlBlaster getXmlBlaster() { return xmlBlasterImpl; } public String login(String loginName, String passwd, String xmlQoS_literal, String secretSessionId) throws XmlBlasterException { Thread.dumpStack(); log.severe("login() not implemented"); throw new XmlBlasterException(glob, ErrorCode.INTERNAL_NOTIMPLEMENTED, ME, "login() not implemented and deprecated"); } /** * Use this to create a user and session for internal users only. * This method is a security risk never allow external code to call it (there is no * passwd needed). * Note that the security instances are created rawish, * they are not registered with the Authentication server. */ public SessionInfo unsecureCreateSession(org.xmlBlaster.client.qos.ConnectQos connectQos) throws XmlBlasterException { SessionName sessionName = connectQos.getSessionName(); if (log.isLoggable(Level.FINER)) log.finer("Entering unsecureCreateSession(" + sessionName + ")"); String secretSessionId = createSessionId(sessionName.getLoginName()); org.xmlBlaster.authentication.plugins.simple.Manager manager = new org.xmlBlaster.authentication.plugins.simple.Manager(); manager.init(glob, null); I_Session session = new org.xmlBlaster.authentication.plugins.simple.Session(manager, secretSessionId); org.xmlBlaster.authentication.plugins.I_SecurityQos securityQos = new org.xmlBlaster.authentication.plugins.simple.SecurityQos(this.glob, sessionName.getLoginName(), ""); session.init(securityQos); I_Subject subject = session.getSubject(); SubjectInfo subjectInfo = null; if (sessionName.getLoginName().startsWith("__")) { // __RequestBroker_internal // strip the pubSessionId and create a subjectInfo ... SessionName subjectName = new SessionName(glob, sessionName.getNodeId(), sessionName.getLoginName()); subjectInfo = new SubjectInfo(getGlobal(), this, subjectName); synchronized(this.loginNameSubjectInfoMap) { this.loginNameSubjectInfoMap.put(subjectInfo.getLoginName(), subjectInfo); } subjectInfo.toAlive(subject, new CbQueueProperty(getGlobal(), Constants.RELATING_SUBJECT, null)); } else { subjectInfo = getOrCreateSubjectInfoByName(sessionName, false, subject, new CbQueueProperty(getGlobal(), Constants.RELATING_SUBJECT, null)); } SessionInfo sessionInfo = subjectInfo.getSession(sessionName); if (sessionInfo == null) { sessionInfo = new SessionInfo(subjectInfo, session, new ConnectQosServer(glob, connectQos.getData()), getGlobal()); } return sessionInfo; } /** * Login to xmlBlaster. */ public final ConnectReturnQosServer connect(ConnectQosServer xmlQos) throws XmlBlasterException { return connect(xmlQos, null); } /** * Login to xmlBlaster. * * If no secretSessionId==null, the secretSessionId from xmlQoS_literal is used, * if this is null as well, we generate one. * <p /> * The given secretSessionId (in the qos) from the client could be from e.g. a2Blaster, * and will be used here as is, the a2Blaster plugin verifies it. * The extra parameter secretSessionId is the CORBA internal POA session id. * <p /> * * @param connectQos The login/connect QoS, see ConnectQosServer.java * @param secretSessionId The caller (here CORBA-POA protocol driver) may insist to you its own secretSessionId */ public /*synchronized*/ final ConnectReturnQosServer connect(ConnectQosServer connectQos, String secretSessionId) throws XmlBlasterException { if (connectQos.getSessionQos().getSessionName().getLoginName().equals(this.glob.getId())) { String text = "You are not allowed to login with the cluster node name " + connectQos.getSessionName().toString() + ", access denied."; log.warning(text); throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION_IDENTICALCLIENT, ME+".connect()", text); } // [1] Try reconnecting with secret sessionId try { if (log.isLoggable(Level.FINE)) log.fine("Entering connect(sessionName=" + connectQos.getSessionName().getAbsoluteName() + ")"); // " secretSessionId=" + secretSessionId + ")"); if (log.isLoggable(Level.FINEST)) log.finest("ConnectQos=" + connectQos.toXml()); // Get or create the secretSessionId (we respect a user supplied secretSessionId) ... if (secretSessionId == null || secretSessionId.length() < 2) { secretSessionId = connectQos.getSessionQos().getSecretSessionId(); if (secretSessionId != null && secretSessionId.length() >= 2) log.info("Using secretSessionId '" + secretSessionId + "' from ConnectQos"); } if (secretSessionId != null && secretSessionId.length() >= 2) { SessionInfo info = getSessionInfo(secretSessionId); if (info != null) { // authentication succeeded info.updateConnectQos(connectQos); ConnectReturnQosServer returnQos = new ConnectReturnQosServer(glob, info.getConnectQos().getData()); returnQos.getSessionQos().setSecretSessionId(secretSessionId); returnQos.getSessionQos().setSessionName(info.getSessionName()); returnQos.setReconnected(true); returnQos.getData().addClientProperty(Constants.CLIENTPROPERTY_RCVTIMESTAMPSTR, IsoDateParser.getCurrentUTCTimestampNanos()); log.info("Reconnected with given secretSessionId."); return returnQos; } } } catch (Throwable e) { log.severe("Internal error when trying to reconnect to session " + connectQos.getSessionName() + " with secret session ID: " + e.toString()); e.printStackTrace(); throw XmlBlasterException.convert(glob, ME, ErrorCode.INTERNAL_CONNECTIONFAILURE.toString(), e); } // [2] Try reconnecting with publicSessionId if (connectQos.hasPublicSessionId()) { SessionInfo info = getSessionInfo(connectQos.getSessionName()); if (info != null && !info.isShutdown() && !info.getConnectQos().bypassCredentialCheck()) { if (connectQos.getSessionQos().reconnectSameClientOnly()) { String text = "Only the creator of session " + connectQos.getSessionName().toString() + " may reconnect, access denied."; log.warning(text); throw new XmlBlasterException(glob, ErrorCode.USER_CONFIGURATION_IDENTICALCLIENT, ME+".connect()", text); } try { // Check password as we can't trust the public session ID boolean ok = info.getSecuritySession().verify(connectQos.getSecurityQos()); if (!ok) throw new XmlBlasterException(glob, ErrorCode.USER_SECURITY_AUTHENTICATION_ACCESSDENIED, ME, "Access denied for " + connectQos.getSecurityQos().getUserId() + " " + connectQos.getClientPluginType()); String oldSecretSessionId = info.getSecretSessionId(); if (secretSessionId == null || secretSessionId.length() < 2) { // Keep the old secretSessionId connectQos.getSessionQos().setSecretSessionId(oldSecretSessionId); } else { // The CORBA driver insists in a new secretSessionId changeSecretSessionId(oldSecretSessionId, secretSessionId); connectQos.getSessionQos().setSecretSessionId(secretSessionId); } // fireClientUpdateQosEvent(info, connectQos); info.updateConnectQos(connectQos);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -