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

📄 somauserloginmodule.java

📁 一个agent 工具包,可以开发移动设备应用,考虑了安全措施
💻 JAVA
字号:
package SOMA.security.auth;

import java.util.*;
import java.io.*;

import javax.security.auth.*;
import javax.security.auth.callback.*;
import javax.security.auth.login.*;
import javax.security.auth.spi.*;

import SOMA.security.infrastructure.*;

import com.entrust.security.exceptions.*;
import iaik.x509.X509Certificate;

/*
  Modulo di login.

  Verifico profileName e password e nel caso
  siano corretti associo i ruoli scelti dall'utente al soggetto da creare.

  L'implementazione e' quella standard di un modulo di login del JAAS

*/

public class SomaUserLoginModule implements LoginModule {

  // initial state
  private Subject subject;
  private CallbackHandler callbackHandler;
  private Map sharedState;
  private Map options;

  // configurable option
  private boolean debug = false;

  // the authentication status
  private boolean succeeded = false;
  private boolean commitSucceeded = false;

  //  Informazioni persistenti
  private String profileName;
  private String profilePassword;
  private X509Certificate identityCert;
  private UserPrincipal userPrincipal;
  private RolePrincipal rolePrincipal;
  private Role role;

  // Informazioni Temporanee
  private Infrastructure pki;
  private char[] password;
  private String[] roleNames;
  private String selectedRoleName;
  private Role[] roles;
  private ProfileManager userProfileManager;

  public void initialize(Subject subject, CallbackHandler callbackHandler,Map sharedState, Map options) {

    this.subject = subject;
    this.callbackHandler = callbackHandler;
    this.sharedState = sharedState;
    this.options = options;

    // initialize any configured options
    debug = "true".equalsIgnoreCase((String)options.get("debug"));

    // Inizializzo l'infrastruttura
    InfrastructureAddress pkiAddress=new InfrastructureAddress(NetAddress.CA_IP,NetAddress.DIR_IP,true);
    try {
      this.pki=new Infrastructure(pkiAddress);
    } catch (Exception ex) {

      ex.printStackTrace();

    }

  }

  public boolean login() throws LoginException {

    // prompt for a profileName and password
    if (callbackHandler == null)
      throw new LoginException("Errore: nessun CallBackHandler per richiedere informazioni");

    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("Profile Name: ");
    callbacks[1] = new PasswordCallback("Password: ", false);

    try {

      callbackHandler.handle(callbacks);

      profileName = ((NameCallback)callbacks[0]).getName();
      char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
      if (tmpPassword == null) {
        // treat a NULL password as an empty password
        tmpPassword = new char[0];
      }
      password = new char[tmpPassword.length];
      System.arraycopy(tmpPassword, 0,password, 0, tmpPassword.length);
      ((PasswordCallback)callbacks[1]).clearPassword();

    } catch (java.io.IOException ioe) {

      throw new LoginException(ioe.toString());

    } catch (UnsupportedCallbackException uce) {

      throw new LoginException("Errore: " + uce.getCallback().toString() +
        "impossibile ottenere le informazioni di autenticazione.");
    }

    // print debugging information
    if (debug) {
      System.out.println("\t\t[SomaUserLoginModule] " +
        "user entered profileName: " +
        profileName);
      System.out.print("\t\t[SomaUserLoginModule] " +
        "user entered password: ");
      for (int i = 0; i < password.length; i++)
        System.out.print(password[i]);
      System.out.println();
    }

    // verify the profileName/password

    if (verifyUserInfo(profileName,password)) {

      // authentication succeeded!!!
      if (debug) {
        System.out.println("\t\t[SomaUserLoginModule] authentication succeeded");
        System.out.println(identityCert.getSubjectDN().getName());
      }

    } else {

      // authentication failed -- clean out state
      if (debug)
        System.out.println("\t\t[SomaUserLoginModule] " +
        "authentication failed");
      succeeded = false;
      profileName = null;
      for (int i = 0; i < password.length; i++)
        password[i] = ' ';
      password = null;
      throw new FailedLoginException("Login Incorrect");
    }

    // Chiedo i ruoli all'utente
    // per ora getUserRoles() torna dei valori fittizzi
    // con cui si possono comunque fare delle prove.
    // Successivamente getUserRoles dovr

⌨️ 快捷键说明

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