⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 configprincipal.java

📁 Java 3D Desktop Environment旨在使用Java 3D来创建一个3D桌面环境。功能包括:分布式的应用程序
💻 JAVA
字号:
/* * Copyright (c) 2000, Niklas Mehner  * All rights reserved. *  * Redistribution and use in source and binary forms, with or without  * modification, are permitted provided that the following conditions  * are met: *  *   - Redistributions of source code must retain the above copyright  *     notice, this list of conditions and the following disclaimer. *  *   - Redistributions in binary form must reproduce the above copyright  *     notice, this list of conditions and the following disclaimer in  *     the documentation and/or other materials provided with the  *     distribution. *  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  */ package org.j3de.security;                       
                                 
import java.io.File;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.AccessController;import java.security.Principal;
import java.security.PrivilegedAction;import java.security.Key;      
import java.security.KeyFactory;
import java.security.NoSuchProviderException;
import java.security.NoSuchAlgorithmException;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.util.Map;
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.CipherInputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.DESedeKeySpec;
 
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

import org.j3de.exception.ExceptionHandler;import org.j3de.util.Configuration;import org.j3de.util.ConfigHelper;/**  * The ConfigPrincipal is a principal containing the configuration of the user environemnt.  * The configuration is stored in a TripleDES encrypted xml-File named <code> username.cxml </code>   * and located in the userdir.  * When a new Principal is created, the user is looked up in the userlist, that is read from the   * configFile provided by UserConfig in the userdir. If this is successfull the user configuration is   * decoded using the users password as key. A wrong password or unknown user will result in   * a IOException beeing thrown.  *  * @author Niklas Mehner  * @version $Revision: 1.4 $, $Date: 2000/09/08 20:50:57 $  * @since  j3de 1.0  * @see UserConfig  */public class ConfigPrincipal implements Principal { 
  private static final String USERFILE = "users.xml";    private String username;     
  private Configuration configuration;
  
  public ConfigPrincipal(String provider,
                         String userdir, 
                         String username, 
                         char[] password) throws ParserConfigurationException,
                                                 SAXException,
                                                 FileNotFoundException,
                                                 IOException,
                                                 NoSuchAlgorithmException,
                                                 NoSuchPaddingException,
                                                 NoSuchProviderException,
                                                 InvalidKeyException,
                                                 InvalidKeySpecException {
    this.username = username;      
    final UserConfig user = (UserConfig)getUserMap(userdir).get(username);        if (user != null) {
      Cipher cipher = Cipher.getInstance("TripleDES", provider);       
    
      byte[] keybytes = new byte[DESedeKeySpec.DES_EDE_KEY_LEN];   
      for (int i=0; i<keybytes.length; i++) 
        keybytes[i] = (byte)password[i % password.length];    
      
      //DESedeKeySpec keyspec  = new DESedeKeySpec(keybytes);    
      //KeyFactory keyfactory  = KeyFactory.getInstance("TripleDES");
      //Key key = keyfactory.generatePrivate(keyspec);
    
      cipher.init(Cipher.DECRYPT_MODE, new K("TripleDES", keybytes));
    
     /* TODO: Uncomment to use encryption of user configuration, currently passwords are ignored :(      CipherInputStream cip = new CipherInputStream(new FileInputStream(user.getConfigFile()), cipher);   
      configuration = new Configuration(new InputStreamReader(cip));
*/            configuration = (Configuration)AccessController.doPrivileged(new PrivilegedAction() {        public Object run() {             try {            FileInputStream fis = new FileInputStream(user.getConfigFile());               return new Configuration(new InputStreamReader(fis));          } catch (Exception e) {            ExceptionHandler.handleException(e);            return null;          }        }      });            if (configuration == null)        throw new IOException("Configuration could not be read");    } else       throw new IOException("Unknown User");  }
    private Map getUserMap(final String userDir)  {    return (Map)AccessController.doPrivileged(new PrivilegedAction() {      public Object run() {           try {          Configuration userConfig = new Configuration(userDir + File.separator + USERFILE);          ConfigHelper helper = userConfig.getHelper();           return helper.getComponentMap("users", this.getClass().getClassLoader());         } catch (Exception e) {          ExceptionHandler.handleException(e);            return null;        }       }    });  }      /**    * Returns the username, this principal belongs to.   * @return the username.   */ 
  public String getName() {
    return username;      
  }  
    /**    * Returns the environment configuration for the user this principal belongs to.   * @return the configuration.   */ 
  public Configuration getConfiguration() {
    return configuration;
  }

  public void saveConfiguration(String provider,
                                char[] password) {
  }
  
  public boolean equals(Object o) {
    if (o == null)
      return false;

    if (this == o)
      return true;

    if (!(o instanceof ConfigPrincipal))
      return false;
    
    ConfigPrincipal that = (ConfigPrincipal)o;

    if (this.getName().equals(that.getName()))
        return true;
        
    return false;
  }
  
  private class K implements Key  {
    private final byte[] keyBytes;
    private final String alg;
        
    K(String alg, byte[] keyBytes) {
      this.alg      = alg;
      this.keyBytes = keyBytes;
    }
        
    public String getAlgorithm() { return alg;  }
    public String getFormat()    { return "RAW"; }
    public byte[] getEncoded()   { return (byte[])keyBytes.clone(); }
  }
}

⌨️ 快捷键说明

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