as I am building a Login for a Website I would like to store a auto generated password in MySQL in a safe way, thus encrypt it in some way. But I am not sure how to combine the auto generation with encrypting the password.
I looked at a lot of questions but none really helped me or answered my question considering that it should be autogenerated. Also I would like to solve it in an elegant way without hardcoding and stuff if possible. I would be really happy if someone could help me as I am a beginner, thanks in advance!
Question:
How do I auto generate a secure encrypted passwords, what datatype should I use in Java (currently String) and then to what database type is it mapped?
How do I make sure I can decrypt the password of the database to check if it matches when someone logs in?
User Entity:
@Entity
public class User {
@Id
@GeneratedValue
private Integer user_id;
@Column(unique = true)
private String username;
private String password;
@Enumerated(EnumType.STRING)
private Role role;
//non-Owning (address) side of the OneToOne relationship
@OneToOne(mappedBy = "user")
private RetailStore retailStore;
/**
* Constructor
*/
protected User() {
}
/**
* Constructor
* @param username
* @param role
*/
public User(String username, String password, Role role) {
super();
this.username = username;
this.password = password;
this.role = role;
}
User Repository:
@Repository
@Transactional
//necessary to be in an transaction to change something in database,
//instead of doing this for every method itself it is declared by
//the annotation @Transactional
public interface UserRepository extends JpaRepository<User, Long>{
}