Guest User

Untitled

a guest
Feb 8th, 2017
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.56 KB | None | 0 0
  1. package com.mlproject.jpa.model;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonIgnore;
  4. import lombok.AllArgsConstructor;
  5. import lombok.Builder;
  6. import lombok.Data;
  7. import lombok.NoArgsConstructor;
  8. import org.springframework.security.core.userdetails.UserDetails;
  9. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  10. import org.springframework.security.crypto.password.PasswordEncoder;
  11.  
  12. import javax.persistence.*;
  13. import java.util.List;
  14. import java.util.Set;
  15.  
  16. @Data
  17. @NoArgsConstructor
  18. @AllArgsConstructor
  19. @Builder
  20. @Entity
  21. @Table(name = "users")
  22. public class User implements UserDetails {
  23.     public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();
  24.  
  25.     @Id
  26.     @GeneratedValue(strategy = GenerationType.AUTO)
  27.     private Long id;
  28.     private String username;
  29.     private String email;
  30.     private boolean enabled;
  31.     private boolean accountNonExpired;
  32.     private boolean accountNonLocked;
  33.     private boolean credentialsNonExpired;
  34.  
  35.     @JsonIgnore
  36.     private String password;
  37.  
  38.     @JsonIgnore
  39.     @ElementCollection(targetClass=Role.class, fetch = FetchType.EAGER)
  40.     private List<Role> authorities;
  41.  
  42.     public User(String username, String password, boolean enabled) {
  43.         this.username = username;
  44.         this.password = PASSWORD_ENCODER.encode(password);
  45.         this.enabled = enabled;
  46.     }
  47.  
  48.     public void setPassword(String password) {
  49.         this.password = PASSWORD_ENCODER.encode(password);
  50.     }
  51.  
  52.     public static PasswordEncoder getPasswordEncoder() {
  53.         return PASSWORD_ENCODER;
  54.     }
  55.  
  56.  
  57.     @Override
  58.     public boolean equals(Object o) {
  59.         if (this == o) return true;
  60.         if (o == null || getClass() != o.getClass()) return false;
  61.  
  62.         User user = (User) o;
  63.  
  64.         if (!id.equals(user.id)) return false;
  65.         if (!username.equals(user.username)) return false;
  66.         if (!email.equals(user.email)) return false;
  67.         return authorities != null ? authorities.equals(user.authorities) : user.authorities == null;
  68.     }
  69.  
  70.     @Override
  71.     public int hashCode() {
  72.         int result = id.hashCode();
  73.         result = 31 * result + username.hashCode();
  74.         result = 31 * result + email.hashCode();
  75.         result = 31 * result + (authorities != null ? authorities.hashCode() : 0);
  76.         return result;
  77.     }
  78.  
  79.     @Override
  80.     public String toString() {
  81.         return "User{" +
  82.                 "id=" + id +
  83.                 ", username='" + username + '\'' +
  84.                 ", email='" + email + '\'' +
  85.                 '}';
  86.     }
  87. }
Add Comment
Please, Sign In to add comment