removing password encoder new bean - using the default
This commit is contained in:
parent
59a2a422a3
commit
4179c843b8
@ -7,9 +7,9 @@ import org.baeldung.persistence.dao.VerificationTokenRepository;
|
|||||||
import org.baeldung.persistence.model.Role;
|
import org.baeldung.persistence.model.Role;
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.baeldung.persistence.model.VerificationToken;
|
import org.baeldung.persistence.model.VerificationToken;
|
||||||
import org.baeldung.security.hash.HashGenerator;
|
|
||||||
import org.baeldung.validation.EmailExistsException;
|
import org.baeldung.validation.EmailExistsException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@ -22,19 +22,20 @@ public class UserService implements IUserService {
|
|||||||
private VerificationTokenRepository tokenRepository;
|
private VerificationTokenRepository tokenRepository;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private HashGenerator hashGenerator;
|
private PasswordEncoder passwordEncoder;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
|
public User registerNewUserAccount(UserDto accountDto) throws EmailExistsException {
|
||||||
if (emailExist(accountDto.getEmail())) {
|
if (emailExist(accountDto.getEmail())) {
|
||||||
throw new EmailExistsException("There is an account with that email adress: " + 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.setFirstName(accountDto.getFirstName());
|
||||||
user.setLastName(accountDto.getLastName());
|
user.setLastName(accountDto.getLastName());
|
||||||
String hashedPassword = hashGenerator.getHashedPassword(accountDto.getPassword());
|
user.setPassword(passwordEncoder.encode(accountDto.getPassword()));
|
||||||
user.setPassword(hashedPassword);
|
|
||||||
user.setEmail(accountDto.getEmail());
|
user.setEmail(accountDto.getEmail());
|
||||||
|
|
||||||
user.setRole(new Role(Integer.valueOf(1), user));
|
user.setRole(new Role(Integer.valueOf(1), user));
|
||||||
return repository.save(user);
|
return repository.save(user);
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
@ -2,7 +2,6 @@ package org.baeldung.spring;
|
|||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
|
|
||||||
import org.baeldung.security.hash.HashGenerator;
|
|
||||||
import org.baeldung.validation.EmailValidator;
|
import org.baeldung.validation.EmailValidator;
|
||||||
import org.baeldung.validation.PasswordMatchesValidator;
|
import org.baeldung.validation.PasswordMatchesValidator;
|
||||||
import org.springframework.context.MessageSource;
|
import org.springframework.context.MessageSource;
|
||||||
@ -101,9 +100,4 @@ public class MvcConfig extends WebMvcConfigurerAdapter {
|
|||||||
return new PasswordMatchesValidator();
|
return new PasswordMatchesValidator();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
public HashGenerator hashGenerator() {
|
|
||||||
return new HashGenerator();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import org.springframework.context.annotation.ImportResource;
|
import org.springframework.context.annotation.ImportResource;
|
||||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||||
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
||||||
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
|
||||||
|
|
||||||
@Configuration
|
@Configuration
|
||||||
@ -35,4 +36,9 @@ public class SecSecurityConfig {
|
|||||||
return authProvider;
|
return authProvider;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public PasswordEncoder passwordEncoder() {
|
||||||
|
return new BCryptPasswordEncoder();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user