fix errors with password encoding
This commit is contained in:
Zakharov_Rostislav 2024-05-23 15:34:37 +04:00
parent 783f8886c2
commit 1d2b52444f
3 changed files with 11 additions and 3 deletions

2
.gitignore vendored
View File

@ -1,3 +1 @@
SpringApp/data.mv.db
SpringApp/library/data.mv.db
SpringApp/library/data.trace.db

View File

@ -20,7 +20,7 @@ import jakarta.persistence.Table;
public class UserEntity extends BaseEntity {
@Column(nullable = false, unique = true, length = 20)
private String login;
@Column(nullable = false, unique = false, length = 20)
@Column(nullable = false, unique = false)
private String password;
private UserRole role = UserRole.USER;
@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)

View File

@ -1,6 +1,7 @@
package com.ip.library.users.service;
import java.util.List;
import java.util.Optional;
import java.util.stream.StreamSupport;
import org.springframework.data.domain.Page;
@ -11,9 +12,11 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.ip.library.books.model.BookEntity;
import com.ip.library.books.service.BookService;
import com.ip.library.core.configuration.Constants;
import com.ip.library.core.error.NotFoundException;
import com.ip.library.core.jwt.JwtException;
import com.ip.library.core.jwt.JwtProvider;
@ -72,7 +75,14 @@ public class UserService implements UserDetailsService{
@Transactional
public UserEntity create(UserEntity entity) {
if (entity == null)
throw new IllegalArgumentException("Entity is null");
checkLoginUniqueness(entity.getLogin());
final String password = Optional.ofNullable(entity.getPassword()).orElse("");
entity.setPassword(
passwordEncoder.encode(
StringUtils.hasText(password.strip()) ? password : Constants.DEFAULT_PASSWORD));
entity.setRole(Optional.ofNullable(entity.getRole()).orElse(UserRole.USER));
return repository.save(entity);
}