org.springframework.security.authentication.encoding.PasswordEncoder Java Examples

The following examples show how to use org.springframework.security.authentication.encoding.PasswordEncoder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleHashUtil.java    From Roothub with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
	// 初始化密码认证处理器
	PasswordEncoder passwordEncoder = new MyMessageDigestPasswordEncoder("md5");
	// 初始化认证服务
	UserDetailsService userDetailsService = new MyUserDetailsService();
	
	// 初始化认证提供者
	AuthenticationProvider provider = new MyAuthenticationProvider(userDetailsService, passwordEncoder);
	
	List<AuthenticationProvider> providers = new ArrayList<>();
	providers.add(provider);
	
	// 初始化认证管理器
	AuthenticationManager am = new MyAuthenticationManager(providers);
	MyUsernamePasswordAuthenticationFilter filter = new MyUsernamePasswordAuthenticationFilter("/login");
	filter.setAuthenticationManager(am);
	//filter.doFilter(req, res, chain);
	
}
 
Example #2
Source File: UserManagerImplTest.java    From ankush with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Password encryption test method for
 * {@link com.impetus.ankush.service.impl.UserManagerImpl#saveUser(com.impetus.ankush.common.domain.model.User)}
 * .
 * @throws UserExistsException 
 */
@Test
public void testSaveUserPasswordEncryptionUnchanged() throws UserExistsException {
	user.setVersion(1);
	user.setUsername(user.getUsername().toLowerCase());
	
	String encryptedPassword = "ENCRYPTED"+user.getPassword();
	user.setPassword(encryptedPassword);
	
	PasswordEncoder passwordEncoder = EasyMock.createStrictMock(PasswordEncoder.class);
	((UserManagerImpl)userManager).setPasswordEncoder(passwordEncoder);
	
	EasyMock.expect(userDao.getUserPassword(user.getUsername())).andReturn(encryptedPassword);
	EasyMock.expect(userDao.saveUser(user)).andReturn(user);
	EasyMock.replay(userDao, passwordEncoder);
	
	userManager.saveUser(user);
	assertEquals("password not encrypted", encryptedPassword, user.getPassword());
}
 
Example #3
Source File: UserManagerImplTest.java    From ankush with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Password encryption test method for
 * {@link com.impetus.ankush.service.impl.UserManagerImpl#saveUser(com.impetus.ankush.common.domain.model.User)}
 * .
 * @throws UserExistsException 
 */
@Test
public void testSaveUserPasswordEncryptionNew() throws UserExistsException {
	user.setVersion(1);
	user.setUsername(user.getUsername().toLowerCase());
	String encryptedPassword = "ENCRYPTED"+user.getPassword();
	
	PasswordEncoder passwordEncoder = EasyMock.createMock(PasswordEncoder.class);
	((UserManagerImpl)userManager).setPasswordEncoder(passwordEncoder);
	
	EasyMock.expect(passwordEncoder.encodePassword(user.getPassword(), null)).andReturn(encryptedPassword);
	
	EasyMock.expect(userDao.getUserPassword(user.getUsername())).andReturn(null);
	EasyMock.expect(userDao.saveUser(user)).andReturn(user);
	EasyMock.replay(userDao, passwordEncoder);
	
	userManager.saveUser(user);
	assertEquals("password not encrypted", encryptedPassword, user.getPassword());
}
 
Example #4
Source File: UserManagerImplTest.java    From ankush with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Password encryption test method for
 * {@link com.impetus.ankush.service.impl.UserManagerImpl#saveUser(com.impetus.ankush.common.domain.model.User)}
 * .
 * @throws UserExistsException 
 */
@Test
public void testSaveUserPasswordEncryptionChanged() throws UserExistsException {
	user.setVersion(1);
	user.setUsername(user.getUsername().toLowerCase());
	String encryptedPassword = "ENCRYPTED"+user.getPassword();
	
	PasswordEncoder passwordEncoder = EasyMock.createMock(PasswordEncoder.class);
	((UserManagerImpl)userManager).setPasswordEncoder(passwordEncoder);
	
	EasyMock.expect(passwordEncoder.encodePassword(user.getPassword(), null)).andReturn(encryptedPassword);
	
	EasyMock.expect(userDao.getUserPassword(user.getUsername())).andReturn(user.getPassword()+"Old");
	EasyMock.expect(userDao.saveUser(user)).andReturn(user);
	EasyMock.replay(userDao, passwordEncoder);
	
	userManager.saveUser(user);
	assertEquals("password not encrypted", encryptedPassword, user.getPassword());
}
 
Example #5
Source File: DefaultCalendarService.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@Autowired
public DefaultCalendarService(final EventDao eventDao,
                              final CalendarUserDao userDao,
                              final JdbcOperations jdbcOperations,
                              final PasswordEncoder passwordEncoder) {
    if (eventDao == null) {
        throw new IllegalArgumentException("eventDao cannot be null");
    }
    if (userDao == null) {
        throw new IllegalArgumentException("userDao cannot be null");
    }
    if (jdbcOperations == null) {
        throw new IllegalArgumentException("jdbcOperations cannot be null");
    }
    if (passwordEncoder == null) {
        throw new IllegalArgumentException("passwordEncoder cannot be null");
    }
    this.eventDao = eventDao;
    this.userDao = userDao;
    this.jdbcOperations = jdbcOperations;
    this.passwordEncoder = passwordEncoder;
}
 
Example #6
Source File: UserService.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
@Transactional(readOnly = false)
public User user_merge(User user) {
	//create save the password
	if (user.getId() == null) {
		user.setCreationDate(new Date());
		user.setLastUpdateDate(new Date());
		PasswordEncoder encoder = new ShaPasswordEncoder(256);
		user.setPassword(encoder.encodePassword(user.getPassword(), user.getSalt()));
		return userDAO.merge(user);
	}
	
	else
		//update do not update the password
		{	
		User dbUser = userDAO.findById(user.getId());
		dbUser.setLastUpdateDate(new Date());			
		dbUser.setLogin(user.getLogin());
		dbUser.setFirstName(user.getFirstName());
		dbUser.setDateOfBirth(user.getDateOfBirth());
		dbUser.setMiddleName(user.getMiddleName());
		dbUser.setLastName(user.getLastName());
		dbUser.setEmail(user.getEmail());
		dbUser.setEnabled(user.getEnabled());
		dbUser.setGroups(user.getGroups());
		dbUser.setDepartments(user.getDepartments());
		dbUser.setSurveyDefinitions(user.getSurveyDefinitions());
		return userDAO.merge(dbUser);	
	}
}
 
Example #7
Source File: UserService.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
@Transactional(readOnly = false)
public User user_updatePassword(User user, PasswordResetRequest passwordResetRequest) {
		//update the request
		passwordResetRequest.setResetDate(new Date());
		passwordResetRequestDAO.merge(passwordResetRequest);

		//update password
		User dbUser = userDAO.findById(user.getId());
		dbUser.setLastUpdateDate(new Date());
		PasswordEncoder encoder = new ShaPasswordEncoder(256);
		dbUser.setPassword(encoder.encodePassword(user.getPassword(), user.getSalt()));
		return userDAO.merge(dbUser);
}
 
Example #8
Source File: PasswordUtilsTest.java    From onboard with Apache License 2.0 5 votes vote down vote up
@Test
public void testOldPWMigrate() {
    PasswordEncoder encoder = new Md5PasswordEncoder();
    String oldPW = encoder.encodePassword(STRING, null).toUpperCase();
    String newPW = PasswordUtils.updateOldEncPass(oldPW, DATESTRING);
    assertTrue("Old PW should match", PasswordUtils.isPasswordValid(newPW, STRING, DATESTRING));
}
 
Example #9
Source File: UserService.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
@Transactional(readOnly = false)
public User user_updatePassword(User user) {
		User dbUser = userDAO.findById(user.getId());
		dbUser.setLastUpdateDate(new Date());
		PasswordEncoder encoder = new ShaPasswordEncoder(256);
		dbUser.setPassword(encoder.encodePassword(user.getPassword(), user.getSalt()));
		return userDAO.merge(dbUser);
}
 
Example #10
Source File: UserService.java    From JDeSurvey with GNU Affero General Public License v3.0 5 votes vote down vote up
public String user_prepareForgotPasswordMessage(Long id)  {
	try {
		
		User user = userDAO.findById(id);
		PasswordEncoder encoder = new ShaPasswordEncoder(256);
		String hash = encoder.encodePassword(user.getEmail() + new Date().getTime(), user.getSalt());
		PasswordResetRequest  passwordResetRequest = new PasswordResetRequest(user.getLogin() ,hash);
		passwordResetRequestDAO.merge(passwordResetRequest);
		return hash;
	} 
	catch (Exception e) {
		log.error(e.getMessage(),e);
		throw (new RuntimeException(e));
	}
}
 
Example #11
Source File: PasswordComparisonAuthenticator.java    From ranger with Apache License 2.0 4 votes vote down vote up
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
	Assert.notNull(passwordEncoder, "passwordEncoder must not be null.");
	this.passwordEncoder = passwordEncoder;
}
 
Example #12
Source File: StorageUpdate0.java    From nextreports-server with Apache License 2.0 4 votes vote down vote up
private void createSystemNodes() throws RepositoryException {
	LOG.info("Creating system nodes");
	
       Node rootNode = getTemplate().getRootNode();

       Node nextServerNode = rootNode.addNode(StorageConstants.NEXT_SERVER_FOLDER_NAME);
       nextServerNode.addMixin("mix:referenceable");
       nextServerNode.setProperty("className", Folder.class.getName());
       nextServerNode.setProperty("version", "-1");

       Node reportsNode = nextServerNode.addNode(StorageConstants.REPORTS_FOLDER_NAME);
       reportsNode.addMixin("mix:referenceable");
       reportsNode.setProperty("className", Folder.class.getName());

       Node datasourcesNode = nextServerNode.addNode(StorageConstants.DATASOURCES_FOLDER_NAME);
       datasourcesNode.addMixin("mix:referenceable");
       datasourcesNode.setProperty("className", Folder.class.getName());

       Node schedulersNode = nextServerNode.addNode(StorageConstants.SCHEDULER_FOLDER_NAME);
       schedulersNode.addMixin("mix:referenceable");
       schedulersNode.setProperty("className", Folder.class.getName());

       Node securityNode = nextServerNode.addNode(StorageConstants.SECURITY_FOLDER_NAME);
       securityNode.addMixin("mix:referenceable");
       securityNode.setProperty("className", Folder.class.getName());

       Node usersNode = securityNode.addNode(StorageConstants.USERS_FOLDER_NAME);
       usersNode.addMixin("mix:referenceable");
       usersNode.setProperty("className", Folder.class.getName());

       Node groupsNode = securityNode.addNode(StorageConstants.GROUPS_FOLDER_NAME);
       groupsNode.addMixin("mix:referenceable");
       groupsNode.setProperty("className", Folder.class.getName());

       Node adminNode = usersNode.addNode(StorageConstants.ADMIN_USER_NAME);
       adminNode.addMixin("mix:referenceable");
       adminNode.setProperty("className", User.class.getName());
       adminNode.setProperty("admin", true);
       PasswordEncoder passwordEncoder = new Md5PasswordEncoder();
       adminNode.setProperty("password", passwordEncoder.encodePassword("1", null));
       
       getTemplate().save();
}
 
Example #13
Source File: SecurityConfig.java    From xmall with MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example #14
Source File: SecurityConfig.java    From macrozheng-mall with MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example #15
Source File: SecurityConfig.java    From macrozheng-mall with MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example #16
Source File: UserLogicImpl.java    From icure-backend with GNU General Public License v2.0 4 votes vote down vote up
@Autowired
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
	this.passwordEncoder = passwordEncoder;
}
 
Example #17
Source File: SimpleHashUtil.java    From Roothub with GNU Affero General Public License v3.0 4 votes vote down vote up
public MyAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
	this.userDetailsService = userDetailsService;
	this.passwordEncoder = passwordEncoder;
}
 
Example #18
Source File: SecurityConfig.java    From xmall with MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example #19
Source File: SecurityConfig.java    From Spring-Security-Third-Edition with MIT License 2 votes vote down vote up
/**
 * Standard SHA-256 Password Encoder
 * @return ShaPasswordEncoder
 *
 * @see ShaPasswordEncoder
 */
@Bean
public PasswordEncoder passwordEncoder(){
    return new ShaPasswordEncoder(256);
}
 
Example #20
Source File: UserManagerImpl.java    From ankush with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Sets the password encoder.
 * 
 * @param passwordEncoder
 *            the new password encoder
 */
@Autowired
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
	this.passwordEncoder = passwordEncoder;
}