Делаю регистрацию юзера. Длина пароля должна быть от 6 до 16 символов. Сначала думал воспользоваться аннотацией @Size(min = 6, max = 16) в энтити, но потом выяснил, что после шифрования пароля BCryptPasswordEncoder именно зашифрованную часть проверяет @Size, а это 60 символов. Как мне реализовать эту функцию? Думал сделать через сервис, но как по мне это хардкод, уверен есть решение лучше.
SellerController:
@RestController
@RequestMapping("/api")
public class SellerController {
@Autowired
SellerServiceImpl sellerService;
@RequestMapping(value = "/seller-registration", method = RequestMethod.POST)
public ResponseEntity<String> sellerRegistration(@Valid @RequestBody Seller newSeller) {
Seller seller = sellerService.save(newSeller);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Seller:
@Entity
@Table(name = "users")
public class Seller {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "username", unique = true)
@NotNull(message = "username can not be null.")
private String username;
@Column(name = "password")
@NotNull(message = "password can not be null.")
//@Size(min = 6, max = 15)
private String password;
@ManyToMany
@JoinTable(name = "user_roles", joinColumns = @JoinColumn(name = "user_id"),
inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roles;
@Column(name = "email", unique = true)
@NotNull(message = "email can not be null.")
@Email
private String email;
SellerServiceImpl:
@Service
public class SellerServiceImpl {
@Autowired
SellerRepository sellerRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
PasswordEncoder passwordEncoder;
public SellerServiceImpl() {
}
public SellerServiceImpl(SellerRepository sellerRepository, RoleRepository roleRepository,
PasswordEncoder passwordEncoder) {
this.sellerRepository = sellerRepository;
this.roleRepository = roleRepository;
this.passwordEncoder = passwordEncoder;
}
public Seller save(Seller seller) {
seller.setPassword(passwordEncoder.encode(seller.getPassword()));
Set<Role> roles = new HashSet<>();
roles.add(roleRepository.findOne(2L));
seller.setRoles(roles);
return sellerRepository.save(seller);
}
}