opennmstomcatrealm.java

来自「opennms得相关源码 请大家看看」· Java 代码 · 共 549 行 · 第 1/2 页

JAVA
549
字号
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2004 The OpenNMS Group, Inc.  All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// Copyright (C) 2004 Eric Molitor (eric@tuxbot.com)// Copyright (C) 1999-2001 Oculan Corp.  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.//// For more information contact://      OpenNMS Licensing       <license@opennms.org>//      http://www.opennms.org///      http://www.opennms.com///package org.opennms.web.authenticate;import java.beans.PropertyChangeListener;import java.beans.PropertyChangeSupport;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.security.Principal;import java.util.Arrays;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import org.apache.catalina.Container;import org.apache.catalina.LifecycleException;import org.apache.catalina.realm.Constants;import org.apache.catalina.realm.RealmBase;import org.apache.catalina.util.StringManager;import org.apache.log4j.Category;import org.exolab.castor.xml.MarshalException;import org.exolab.castor.xml.ValidationException;import org.opennms.core.resource.Vault;import org.opennms.core.utils.BundleLists;import org.opennms.netmgt.ConfigFileConstants;import org.opennms.netmgt.config.UserFactory;import org.opennms.netmgt.config.users.User;/** * Implements the interface to allow Tomcat to check our users.xml file * to authenticate users. * <p/> * <p>This class is Tomcat-specific and will not be portable to other * servlet containers. It relies on packages supplied with Tomcat.</p> * * @author <A HREF="mailto:larry@opennms.org">Lawrence Karnowski</A> * @author <A HREF="mailto:eric@tuxbot.com">Eric Molitor</A> * @author <A HREF="http://www.opennms.org/">OpenNMS</A> */public class OpenNMSTomcatRealm extends RealmBase {    /**     * The relative path to find the users.xml file     */    protected String HOME_DIR = "/opt/OpenNMS/";    /**     * Descriptive information about this Realm implementation.     */    protected final String info = "org.opennms.web.authenticate.OpenNMSTomcatRealm/1.0";    /**     * Descriptive information about this Realm implementation.     */    protected static final String name = "OpenNMSTomcatRealm";        /**     * The global JNDI name of the <code>UserDatabase</code> resource     * we will be utilizing.     */    protected String resourceName = "UserDatabase";    /**     * The string manager for this package.     */    private static StringManager sm =        StringManager.getManager(Constants.Package);    /**     * The set of valid Principals for this Realm, keyed by user name.     */    protected HashMap principals = new HashMap();    /**     * Convenient support for <em>PropertyChangeEvents</em>.     */    protected PropertyChangeSupport propertyChangeSupport;    /**     * The magic-users.properties file that is read for the list of special     * users, their passwords, and authorization roles.     */    protected File magicUsersFile;    /**     * The time (in milliseconds) that the magic-users.properties file was     * last modified.  This value is kept so that the users.xml     * file will be reparsed anytime it is modified.     */    protected long magicUsersLastModified = 0;    /**     * The Log4J category for logging web authentication messages.     */    protected Category log = Authentication.log;    /**     * A mapping of special roles to authorized users.  Each role name key     * contains a <code>List</code> value of authorized user names.     */    protected Map magicRoleMapping = new HashMap();    /**     * Create a new instance.     */    public OpenNMSTomcatRealm() {        this.propertyChangeSupport = new PropertyChangeSupport(this);        Vault.getProperties().setProperty("opennms.home", HOME_DIR);    }    /**     * Return the global JNDI name of the <code>UserDatabase</code> resource     * we will be using.     */    public String getResourceName() {        return resourceName;    }    /**     * Set the global JNDI name of the <code>UserDatabase</code> resource     * we will be using.     *     * @param resourceName The new global JNDI name     */    public void setResourceName(String resourceName) {        this.resourceName = resourceName;    }    /**     * Convenience method for parsing the users.xml file.     * <p/>     * <p>This method is synchronized so only one thread at a time     * can parse the users.xml file and create the <code>principal</code>     * instance variable.</p>     */    protected synchronized void parse() {        //reset the principals cache                this.principals = new HashMap();        try {            UserFactory.getInstance().reload();            UserFactory factory = UserFactory.getInstance();            this.log.debug("Reloaded the users.xml file into memory");            Map map = factory.getUsers();            this.log.debug("Loaded " + map.size() + " users into memory");            Iterator iterator = map.keySet().iterator();            while (iterator.hasNext()) {                String key = (String) iterator.next();                OpenNMSPrincipal principal = new OpenNMSPrincipal((User) map.get(key));                this.principals.put(key, principal);            }            this.log.debug("Loaded the regular users into the principal cache");        } catch (MarshalException e) {            this.log.error("Could not parse the users.xml file", e);        } catch (ValidationException e) {            this.log.error("Could not parse the users.xml file", e);        } catch (FileNotFoundException e) {            this.log.error("Could not find the users.xml file", e);        } catch (Exception e) {            this.log.error("Unexpected exception parsing users.xml file", e);        }        try {            //load the "magic" users            Map[] maps = this.parseMagicUsers();            Map magicUserToPasswordMapping = maps[0];            this.magicRoleMapping = maps[1];            this.log.debug("Loaded the magic user config file");            Iterator iterator = magicUserToPasswordMapping.keySet().iterator();            while (iterator.hasNext()) {                String name = (String) iterator.next();                String password = (String) magicUserToPasswordMapping.get(name);                User magicUser = new User();                magicUser.setUserId(name);                magicUser.setPassword(UserFactory.getInstance().encryptedPassword(password));                this.principals.put(name, new OpenNMSPrincipal(magicUser));            }            this.log.debug("Loaded the magic users into the principal cache");            this.magicUsersLastModified = this.magicUsersFile.lastModified();            this.log.debug("Updated the magic user file last modified time stamp to " + this.magicUsersLastModified);        } catch (FileNotFoundException e) {            this.log.error("Could not find the magic users file", e);        } catch (IOException e) {            this.log.error("Could not read the magic users file", e);        } catch (Exception e) {            this.log.error("Unexpected exception parsing users.xml file", e);        }    }    /**     * Return the Container with which this Realm has been associated.     */    public Container getContainer() {        return (this.container);    }    /**     * Set the Container with which this Realm has been associated.     *     * @param container The associated Container     */    public void setContainer(Container container) {        this.container = container;        this.log.debug("Initialized with container: " + this.container.getName() + " (" + this.container.getInfo() + ")");    }    /**     * Return descriptive information about this Realm implementation and     * the corresponding version number, in the format     * <code>&lt;description&gt;/&lt;version&gt;</code>.     */    public String getInfo() {        return (this.info);    }    /**     * Return the Principal associated with the specified username and     * credentials, if there is one; otherwise return <code>null</code>.     *     * @param username    Username of the Principal to look up     * @param credentials Password or other credentials to use in

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?