Guest User

Untitled

a guest
Aug 7th, 2018
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. @Entity
  2. public class User implements UserDetails, Serializable{
  3.  
  4. private static final long serialVersionUID = 902783495L;
  5.  
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.AUTO)
  8. @Column(name="Id", nullable=false, updatable = false)
  9. private Long id;
  10.  
  11. private String username;
  12. private String password;
  13. private String email;
  14.  
  15. private boolean enabled = true;
  16.  
  17. @OneToMany(mappedBy = "user", cascade=CascadeType.ALL, fetch = FetchType.EAGER)
  18. @JsonIgnore
  19. private Set<UserRole> userRoles = new HashSet<>();
  20.  
  21. public Long getId() {
  22. return id;
  23. }
  24.  
  25. public void setId(Long id) {
  26. this.id = id;
  27. }
  28.  
  29. public String getUsername() {
  30. return username;
  31. }
  32.  
  33. public void setUsername(String username) {
  34. this.username = username;
  35. }
  36.  
  37. public String getPassword() {
  38. return password;
  39. }
  40.  
  41. public void setPassword(String password) {
  42. this.password = password;
  43. }
  44.  
  45.  
  46.  
  47. public void setEnabled(boolean enabled) {
  48. this.enabled = enabled;
  49. }
  50.  
  51. public Set<UserRole> getUserRoles() {
  52. return userRoles;
  53. }
  54.  
  55. public void setUserRoles(Set<UserRole> userRoles) {
  56. this.userRoles = userRoles;
  57. }
  58.  
  59. public String getEmail() {
  60. return email;
  61. }
  62.  
  63. public void setEmail(String email) {
  64. this.email = email;
  65. }
  66.  
  67. @Override
  68. public Collection<? extends GrantedAuthority> getAuthorities() {
  69.  
  70. Set<GrantedAuthority> authorities = new HashSet<>();
  71. userRoles.forEach(ur -> authorities.add(new Authority(ur.getRole().getName())));
  72.  
  73. return authorities;
  74. }
  75.  
  76. @Override
  77. public boolean isAccountNonExpired() {
  78. // TODO Auto-generated method stub
  79. return true;
  80. }
  81.  
  82. @Override
  83. public boolean isAccountNonLocked() {
  84. // TODO Auto-generated method stub
  85. return true;
  86. }
  87.  
  88. @Override
  89. public boolean isCredentialsNonExpired() {
  90. // TODO Auto-generated method stub
  91. return true;
  92. }
  93.  
  94. @Override
  95. public boolean isEnabled() {
  96. return enabled;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. }
  103.  
  104. @Entity
  105. public class Role implements Serializable{
  106.  
  107. private static final long serialVersionUID = 890245234L;
  108.  
  109. @Id
  110. private int roleId;
  111.  
  112. private String name;
  113.  
  114. @OneToMany(mappedBy = "role", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  115. private Set<UserRole> userRoles = new HashSet<>();
  116.  
  117. public Role(){}
  118.  
  119. public int getRoleId() {
  120. return roleId;
  121. }
  122.  
  123. public void setRoleId(int roleId) {
  124. this.roleId = roleId;
  125. }
  126.  
  127. public String getName() {
  128. return name;
  129. }
  130.  
  131. public void setName(String name) {
  132. this.name = name;
  133. }
  134.  
  135. public Set<UserRole> getUserRoles() {
  136. return userRoles;
  137. }
  138.  
  139. public void setUserRoles(Set<UserRole> userRoles) {
  140. this.userRoles = userRoles;
  141. }
  142.  
  143.  
  144. }
  145.  
  146. @Entity
  147. @Table(name="user_role")
  148. public class UserRole implements Serializable {
  149. private static final long serialVersionUID = 890345L;
  150.  
  151. @Id
  152. @GeneratedValue(strategy = GenerationType.AUTO)
  153. private long userRoleId;
  154.  
  155. public UserRole () {}
  156.  
  157. public UserRole (User user, Role role) {
  158. this.user = user;
  159. this.role = role;
  160. }
  161.  
  162. @ManyToOne(fetch = FetchType.EAGER)
  163. @JoinColumn(name = "user_id")
  164. private User user;
  165.  
  166. @ManyToOne(fetch = FetchType.EAGER)
  167. private Role role;
  168.  
  169. public long getUserRoleId() {
  170. return userRoleId;
  171. }
  172.  
  173. public void setUserRoleId(long userRoleId) {
  174. this.userRoleId = userRoleId;
  175. }
  176.  
  177. public User getUser() {
  178. return user;
  179. }
  180.  
  181. public void setUser(User user) {
  182. this.user = user;
  183. }
  184.  
  185. public Role getRole() {
  186. return role;
  187. }
  188.  
  189. public void setRole(Role role) {
  190. this.role = role;
  191. }
  192.  
  193.  
  194. }
Add Comment
Please, Sign In to add comment