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

The following examples show how to use org.springframework.security.authentication.encoding.PasswordEncoder. These examples are extracted from open source projects. 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
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
@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 3
/**
 * 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 4
/**
 * 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 5
/**
 * 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 6
Source Project: onboard   Source File: PasswordUtilsTest.java    License: 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 7
Source Project: JDeSurvey   Source File: UserService.java    License: 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 8
Source Project: JDeSurvey   Source File: UserService.java    License: 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 9
Source Project: JDeSurvey   Source File: UserService.java    License: 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 10
Source Project: JDeSurvey   Source File: UserService.java    License: 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 11
Source Project: xmall   Source File: SecurityConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example 12
Source Project: xmall   Source File: SecurityConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example 13
public MyAuthenticationProvider(UserDetailsService userDetailsService, PasswordEncoder passwordEncoder) {
	this.userDetailsService = userDetailsService;
	this.passwordEncoder = passwordEncoder;
}
 
Example 14
Source Project: icure-backend   Source File: UserLogicImpl.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Autowired
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
	this.passwordEncoder = passwordEncoder;
}
 
Example 15
Source Project: macrozheng-mall   Source File: SecurityConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example 16
Source Project: macrozheng-mall   Source File: SecurityConfig.java    License: MIT License 4 votes vote down vote up
@Bean
public PasswordEncoder passwordEncoder() {
    return new Md5PasswordEncoder();
}
 
Example 17
Source Project: ranger   Source File: PasswordComparisonAuthenticator.java    License: 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 18
Source Project: nextreports-server   Source File: StorageUpdate0.java    License: 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 19
Source Project: Spring-Security-Third-Edition   Source File: SecurityConfig.java    License: 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
/**
 * Sets the password encoder.
 * 
 * @param passwordEncoder
 *            the new password encoder
 */
@Autowired
public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
	this.passwordEncoder = passwordEncoder;
}