removing password encoder new bean - using the default

This commit is contained in:
eugenp 2015-01-04 12:56:16 +02:00
parent 59a2a422a3
commit 4179c843b8
4 changed files with 12 additions and 23 deletions

View File

@ -7,9 +7,9 @@ import org.baeldung.persistence.dao.VerificationTokenRepository;
import org.baeldung.persistence.model.Role;
import org.baeldung.persistence.model.User;
import org.baeldung.persistence.model.VerificationToken;
import org.baeldung.security.hash.HashGenerator;
import org.baeldung.validation.EmailExistsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
@ -22,19 +22,20 @@ public class UserService implements IUserService {
private VerificationTokenRepository tokenRepository;
@Autowired
private HashGenerator hashGenerator;
private PasswordEncoder passwordEncoder;
@Override
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
if (emailExist(accountDto.getEmail())) {
throw new EmailExistsException("There is an account with that email adress: " + accountDto.getEmail());
}
User user = new User();
final User user = new User();
user.setFirstName(accountDto.getFirstName());
user.setLastName(accountDto.getLastName());
String hashedPassword = hashGenerator.getHashedPassword(accountDto.getPassword());
user.setPassword(hashedPassword);
user.setPassword(passwordEncoder.encode(accountDto.getPassword()));
user.setEmail(accountDto.getEmail());
user.setRole(new Role(Integer.valueOf(1), user));
return repository.save(user);
}

View File

@ -1,12 +0,0 @@
package org.baeldung.security.hash;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
public class HashGenerator {
public String getHashedPassword(String password) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String hashedPassword = passwordEncoder.encode(password);
return hashedPassword;
}
}

View File

@ -2,7 +2,6 @@ package org.baeldung.spring;
import java.util.Locale;
import org.baeldung.security.hash.HashGenerator;
import org.baeldung.validation.EmailValidator;
import org.baeldung.validation.PasswordMatchesValidator;
import org.springframework.context.MessageSource;
@ -101,9 +100,4 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
return new PasswordMatchesValidator();
}
@Bean
public HashGenerator hashGenerator() {
return new HashGenerator();
}
}

View File

@ -7,6 +7,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
@Configuration
@ -35,4 +36,9 @@ public class SecSecurityConfig {
return authProvider;
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}