dbusermanager.java
来自「JAVA FTP 上传下载 的源文件」· Java 代码 · 共 698 行 · 第 1/2 页
JAVA
698 行
String password = null; if(user.getPassword() != null) { // password provided, encrypt it and store the encrypted value password= getPasswordEncryptor().encrypt(user.getPassword()); } else { // password was not provided, either load from the existing user and store that again // or store as null ResultSet rs = null; try { User userWithPassword = selectUserByName(user.getName()); if(userWithPassword != null) { // user exists, reuse password password = userWithPassword.getPassword(); } } finally { closeQuitely(rs); } } map.put(ATTR_PASSWORD, escapeString(password)); String home = user.getHomeDirectory(); if (home == null) { home = "/"; } map.put(ATTR_HOME, escapeString(home)); map.put(ATTR_ENABLE, String.valueOf(user.getEnabled())); map.put(ATTR_WRITE_PERM, String.valueOf(user .authorize(new WriteRequest()) != null)); map.put(ATTR_MAX_IDLE_TIME, user.getMaxIdleTime()); TransferRateRequest transferRateRequest = new TransferRateRequest(); transferRateRequest = (TransferRateRequest) user .authorize(transferRateRequest); if (transferRateRequest != null) { map.put(ATTR_MAX_UPLOAD_RATE, transferRateRequest .getMaxUploadRate()); map.put(ATTR_MAX_DOWNLOAD_RATE, transferRateRequest .getMaxDownloadRate()); } else { map.put(ATTR_MAX_UPLOAD_RATE, 0); map.put(ATTR_MAX_DOWNLOAD_RATE, 0); } // request that always will succeed ConcurrentLoginRequest concurrentLoginRequest = new ConcurrentLoginRequest( 0, 0); concurrentLoginRequest = (ConcurrentLoginRequest) user .authorize(concurrentLoginRequest); if (concurrentLoginRequest != null) { map.put(ATTR_MAX_LOGIN_NUMBER, concurrentLoginRequest .getMaxConcurrentLogins()); map.put(ATTR_MAX_LOGIN_PER_IP, concurrentLoginRequest .getMaxConcurrentLoginsPerIP()); } else { map.put(ATTR_MAX_LOGIN_NUMBER, 0); map.put(ATTR_MAX_LOGIN_PER_IP, 0); } String sql = null; if (!doesExist(user.getName())) { sql = StringUtils.replaceString(insertUserStmt, map); } else { sql = StringUtils.replaceString(updateUserStmt, map); } LOG.info(sql); // execute query stmt = createConnection().createStatement(); stmt.executeUpdate(sql); } catch (SQLException ex) { LOG.error("DbUserManager.save()", ex); throw new FtpException("DbUserManager.save()", ex); } finally { closeQuitely(stmt); } } private void closeQuitely(Statement stmt) { if(stmt != null) { Connection con = null; try { con = stmt.getConnection(); } catch (Exception e) { } try { stmt.close(); } catch (SQLException e) { // ignore } closeQuitely(con); } } private void closeQuitely(ResultSet rs) { if(rs != null) { try { rs.close(); } catch (SQLException e) { // ignore } } } protected void closeQuitely(Connection con) { if (con != null) { try { con.close(); } catch (SQLException e) { // ignore } } } private BaseUser selectUserByName(String name) throws SQLException { // create sql query HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(name)); String sql = StringUtils.replaceString(selectUserStmt, map); LOG.info(sql); Statement stmt = null; ResultSet rs = null; try { // execute query stmt = createConnection().createStatement(); rs = stmt.executeQuery(sql); // populate user object BaseUser thisUser = null; if (rs.next()) { thisUser = new BaseUser(); thisUser.setName(rs.getString(ATTR_LOGIN)); thisUser.setPassword(rs.getString(ATTR_PASSWORD)); thisUser.setHomeDirectory(rs.getString(ATTR_HOME)); thisUser.setEnabled(rs.getBoolean(ATTR_ENABLE)); thisUser.setMaxIdleTime(rs.getInt(ATTR_MAX_IDLE_TIME)); List<Authority> authorities = new ArrayList<Authority>(); if (rs.getBoolean(ATTR_WRITE_PERM)) { authorities.add(new WritePermission()); } authorities.add(new ConcurrentLoginPermission(rs .getInt(ATTR_MAX_LOGIN_NUMBER), rs .getInt(ATTR_MAX_LOGIN_PER_IP))); authorities.add(new TransferRatePermission(rs .getInt(ATTR_MAX_DOWNLOAD_RATE), rs .getInt(ATTR_MAX_UPLOAD_RATE))); thisUser.setAuthorities(authorities); } return thisUser; } finally { closeQuitely(rs); closeQuitely(stmt); } } /** * Get the user object. Fetch the row from the table. */ public User getUserByName(String name) throws FtpException { Statement stmt = null; ResultSet rs = null; try { BaseUser user = selectUserByName(name); if(user != null) { // reset the password, not to be sent to API users user.setPassword(null); } return user; } catch (SQLException ex) { LOG.error("DbUserManager.getUserByName()", ex); throw new FtpException("DbUserManager.getUserByName()", ex); } finally { closeQuitely(rs); closeQuitely(stmt); } } /** * User existance check. */ public boolean doesExist(String name) throws FtpException { Statement stmt = null; ResultSet rs = null; try { // create the sql HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(name)); String sql = StringUtils.replaceString(selectUserStmt, map); LOG.info(sql); // execute query stmt = createConnection().createStatement(); rs = stmt.executeQuery(sql); return rs.next(); } catch (SQLException ex) { LOG.error("DbUserManager.doesExist()", ex); throw new FtpException("DbUserManager.doesExist()", ex); } finally { closeQuitely(rs); closeQuitely(stmt); } } /** * Get all user names from the database. */ public String[] getAllUserNames() throws FtpException { Statement stmt = null; ResultSet rs = null; try { // create sql query String sql = selectAllStmt; LOG.info(sql); // execute query stmt = createConnection().createStatement(); rs = stmt.executeQuery(sql); // populate list ArrayList<String> names = new ArrayList<String>(); while (rs.next()) { names.add(rs.getString(ATTR_LOGIN)); } return names.toArray(new String[0]); } catch (SQLException ex) { LOG.error("DbUserManager.getAllUserNames()", ex); throw new FtpException("DbUserManager.getAllUserNames()", ex); } finally { closeQuitely(rs); closeQuitely(stmt); } } /** * User authentication. */ public User authenticate(Authentication authentication) throws AuthenticationFailedException { if (authentication instanceof UsernamePasswordAuthentication) { UsernamePasswordAuthentication upauth = (UsernamePasswordAuthentication) authentication; String user = upauth.getUsername(); String password = upauth.getPassword(); if (user == null) { throw new AuthenticationFailedException("Authentication failed"); } if (password == null) { password = ""; } Statement stmt = null; ResultSet rs = null; try { // create the sql query HashMap<String, Object> map = new HashMap<String, Object>(); map.put(ATTR_LOGIN, escapeString(user)); String sql = StringUtils.replaceString(authenticateStmt, map); LOG.info(sql); // execute query stmt = createConnection().createStatement(); rs = stmt.executeQuery(sql); if (rs.next()) { try { String storedPassword = rs.getString(ATTR_PASSWORD); if (getPasswordEncryptor().matches(password, storedPassword)) { return getUserByName(user); } else { throw new AuthenticationFailedException( "Authentication failed"); } } catch (FtpException e) { throw new AuthenticationFailedException( "Authentication failed", e); } } else { throw new AuthenticationFailedException( "Authentication failed"); } } catch (SQLException ex) { LOG.error("DbUserManager.authenticate()", ex); throw new AuthenticationFailedException( "Authentication failed", ex); } finally { closeQuitely(rs); closeQuitely(stmt); } } else if (authentication instanceof AnonymousAuthentication) { try { if (doesExist("anonymous")) { return getUserByName("anonymous"); } else { throw new AuthenticationFailedException( "Authentication failed"); } } catch (AuthenticationFailedException e) { throw e; } catch (FtpException e) { throw new AuthenticationFailedException( "Authentication failed", e); } } else { throw new IllegalArgumentException( "Authentication not supported by this user manager"); } } /** * Escape string to be embedded in SQL statement. */ private String escapeString(String input) { if (input == null) { return input; } StringBuffer valBuf = new StringBuffer(input); for (int i = 0; i < valBuf.length(); i++) { char ch = valBuf.charAt(i); if (ch == '\'' || ch == '\\' || ch == '$' || ch == '^' || ch == '[' || ch == ']' || ch == '{' || ch == '}') { valBuf.insert(i, '\\'); i++; } } return valBuf.toString(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?