You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
3.0 KiB

2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
2 weeks ago
  1. package com.example.demo.domain.entity;
  2. import com.fasterxml.jackson.annotation.JsonFormat;
  3. import com.fasterxml.jackson.annotation.JsonIgnore;
  4. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  5. import lombok.Data;
  6. import lombok.NoArgsConstructor;
  7. import org.springframework.security.core.GrantedAuthority;
  8. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  9. import org.springframework.security.core.userdetails.UserDetails;
  10. import java.io.Serializable;
  11. import java.util.*;
  12. @Data
  13. @NoArgsConstructor
  14. @JsonIgnoreProperties(ignoreUnknown = true)
  15. public class Admin implements UserDetails, Serializable {
  16. private static final long serialVersionUID = 1L;
  17. private Integer id; // 主键ID
  18. private String adminName; // 用户姓名
  19. private String account; // 账号
  20. private String password; // 密码
  21. private String machineId; // 机器码,限两个
  22. private Byte adminStatus; // 状态(启用/不启用)
  23. private String market; // 地区
  24. private String postiton; // 职位
  25. private String remark; // 备注
  26. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
  27. private Date createTime; // 创建时间
  28. @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai")
  29. private Date updateTime; // 更新时间
  30. private Integer roleId;
  31. @Override
  32. @JsonIgnore
  33. public Collection<? extends GrantedAuthority> getAuthorities() {
  34. Set<GrantedAuthority> authorities = new HashSet<>();
  35. Optional.ofNullable(postiton)
  36. .map(Integer::valueOf)
  37. .ifPresent(permValue -> {
  38. switch (permValue) {
  39. case 1:
  40. authorities.add(new SimpleGrantedAuthority("ROLE_SUPER_ADMIN"));
  41. break;
  42. case 2:
  43. authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
  44. break;
  45. case 3:
  46. authorities.add(new SimpleGrantedAuthority("ROLE_AUDITORS"));
  47. break;
  48. case 5:
  49. authorities.add(new SimpleGrantedAuthority("Branch_Manager"));
  50. break;
  51. default:
  52. // 可以添加默认角色或处理未知权限
  53. break;
  54. }
  55. });
  56. return authorities;
  57. }
  58. @Override
  59. public String getUsername() {
  60. return account;
  61. }
  62. @Override
  63. @JsonIgnore
  64. public boolean isAccountNonExpired() {
  65. return true; // 默认账户未过期
  66. }
  67. @Override
  68. @JsonIgnore
  69. public boolean isAccountNonLocked() {
  70. return true; // 默认账户未锁定
  71. }
  72. @Override
  73. @JsonIgnore
  74. public boolean isCredentialsNonExpired() {
  75. return true; // 默认凭证未过期
  76. }
  77. @Override
  78. @JsonIgnore
  79. public boolean isEnabled() {
  80. return adminStatus != null && adminStatus == 1;
  81. }
  82. }