Advertisement
Guest User

Untitled

a guest
Oct 20th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.46 KB | None | 0 0
  1. @Entity(name = "USERS")
  2. public class User implements Serializable {
  3.  
  4. private static final long serialVersionUID = 1L;
  5. @Id
  6. @Column(name = "USER_NAME")
  7. private String userName;
  8. @Column(name = "PASSWD", length = 32,
  9. columnDefinition = "VARCHAR(32)")
  10. private char[] password;
  11. @OneToOne(fetch = FetchType.EAGER,
  12. cascade = CascadeType.ALL, mappedBy = "user")
  13. private Role role;
  14. private String name;
  15. private String lastname;
  16. @ManyToOne
  17. private Province province;
  18.  
  19. public User() {
  20. }
  21.  
  22. public User(String userName, char[] password, ROLE role) {
  23. this.userName = userName;
  24. this.password = hashPassword(password);
  25. this.role = new Role(role, this);
  26. }
  27.  
  28. public char[] getPassword() {
  29. return password;
  30. }
  31.  
  32. public void setPassword(char[] password) {
  33. this.password = hashPassword(password);
  34. }
  35.  
  36. public String getUserName() {
  37. return userName;
  38. }
  39.  
  40. public void setUserName(String userName) {
  41. this.userName = userName;
  42. }
  43.  
  44. public Role getRole() {
  45. return role;
  46. }
  47.  
  48. public void setRole(Role role) {
  49. this.role = role;
  50. role.setUser(this);
  51. }
  52.  
  53. public String getName() {
  54. return name;
  55. }
  56.  
  57. public void setName(String name) {
  58. this.name = name;
  59. }
  60.  
  61. public String getLastname() {
  62. return lastname;
  63. }
  64.  
  65. public void setLastname(String lastname) {
  66. this.lastname = lastname;
  67. }
  68.  
  69. public Province getProvince() {
  70. return province;
  71. }
  72.  
  73. public void setProvince(Province province) {
  74. this.province = province;
  75. }
  76.  
  77. @Override
  78. public boolean equals(Object obj) {
  79. if (obj == null) {
  80. return false;
  81. }
  82. if (getClass() != obj.getClass()) {
  83. return false;
  84. }
  85. final User other = (User) obj;
  86. if ((this.userName == null) ? (other.userName != null) :
  87. !this.userName.equals(other.userName)) {
  88. return false;
  89. }
  90. return true;
  91. }
  92.  
  93. @Override
  94. public int hashCode() {
  95. int hash = 3;
  96. hash = 83 * hash + (this.userName != null ? this.userName.hashCode() : 0);
  97. return hash;
  98. }
  99.  
  100. @Override
  101. public String toString() {
  102. return "User{" + "userName=" + userName + ", password=" + password + ", role=" + role + '}';
  103. }
  104.  
  105.  
  106.  
  107. private char[] hashPassword(char[] password) {
  108. char[] encoded = null;
  109. try {
  110. ByteBuffer passwdBuffer =
  111. Charset.defaultCharset().encode(CharBuffer.wrap(password));
  112. byte[] passwdBytes = passwdBuffer.array();
  113. MessageDigest mdEnc = MessageDigest.getInstance("MD5");
  114. mdEnc.update(passwdBytes, 0, password.length);
  115. encoded = new BigInteger(1, mdEnc.digest()).toString(16).toCharArray();
  116. } catch (NoSuchAlgorithmException ex) {
  117. Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
  118. }
  119.  
  120. return encoded;
  121. }
  122. }
  123.  
  124.  
  125.  
  126. @Entity(name = "ROLES")
  127. public class Role implements Serializable {
  128.  
  129. public static enum ROLE {
  130. ADMINISTRATOR, USER
  131. }
  132.  
  133. private static final long serialVersionUID = 1L;
  134. @Id
  135. @Column(name = "ROLE_NAME")
  136. @Enumerated(EnumType.STRING)
  137. private ROLE role;
  138. @Id
  139. @OneToOne
  140. @JoinColumn(name = "USER_NAME")
  141. private User user;
  142.  
  143. protected Role() {
  144. }
  145.  
  146. protected Role(ROLE role, User user) {
  147. this.role = role;
  148. this.user = user;
  149. }
  150.  
  151. public ROLE getRole() {
  152. return role;
  153. }
  154.  
  155. public void setRole(ROLE role) {
  156. this.role = role;
  157. }
  158.  
  159. public User getUser() {
  160. return user;
  161. }
  162.  
  163. public void setUser(User user) {
  164. this.user = user;
  165. }
  166.  
  167. @Override
  168. public boolean equals(Object obj) {
  169. if (obj == null) {
  170. return false;
  171. }
  172. if (getClass() != obj.getClass()) {
  173. return false;
  174. }
  175. final Role other = (Role) obj;
  176. if (this.role != other.role) {
  177. return false;
  178. }
  179. if (this.user != other.user && (this.user == null ||
  180. !this.user.equals(other.user))) {
  181. return false;
  182. }
  183. return true;
  184. }
  185.  
  186. @Override
  187. public int hashCode() {
  188. int hash = 5;
  189. hash = 89 * hash + (this.role != null ? this.role.hashCode() : 0);
  190. hash = 89 * hash + (this.user != null ? this.user.hashCode() : 0);
  191. return hash;
  192. }
  193.  
  194. @Override
  195. public String toString() {
  196. return "Role{" + "role=" + role + ", user=" + user + '}';
  197. }
  198.  
  199.  
  200. }
  201.  
  202. <security-constraint>
  203. <display-name>Security Constraints</display-name>
  204. <web-resource-collection>
  205. <web-resource-name>Only Admin</web-resource-name>
  206. <description>only admin resources</description>
  207. <url-pattern>/admin/*</url-pattern>
  208. </web-resource-collection>
  209. <auth-constraint>
  210. <description/>
  211. <role-name>ADMINISTRATOR</role-name>
  212. </auth-constraint>
  213. </security-constraint>
  214. <login-config>
  215. <auth-method>FORM</auth-method>
  216. <realm-name>JDBCRealm</realm-name>
  217. <form-login-config>
  218. <form-login-page>/login.xhtml</form-login-page>
  219. <form-error-page>/login-error.xhtml</form-error-page>
  220. </form-login-config>
  221. </login-config>
  222. <security-role>
  223. <description>Administradores</description>
  224. <role-name>ADMINISTRATOR</role-name>
  225. </security-role>
  226. <security-role>
  227. <description>Usuario normal</description>
  228. <role-name>USER</role-name>
  229. </security-role>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement