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

📄 checkpassword.java

📁 21天学通java的示例程序源代码
💻 JAVA
字号:
/* * CheckPassword.java */package com.wrox.password;import java.io.*;import java.net.*;import java.security.*;/** * Checks passwords from an unencrypted password file. For demonstration * purposes only - this is not production quality code! */public class CheckPassword {  /**   * Checks a password. This method uses a doPrivileged block, so there   * is no need for the caller to have permission to read the password   * file.   * @param username The user to check the password for   * @param password The password   * @param passwordLocation The password file URL to use when checking   * @return <code>true</code> if the password is correct   */  public boolean check(final String username, final String password,                        final String passwordLocation) {    // First verify that the callers have permission to check passwords    SecurityManager security = System.getSecurityManager();    if (security != null) {      security.checkPermission(new CheckPasswordPermission());    }     // Then check the password in a privileged code block    Boolean passwordOk =       (Boolean) AccessController.doPrivileged(new PrivilegedAction() {      public Object run() {        // start of privileged code        try {          boolean ok = privilegedCheck(username, password,                                        passwordLocation);          return new Boolean(ok);        } catch (IOException e) {          return Boolean.FALSE;        }         // end of privileged code      }     });    return passwordOk.booleanValue();  }   /**   * Checks a password. The caller needs to have permission to read the   * password file.   * @param username The user to check the password for   * @param password The password   * @param passwordLocation The password file URL to use when checking   * @return <code>true</code> if the password is correct   * @throws IOException if the password URL could not be read   */  public boolean privilegedCheck(String username, String password,                                  String passwordLocation) throws IOException {    String toSearch = username + ":";    String passwordFound = null;    URL passwordUrl = new URL(passwordLocation);    BufferedReader passwords =       new BufferedReader(new InputStreamReader(passwordUrl.openStream()));    try {      while (true) {        String line = passwords.readLine();        if (line == null) {          break;        }         if (line.startsWith(toSearch)) {          passwordFound = line.substring(toSearch.length());          break;        }       }     }     finally {      passwords.close();    }     return passwordFound != null && password.equals(passwordFound);  } }

⌨️ 快捷键说明

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