Java tutorial
/* * 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 de.codecentric.multitool.ftp; import java.util.ArrayList; import java.util.List; import org.apache.commons.lang.StringUtils; 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.PasswordEncryptor; import org.apache.ftpserver.usermanager.UsernamePasswordAuthentication; import org.apache.ftpserver.usermanager.impl.AbstractUserManager; import org.apache.ftpserver.usermanager.impl.BaseUser; import org.apache.ftpserver.usermanager.impl.ConcurrentLoginPermission; import org.apache.ftpserver.usermanager.impl.WritePermission; /** * Einfache Benutzerverwaltung fr den FTP-Server, die nur einen Benutzer kennt. * * @author carsten.mjartan */ class SimpleUserManager extends AbstractUserManager { private final BaseUser user; SimpleUserManager(String adminName, PasswordEncryptor passwordEncryptor, String username, String password, String homeDirectory, boolean canWrite) { super(adminName, passwordEncryptor); user = createUser(username, password, homeDirectory, canWrite); } private BaseUser createUser(String username, String password, String homeDirectory, boolean canWrite) { BaseUser user = new BaseUser(); user.setName(username); user.setPassword(getPasswordEncryptor().encrypt(password)); user.setEnabled(true); user.setHomeDirectory(homeDirectory); List<Authority> authorities = new ArrayList<Authority>(); authorities.add(new ConcurrentLoginPermission(1, 1)); if (canWrite) { authorities.add(new WritePermission()); } user.setAuthorities(authorities); return user; } /** * {@inheritDoc} */ public User authenticate(Authentication auth) throws AuthenticationFailedException { if (auth instanceof UsernamePasswordAuthentication) { UsernamePasswordAuthentication authentication = (UsernamePasswordAuthentication) auth; if (StringUtils.equals(authentication.getUsername(), user.getName()) && getPasswordEncryptor().matches(authentication.getPassword(), user.getPassword())) { return user; } } throw new AuthenticationFailedException("Authentication credentials did not match"); } /** * {@inheritDoc} */ public void delete(String username) throws FtpException { throw new FtpException("Cannot delete user '" + username + "'"); } /** * {@inheritDoc} */ public boolean doesExist(String username) throws FtpException { return user.getName().equals(username); } /** * {@inheritDoc} */ public String[] getAllUserNames() throws FtpException { return new String[] { user.getName() }; } /** * {@inheritDoc} */ public User getUserByName(String username) throws FtpException { return user.getName().equals(username) ? null : user; } /** * {@inheritDoc} */ public void save(User user) throws FtpException { throw new FtpException("Cannot save user '" + user.getName() + "'"); } }