Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mlproject.jpa.model;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- import lombok.AllArgsConstructor;
- import lombok.Builder;
- import lombok.Data;
- import lombok.NoArgsConstructor;
- import org.springframework.security.core.userdetails.UserDetails;
- import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
- import org.springframework.security.crypto.password.PasswordEncoder;
- import javax.persistence.*;
- import java.util.List;
- import java.util.Set;
- @Data
- @NoArgsConstructor
- @AllArgsConstructor
- @Builder
- @Entity
- @Table(name = "users")
- public class User implements UserDetails {
- public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- private String username;
- private String email;
- private boolean enabled;
- private boolean accountNonExpired;
- private boolean accountNonLocked;
- private boolean credentialsNonExpired;
- @JsonIgnore
- private String password;
- @JsonIgnore
- @ElementCollection(targetClass=Role.class, fetch = FetchType.EAGER)
- private List<Role> authorities;
- public User(String username, String password, boolean enabled) {
- this.username = username;
- this.password = PASSWORD_ENCODER.encode(password);
- this.enabled = enabled;
- }
- public void setPassword(String password) {
- this.password = PASSWORD_ENCODER.encode(password);
- }
- public static PasswordEncoder getPasswordEncoder() {
- return PASSWORD_ENCODER;
- }
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- User user = (User) o;
- if (!id.equals(user.id)) return false;
- if (!username.equals(user.username)) return false;
- if (!email.equals(user.email)) return false;
- return authorities != null ? authorities.equals(user.authorities) : user.authorities == null;
- }
- @Override
- public int hashCode() {
- int result = id.hashCode();
- result = 31 * result + username.hashCode();
- result = 31 * result + email.hashCode();
- result = 31 * result + (authorities != null ? authorities.hashCode() : 0);
- return result;
- }
- @Override
- public String toString() {
- return "User{" +
- "id=" + id +
- ", username='" + username + '\'' +
- ", email='" + email + '\'' +
- '}';
- }
- }
Add Comment
Please, Sign In to add comment