extract password hashing to external service PasswordService.java

This commit is contained in:
Mika Bomm 2024-09-08 10:18:37 +02:00
parent fbc24d309e
commit fa7dd1e3c4
2 changed files with 27 additions and 4 deletions

View file

@ -0,0 +1,22 @@
package com.mixel.docusphere.service;
import org.springframework.security.crypto.argon2.Argon2PasswordEncoder;
import org.springframework.stereotype.Service;
@Service
public class PasswordService {
private final Argon2PasswordEncoder passwordEncoder;
public PasswordService() {
this.passwordEncoder = new Argon2PasswordEncoder(16, 32, 1, 4096, 3);
}
public String hashPassword(String password) {
return passwordEncoder.encode(password);
}
public boolean checkPassword(String rawPassword, String encodedPassword) {
return passwordEncoder.matches(rawPassword, encodedPassword);
}
}

View file

@ -18,12 +18,13 @@ public class UserService {
private final UserRepository userRepository;
private final Argon2PasswordEncoder passwordEncoder;
private final PasswordService passwordService;
// Constructor
public UserService(UserRepository userRepository) {
this.passwordEncoder = new Argon2PasswordEncoder(16, 32, 1, 4096, 3);
public UserService(UserRepository userRepository, PasswordService passwordService) {
this.userRepository = userRepository;
this.passwordService = passwordService;
}
public List<UserRespondDTO> findAll() {
@ -59,7 +60,7 @@ public class UserService {
private void isPasswordAlreadySet(UserRequestDTO userRequestDTO, User user) {
if (userRequestDTO.getPassword() != null && !userRequestDTO.getPassword().isEmpty()) {
user.setPasswordHash(passwordEncoder.encode(userRequestDTO.getPassword()));
user.setPasswordHash(passwordService.hashPassword(userRequestDTO.getPassword()));
}
}