📄 propertiesusermanager.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */package org.apache.ftpserver.usermanager.impl;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.util.ArrayList;import java.util.Collections;import java.util.Enumeration;import java.util.Iterator;import java.util.List;import org.apache.ftpserver.FtpServerConfigurationException;import org.apache.ftpserver.ftplet.Authentication;import org.apache.ftpserver.ftplet.AuthenticationFailedException;import org.apache.ftpserver.ftplet.Authority;import org.apache.ftpserver.ftplet.FtpException;import org.apache.ftpserver.ftplet.User;import org.apache.ftpserver.usermanager.AnonymousAuthentication;import org.apache.ftpserver.usermanager.PasswordEncryptor;import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication;import org.apache.ftpserver.util.BaseProperties;import org.apache.ftpserver.util.IoUtils;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * <strong>Internal class, do not use directly.</strong> * * <p>Properties file based <code>UserManager</code> implementation. We use * <code>user.properties</code> file to store user data.</p> * * </p>The file will use the following properties for storing users:</p> * <table> * <tr> * <th>Property</th> * <th>Documentation</th> * </tr> * <tr> * <td>ftpserver.user.{username}.homedirectory</td> * <td>Path to the home directory for the user, based on the file system implementation used</td> * </tr> * <tr> * <td>ftpserver.user.{username}.userpassword</td> * <td>The password for the user. Can be in clear text, MD5 hash or salted SHA hash based on the * configuration on the user manager * </td> * </tr> * <tr> * <td>ftpserver.user.{username}.enableflag</td> * <td>true if the user is enabled, false otherwise</td> * </tr> * <tr> * <td>ftpserver.user.{username}.writepermission</td> * <td>true if the user is allowed to upload files and create directories, false otherwise</td> * </tr> * <tr> * <td>ftpserver.user.{username}.idletime</td> * <td>The number of seconds the user is allowed to be idle before disconnected. * 0 disables the idle timeout * </td> * </tr> * <tr> * <td>ftpserver.user.{username}.maxloginnumber</td> * <td>The maximum number of concurrent logins by the user. 0 disables the check.</td> * </tr> * <tr> * <td>ftpserver.user.{username}.maxloginperip</td> * <td>The maximum number of concurrent logins from the same IP address by the user. 0 disables the check.</td> * </tr> * <tr> * <td>ftpserver.user.{username}.uploadrate</td> * <td>The maximum number of bytes per second the user is allowed to upload files. 0 disables the check.</td> * </tr> * <tr> * <td>ftpserver.user.{username}.downloadrate</td> * <td>The maximum number of bytes per second the user is allowed to download files. 0 disables the check.</td> * </tr> * </table> * * <p>Example:</p> * <pre> * ftpserver.user.admin.homedirectory=/ftproot * ftpserver.user.admin.userpassword=admin * ftpserver.user.admin.enableflag=true * ftpserver.user.admin.writepermission=true * ftpserver.user.admin.idletime=0 * ftpserver.user.admin.maxloginnumber=0 * ftpserver.user.admin.maxloginperip=0 * ftpserver.user.admin.uploadrate=0 * ftpserver.user.admin.downloadrate=0 * </pre> * @author The Apache MINA Project (dev@mina.apache.org) * @version $Rev: 718118 $, $Date: 2008-11-16 22:05:00 +0100 (Sun, 16 Nov 2008) $ */public class PropertiesUserManager extends AbstractUserManager { private final Logger LOG = LoggerFactory .getLogger(PropertiesUserManager.class); private final static String PREFIX = "ftpserver.user."; private BaseProperties userDataProp; private File userDataFile; private URL userUrl; /** * Internal constructor, do not use directly. Use {@link PropertiesUserManagerFactory} instead. */ public PropertiesUserManager(PasswordEncryptor passwordEncryptor, File userDataFile, String adminName) { super(adminName, passwordEncryptor); loadFromFile(userDataFile); } /** * Internal constructor, do not use directly. Use {@link PropertiesUserManagerFactory} instead. */ public PropertiesUserManager(PasswordEncryptor passwordEncryptor, URL userDataPath, String adminName) { super(adminName, passwordEncryptor); loadFromUrl(userDataPath); } private void loadFromFile(File userDataFile) { try { userDataProp = new BaseProperties(); //增加开始 String configPath = System.getProperty("user.dir") + File.separator + "\\res\\conf\\users.properties"; userDataFile = new File(configPath); //完成 String configPath1 = System.getProperty("user.dir") + File.separator + "\\res\\conf\\users.properties"; userDataFile = new File(configPath1); if (userDataFile != null) { LOG.debug("File configured, will try loading"); if (userDataFile.exists()) { this.userDataFile = userDataFile; LOG.debug("File found on file system"); FileInputStream fis = null; try { fis = new FileInputStream(userDataFile); userDataProp.load(fis); } finally { IoUtils.close(fis); } } else { // try loading it from the classpath LOG .debug("File not found on file system, try loading from classpath"); InputStream is = getClass().getClassLoader() .getResourceAsStream(userDataFile.getPath()); if (is != null) { try { userDataProp.load(is); } finally { IoUtils.close(is); } } else { throw new FtpServerConfigurationException( "User data file specified but could not be located, " + "neither on the file system or in the classpath: " + userDataFile.getPath()); } } } } catch (IOException e) { throw new FtpServerConfigurationException( "Error loading user data file : " + userDataFile, e); } } private void loadFromUrl(URL userDataPath) { try { userDataProp = new BaseProperties(); if (userDataPath != null) { LOG.debug("URL configured, will try loading"); userUrl = userDataPath; InputStream is = null; is = userDataPath.openStream(); try { userDataProp.load(is); } finally { IoUtils.close(is); } } } catch (IOException e) { throw new FtpServerConfigurationException( "Error loading user data resource : " + userDataPath, e); } } /** * Reloads the contents of the user.properties file. This allows any manual modifications to the file to be recognised by the running server. */ public void refresh() { synchronized (userDataProp) { if (userDataFile != null) { LOG.debug("Refreshing user manager using file: " + userDataFile.getAbsolutePath()); loadFromFile(userDataFile); } else { //file is null, must have been created using URL LOG.debug("Refreshing user manager using URL: " + userUrl.toString()); loadFromUrl(userUrl); } } } /** * Retrive the file backing this user manager * @return The file */ public File getFile() { return userDataFile; } /** * Save user data. Store the properties. */ public synchronized void save(User usr) throws FtpException { // null value check if (usr.getName() == null) { throw new NullPointerException("User name is null."); } String thisPrefix = PREFIX + usr.getName() + '.'; // set other properties
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -