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

📄 defaultsystemsecurity.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.util.List;     
import java.util.HashMap;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.Provider;
import java.security.Security;

import javax.security.auth.Subject;
import javax.security.auth.callback.CallbackHandler; 
import javax.security.auth.login.LoginException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.Configuration;
import javax.security.auth.login.AppConfigurationEntry;
import javax.security.auth.spi.LoginModule;
 
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node; 
import org.w3c.dom.Document;
import org.w3c.dom.Element; 

import org.j3de.exception.ExceptionHandler;
import org.j3de.util.ConfigHelper;
import org.j3de.util.ConfigurationException;
import org.j3de.util.EntryFactory;
import org.j3de.util.AbstractComponent;

public class DefaultSystemSecurity extends AbstractComponent implements SystemSecurity {   
  private final static String MODULE_LIST     = "login_modules";
  private final static String CRYPTO_PROVIDER = "crypto_provider";
  private static int loginNumber =  0;
       
  public UserInfo login(final CallbackHandler callbackhandler) {    
    Subject subject; 
      
    int i;
    for (i = 0; i < 3; i++) {
	    	    synchronized(this) {	      loginNumber++;	    }	    
      try {
        LoginContext lc = (LoginContext)AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {   
            try {
              return new LoginContext(new Integer(loginNumber).toString(), new Subject(), callbackhandler);
            } catch (LoginException e) {
              ExceptionHandler.handleException(e); 
              return null;
            } 
          }
        }); 
        
        lc.login();  
        subject = lc.getSubject();
        return new UserInfo(subject, null, lc);
         
      } catch (LoginException e) {
        ExceptionHandler.handleException(e);
      } 
      
    }                 
    
    return null;
  }

  public void logout(UserInfo userInfo) {     
    try {
      userInfo.getLoginContext().logout();
    } catch (LoginException e) {
      ExceptionHandler.handleException(e);
    } 
  }
   
  
  public void configure(Node node, Document nodeFactory) throws ConfigurationException  {                          
    super.configure(node, nodeFactory);
    
    final ConfigHelper helper = this.helper;
 
    ConfigurationException e = 
      (ConfigurationException)AccessController.doPrivileged(new PrivilegedAction() {
          public Object run() {
            try {
              Provider provider = (Provider)helper.getComponent("provider");
              Security.addProvider(provider);
              return null;
            } catch (ConfigurationException exc) {
              return exc;
            }
          }
      });
    
    if (e != null)
      throw e;

    final List moduleList = helper.getList(
      MODULE_LIST,

      new EntryFactory() {

        public Object createEntry(Node node) { 
          HashMap attrs = new HashMap(); 
          NamedNodeMap attributes = node.getAttributes();                  
          String loginModuleName = attributes.getNamedItem("name").getNodeValue();
          
          for (int i=0; i<attributes.getLength(); i++) {   
            Node item = attributes.item(i);
            String name  = item.getNodeName();
            String value = item.getNodeValue();
            if ((name != null) && (value != null))
              attrs.put(name, value);
          }
          
          return new AppConfigurationEntry(loginModuleName,
                                           AppConfigurationEntry.LoginModuleControlFlag.REQUIRED,
                                           attrs);
        }

      }

    );
                                                      
    AccessController.doPrivileged(new PrivilegedAction() {
        public Object run() {
          javax.security.auth.login.Configuration.setConfiguration(new LoginConfiguration(moduleList));  
          return null;
        }
      });
  }
                                             
  private class LoginConfiguration extends javax.security.auth.login.Configuration {
    AppConfigurationEntry[] entries;
    
    public LoginConfiguration(List modules) {               
      entries = new AppConfigurationEntry[modules.size()];
      for (int i=0; i<entries.length; i++)
        entries[i] = (AppConfigurationEntry)modules.get(i); 
    }
    
    public AppConfigurationEntry[] getAppConfigurationEntry(String applicationName) {
      return entries;
    }    
    
    public void refresh() {
    }
  } 
}

⌨️ 快捷键说明

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