Example usage for javax.security.auth.login Configuration refresh

List of usage examples for javax.security.auth.login Configuration refresh

Introduction

In this page you can find the example usage for javax.security.auth.login Configuration refresh.

Prototype

public void refresh() 

Source Link

Document

Refresh and reload the Configuration.

Usage

From source file:it.cnr.icar.eric.client.xml.registry.jaas.LoginModuleManager.java

private AppConfigurationEntry[] getReloadedAppConfigurationEntries(Configuration config, String cfgFileName,
        String cfgFileContents, String appConfigName) throws JAXRException {
    AppConfigurationEntry[] appConfigEntries = null;

    // if there is an IOException, we do not have permission to write
    // to the local filesystem.  Without this permission, we cannot
    // control the authentication.  In this case, throw new 
    // JAXRException to notify the user to give us permission
    try {/*from  w  w  w. j  a  v a 2 s .  c  o m*/
        File file = new File(cfgFileName);
        writeCfgFile(file, cfgFileContents, false);
    } catch (Throwable t) {
        log.error(t);
        throw new JAXRException(JAXRResourceBundle.getInstance()
                .getString("message.error.no.permission.wirte.local.filesystem"));
    }

    String javaSecLoginCfg = System.getProperty("java.security.auth.login.config");
    String userCfgFileName = getUserCfgFileName();
    System.setProperty("java.security.auth.login.config", cfgFileName);
    config.refresh();
    appConfigEntries = config.getAppConfigurationEntry(appConfigName);

    try {
        deleteCfgFile(cfgFileName);
    } catch (Throwable t) {
        log.warn(JAXRResourceBundle.getInstance().getString("message.problemDeletingConfigFile"), t);
    } finally {
        if (javaSecLoginCfg != null) {
            System.setProperty("java.security.auth.login.config", javaSecLoginCfg);
        } else {
            System.setProperty("java.security.auth.login.config", userCfgFileName);
        }

        config.refresh();
    }

    return appConfigEntries;
}

From source file:it.cnr.icar.eric.client.xml.registry.jaas.LoginModuleManager.java

/**
 * This method is used to create the default login configuration file.
 * Currently, the default file is for the
 * com.sun.security.auth.module.KeystoreLoginModule
 *
 * @throws JAXRException//from   www .  jav a 2  s . com
 *  This is thrown if there is a problem writing the default login config
 *  file to the filesystem
 */
public void createDefaultLoginConfigFile() throws JAXRException {
    log.trace("start creation of default login config file");

    File keystoreFile = KeystoreUtil.getKeystoreFile();
    KeystoreUtil.canReadKeystoreFile(keystoreFile);

    // This property should always be set by java
    String userHomeFileName = System.getProperty("user.home");

    if ((userHomeFileName == null) || (userHomeFileName.length() == 0)) {
        throw new JAXRException(
                JAXRResourceBundle.getInstance().getString("message.error.not.find.system.property"));
    }

    File configFile;
    // Login config filename might be define as system property
    String configFileName = System.getProperty("java.security.auth.login.config");
    if (configFileName != null) {
        configFile = new File(configFileName);
    } else {
        configFile = new File(userHomeFileName, ".java.login.config");
    }

    if (configFile.exists()) {
        if (configFile.canRead()) {
            Configuration config = ConfigFile.getConfiguration();
            String appName = getApplicationName();
            AppConfigurationEntry[] defaultAppConfigEntries = getReloadedAppConfigurationEntries(config,
                    configFile.getPath() + ".tmp",
                    getDefaultConfigFileContents(DEFAULT_APPLICATION_NAME + ".tmp"), appName + ".tmp");
            AppConfigurationEntry[] userAppConfigEntries = config.getAppConfigurationEntry(appName);

            //TODO: Paul to verify this!! What if one of the Entries is null??
            boolean isCorrect;
            if (defaultAppConfigEntries == null && userAppConfigEntries == null) {
                // this will happen when using constructor LoginModuleManager(String applicationName)
                // and not having an entry for 'applicationName' in .java.login.config
                isCorrect = true;
            } else if (defaultAppConfigEntries != null && userAppConfigEntries == null) {
                // force add default to existing cfg file
                isCorrect = false;
            } else {
                isCorrect = checkLoginModules(userAppConfigEntries, defaultAppConfigEntries);
            }

            // if the user has a login config file with the same app name
            // as the default, but the login modules are different, rename
            // the existing user login config file and write the default
            // config file in place of the existing
            if (!isCorrect) {
                String userCfgFileName = configFile.getPath();
                String userCfgFileContent = getUserCfgFileContents(userCfgFileName);
                log.warn(JAXRResourceBundle.getInstance()
                        .getString("message.UserLoginConfigFileDoesNotHaveTheSameLoginModulesAsTheDefault"));
                renameCfgFile(userCfgFileName, userCfgFileName + ".bak");
                writeCfgFile(configFile, userCfgFileContent + LINE_SEPARATOR + getDefaultConfigFileContents(),
                        false);
                config.refresh();
                log.info(JAXRResourceBundle.getInstance().getString("message.createdNewLoginConfigFile",
                        new Object[] { configFile.getName() }));
            } else {
                log.info(JAXRResourceBundle.getInstance().getString("message.usingExistingConfigFile",
                        new Object[] { configFile.getName() }));

                return;
            }
        } else {
            throw new JAXRException(JAXRResourceBundle.getInstance().getString(
                    "message.error.file.not.readable", new Object[] { configFile.getAbsolutePath() }));
        }
    } else {
        writeCfgFile(configFile, getDefaultConfigFileContents(), false);
        log.info(JAXRResourceBundle.getInstance().getString("message.createdNewLoginConfigFile",
                new Object[] { configFile.getName() }));
    }

    log.trace("finish creation of default login config file");
}