- Spring Security(Third Edition)
- Mick Knutson Robert Winch Peter Mularien
- 110字
- 2025-04-04 17:54:29
Hashing the passwords of new users
If we tried running the application and creating a new user, we would not be able to log in. This is because the newly-created user's password would not be hashed. We need to update DefaultCalendarService to hash the password. Make the following updates to ensure that the newly-created users' passwords are hashed:
//src/main/java/com/packtpub/springsecurity/service/DefaultCalendarService.java
import org.springframework.security.authentication.encoding.PasswordEncoder;
// other imports omitted
public class DefaultCalendarService implements CalendarService {
...
private final PasswordEncoder passwordEncoder;
@Autowired
public DefaultCalendarService(EventDao eventDao,
CalendarUserDao userDao, JdbcOperations jdbcOperations,
PasswordEncoder passwordEncoder) {
...
this.passwordEncoder = passwordEncoder;
}
...
public int createUser(CalendarUser user) {
String encodedPassword = passwordEncoder.
encodePassword(user.getPassword(), null);
user.setPassword(encodedPassword);
...
return userId;
}
}