lab-5
fix errors with password encoding
This commit is contained in:
parent
783f8886c2
commit
1d2b52444f
2
.gitignore
vendored
2
.gitignore
vendored
@ -1,3 +1 @@
|
||||
SpringApp/data.mv.db
|
||||
SpringApp/library/data.mv.db
|
||||
SpringApp/library/data.trace.db
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user