📄 fileloginmodule.java
字号:
logger.debug("login", "Acquiring password"); } // attempt the authentication using the supplied username and password try { attemptAuthentication(false); // authentication succeeded succeeded = true; if (logger.debugOn()) { logger.debug("login", "Authentication has succeeded"); } return true; } catch (LoginException le) { cleanState(); logger.debug("login", "Authentication has failed"); throw le; } } /** * Complete user authentication (Authentication Phase 2). * * <p> This method is called if the LoginContext's * overall authentication has succeeded * (all the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL * LoginModules have succeeded). * * <p> If this LoginModule's own authentication attempt * succeeded (checked by retrieving the private state saved by the * <code>login</code> method), then this method associates a * <code>JMXPrincipal</code> with the <code>Subject</code> located in the * <code>LoginModule</code>. If this LoginModule's own * authentication attempted failed, then this method removes * any state that was originally saved. * * @exception LoginException if the commit fails * @return true if this LoginModule's own login and commit * attempts succeeded, or false otherwise. */ public boolean commit() throws LoginException { if (succeeded == false) { return false; } else { if (subject.isReadOnly()) { cleanState(); throw new LoginException("Subject is read-only"); } // add Principals to the Subject if (!subject.getPrincipals().contains(user)) { subject.getPrincipals().add(user); } if (logger.debugOn()) { logger.debug("commit", "Authentication has completed successfully"); } } // in any case, clean out state cleanState(); commitSucceeded = true; return true; } /** * Abort user authentication (Authentication Phase 2). * * <p> This method is called if the LoginContext's overall authentication * failed (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL * LoginModules did not succeed). * * <p> If this LoginModule's own authentication attempt * succeeded (checked by retrieving the private state saved by the * <code>login</code> and <code>commit</code> methods), * then this method cleans up any state that was originally saved. * * @exception LoginException if the abort fails. * @return false if this LoginModule's own login and/or commit attempts * failed, and true otherwise. */ public boolean abort() throws LoginException { if (logger.debugOn()) { logger.debug("abort", "Authentication has not completed successfully"); } if (succeeded == false) { return false; } else if (succeeded == true && commitSucceeded == false) { // Clean out state succeeded = false; cleanState(); user = null; } else { // overall authentication succeeded and commit succeeded, // but someone else's commit failed logout(); } return true; } /** * Logout a user. * * <p> This method removes the Principals * that were added by the <code>commit</code> method. * * @exception LoginException if the logout fails. * @return true in all cases since this <code>LoginModule</code> * should not be ignored. */ public boolean logout() throws LoginException { if (subject.isReadOnly()) { cleanState(); throw new LoginException ("Subject is read-only"); } subject.getPrincipals().remove(user); // clean out state cleanState(); succeeded = false; commitSucceeded = false; user = null; if (logger.debugOn()) { logger.debug("logout", "Subject is being logged out"); } return true; } /** * Attempt authentication * * @param usePasswdFromSharedState a flag to tell this method whether * to retrieve the password from the sharedState. */ private void attemptAuthentication(boolean usePasswdFromSharedState) throws LoginException { // get the username and password getUsernamePassword(usePasswdFromSharedState); String localPassword = null; // userCredentials is initialized in login() if (((localPassword = userCredentials.getProperty(username)) == null) || (! localPassword.equals(new String(password)))) { // username not found or passwords do not match if (logger.debugOn()) { logger.debug("login", "Invalid username or password"); } throw new FailedLoginException("Invalid username or password"); } // Save the username and password in the shared state // only if authentication succeeded if (storePass && !sharedState.containsKey(USERNAME_KEY) && !sharedState.containsKey(PASSWORD_KEY)) { sharedState.put(USERNAME_KEY, username); sharedState.put(PASSWORD_KEY, password); } // Create a new user principal user = new JMXPrincipal(username); if (logger.debugOn()) { logger.debug("login", "User '" + username + "' successfully validated"); } } /* * Read the password file. */ private void loadPasswordFile() throws IOException { FileInputStream fis; try { fis = new FileInputStream(passwordFile); } catch (SecurityException e) { if (userSuppliedPasswordFile || hasJavaHomePermission) { throw e; } else { FilePermission fp = new FilePermission(passwordFileDisplayName, "read"); AccessControlException ace = new AccessControlException( "access denied " + fp.toString()); ace.setStackTrace(e.getStackTrace()); throw ace; } } BufferedInputStream bis = new BufferedInputStream(fis); userCredentials = new Properties(); userCredentials.load(bis); bis.close(); } /** * Get the username and password. * This method does not return any value. * Instead, it sets global name and password variables. * * <p> Also note that this method will set the username and password * values in the shared state in case subsequent LoginModules * want to use them via use/tryFirstPass. * * @param usePasswdFromSharedState boolean that tells this method whether * to retrieve the password from the sharedState. */ private void getUsernamePassword(boolean usePasswdFromSharedState) throws LoginException { if (usePasswdFromSharedState) { // use the password saved by the first module in the stack username = (String)sharedState.get(USERNAME_KEY); password = (char[])sharedState.get(PASSWORD_KEY); return; } // acquire username and password if (callbackHandler == null) throw new LoginException("Error: no CallbackHandler available " + "to garner authentication information from the user"); Callback[] callbacks = new Callback[2]; callbacks[0] = new NameCallback("username"); callbacks[1] = new PasswordCallback("password", false); try { callbackHandler.handle(callbacks); username = ((NameCallback)callbacks[0]).getName(); char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword(); password = new char[tmpPassword.length]; System.arraycopy(tmpPassword, 0, password, 0, tmpPassword.length); ((PasswordCallback)callbacks[1]).clearPassword(); } catch (IOException ioe) { LoginException le = new LoginException(ioe.toString()); throw (LoginException) EnvHelp.initCause(le, ioe); } catch (UnsupportedCallbackException uce) { LoginException le = new LoginException( "Error: " + uce.getCallback().toString() + " not available to garner authentication " + "information from the user"); throw (LoginException) EnvHelp.initCause(le, uce); } } /** * Clean out state because of a failed authentication attempt */ private void cleanState() { username = null; if (password != null) { Arrays.fill(password, ' '); password = null; } if (clearPass) { sharedState.remove(USERNAME_KEY); sharedState.remove(PASSWORD_KEY); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -