📄 usermanagerimpl.java
字号:
package com.cib.service.impl;import org.springframework.security.providers.dao.DaoAuthenticationProvider;import org.springframework.security.providers.dao.SaltSource;import org.springframework.security.providers.encoding.PasswordEncoder;import org.springframework.security.userdetails.UsernameNotFoundException;import com.cib.dao.UserDao;import com.cib.model.User;import com.cib.service.UserExistsException;import com.cib.service.UserManager;import com.cib.service.UserService;import org.springframework.beans.factory.annotation.Required;import org.springframework.dao.DataIntegrityViolationException;import javax.jws.WebService;import javax.persistence.EntityExistsException;import java.util.List;/** * Implementation of UserManager interface. * * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a> */@WebService(serviceName = "UserService", endpointInterface = "com.cib.service.UserService")public class UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { private UserDao dao; private PasswordEncoder passwordEncoder; /** * Set the Dao for communication with the data layer. * @param dao the UserDao that communicates with the database */ @Required public void setUserDao(UserDao dao) { this.dao = dao; } /** * Set the PasswordEncoder used to encrypt passwords. * @param passwordEncoder the PasswordEncoder implementation */ @Required public void setPasswordEncoder(PasswordEncoder passwordEncoder) { this.passwordEncoder = passwordEncoder; } /** * {@inheritDoc} */ public User getUser(String userId) { return dao.get(Long.valueOf(userId)); } /** * {@inheritDoc} */ public List<User> getUsers(User user) { return dao.getUsers(); } /** * {@inheritDoc} */ public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { // if new user, lowercase userId user.setUsername(user.getUsername().toLowerCase()); } // Get and prepare password management-related artifacts boolean passwordChanged = false; if (passwordEncoder != null) { // Check whether we have to encrypt (or re-encrypt) the password if (user.getVersion() == null) { // New user, always encrypt passwordChanged = true; } else { // Existing user, check password in DB String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } // If password was changed (or new user), encrypt it if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { // needed for JPA e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } } /** * {@inheritDoc} */ public void removeUser(String userId) { log.debug("removing user: " + userId); dao.remove(Long.valueOf(userId)); } /** * {@inheritDoc} * @param username the login name of the human * @return User the populated user object * @throws UsernameNotFoundException thrown when username not found */ public User getUserByUsername(String username) throws UsernameNotFoundException { return (User) dao.loadUserByUsername(username); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -