|
@@ -0,0 +1,332 @@
|
|
|
+package cn.rankin.cmsweb.entity;
|
|
|
+
|
|
|
+import cn.rankin.common.utils.constant.Domain;
|
|
|
+import com.alibaba.fastjson.annotation.JSONField;
|
|
|
+import org.springframework.security.core.CredentialsContainer;
|
|
|
+import org.springframework.security.core.GrantedAuthority;
|
|
|
+import org.springframework.security.core.SpringSecurityCoreVersion;
|
|
|
+import org.springframework.util.Assert;
|
|
|
+
|
|
|
+import java.io.Serializable;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author Dreampie
|
|
|
+ * @date 2015-09-18
|
|
|
+ * @what
|
|
|
+ */
|
|
|
+public class UserDetails implements org.springframework.security.core.userdetails.UserDetails, CredentialsContainer {
|
|
|
+
|
|
|
+ private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
|
|
|
+
|
|
|
+ private Set<GrantedAuthority> authorities;
|
|
|
+
|
|
|
+ private boolean accountNonExpired;
|
|
|
+
|
|
|
+ private boolean accountNonLocked;
|
|
|
+
|
|
|
+ private boolean credentialsNonExpired;
|
|
|
+
|
|
|
+ private boolean enabled;
|
|
|
+
|
|
|
+ private String id;
|
|
|
+
|
|
|
+ private String avatar;
|
|
|
+
|
|
|
+ private Date birthday;
|
|
|
+
|
|
|
+ private String code;
|
|
|
+
|
|
|
+ private Integer domain;
|
|
|
+
|
|
|
+ private String merchantId;
|
|
|
+
|
|
|
+ private String merchantName;
|
|
|
+
|
|
|
+ private String mobileNo;
|
|
|
+
|
|
|
+ private String username;
|
|
|
+
|
|
|
+ private String nickName;
|
|
|
+
|
|
|
+ private String password;
|
|
|
+
|
|
|
+ private Boolean isAdmin;
|
|
|
+
|
|
|
+ public UserDetails() {
|
|
|
+ super();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Calls the more complex constructor with all boolean arguments set to {@code true}.
|
|
|
+ */
|
|
|
+ public UserDetails(String username, String password, Collection<? extends GrantedAuthority> authorities) {
|
|
|
+ this(username, password, true, true, true, true, authorities);
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserDetails(String username, String password, boolean enabled,
|
|
|
+ boolean accountNonExpired, boolean credentialsNonExpired,
|
|
|
+ boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
|
|
|
+
|
|
|
+ if (((username == null) || "".equals(username)) || (password == null)) {
|
|
|
+ throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
|
|
|
+ }
|
|
|
+
|
|
|
+ this.username = username;
|
|
|
+ this.password = password;
|
|
|
+ this.enabled = enabled;
|
|
|
+ this.accountNonExpired = accountNonExpired;
|
|
|
+ this.credentialsNonExpired = credentialsNonExpired;
|
|
|
+ this.accountNonLocked = accountNonLocked;
|
|
|
+ this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
|
|
|
+ }
|
|
|
+
|
|
|
+ public UserDetails(Set<GrantedAuthority> authorities, boolean accountNonExpired, boolean accountNonLocked, boolean credentialsNonExpired, boolean enabled, String id, String username, String avatar, Date birthday, String code, Integer domain, String merchantId, String merchantName, String mobileNo, String nickName, String password, Boolean isAdmin) {
|
|
|
+
|
|
|
+ if (((username == null) || "".equals(username)) || (password == null)) {
|
|
|
+ throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
|
|
|
+ }
|
|
|
+ this.authorities = authorities;
|
|
|
+ this.accountNonExpired = accountNonExpired;
|
|
|
+ this.accountNonLocked = accountNonLocked;
|
|
|
+ this.credentialsNonExpired = credentialsNonExpired;
|
|
|
+ this.enabled = enabled;
|
|
|
+ this.id = id;
|
|
|
+ this.username = username;
|
|
|
+ this.avatar = avatar;
|
|
|
+ this.birthday = birthday;
|
|
|
+ this.code = code;
|
|
|
+ this.domain = domain;
|
|
|
+ this.merchantId = merchantId;
|
|
|
+ this.merchantName=merchantName;
|
|
|
+ this.mobileNo = mobileNo;
|
|
|
+ this.nickName = nickName;
|
|
|
+ this.password = password;
|
|
|
+ this.isAdmin = isAdmin;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // ~ Methods
|
|
|
+ // ========================================================================================================
|
|
|
+
|
|
|
+ private static SortedSet<GrantedAuthority> sortAuthorities(
|
|
|
+ Collection<? extends GrantedAuthority> authorities) {
|
|
|
+ Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
|
|
|
+ // Ensure array iteration order is predictable (as per
|
|
|
+ // UserDetails.getAuthorities() contract and SEC-717)
|
|
|
+ SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<GrantedAuthority>(
|
|
|
+ new AuthorityComparator());
|
|
|
+
|
|
|
+ for (GrantedAuthority grantedAuthority : authorities) {
|
|
|
+ Assert.notNull(grantedAuthority,
|
|
|
+ "GrantedAuthority list cannot contain any null elements");
|
|
|
+ sortedAuthorities.add(grantedAuthority);
|
|
|
+ }
|
|
|
+
|
|
|
+ return sortedAuthorities;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getId() {
|
|
|
+ return id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setId(String id) {
|
|
|
+ this.id = id;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getAvatar() {
|
|
|
+ return avatar;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAvatar(String avatar) {
|
|
|
+ this.avatar = avatar;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Date getBirthday() {
|
|
|
+ return birthday;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setBirthday(Date birthday) {
|
|
|
+ this.birthday = birthday;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getCode() {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCode(String code) {
|
|
|
+ this.code = code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Integer getDomain() {
|
|
|
+ return domain;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setDomain(Integer domain) {
|
|
|
+ this.domain = domain;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMerchantId() {
|
|
|
+ return merchantId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMerchantId(String merchantId) {
|
|
|
+ this.merchantId = merchantId;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMerchantName() {
|
|
|
+ return merchantName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMerchantName(String merchantName) {
|
|
|
+ this.merchantName = merchantName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getMobileNo() {
|
|
|
+ return mobileNo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setMobileNo(String mobileNo) {
|
|
|
+ this.mobileNo = mobileNo;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getUsername() {
|
|
|
+ return username;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setUsername(String username) {
|
|
|
+ this.username = username;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getNickName() {
|
|
|
+ return nickName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setNickName(String nickName) {
|
|
|
+ this.nickName = nickName;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setPassword(String password) {
|
|
|
+ this.password = password;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Boolean getAdmin() {
|
|
|
+ return isAdmin;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setAdmin(Boolean admin) {
|
|
|
+ isAdmin = admin;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public Collection<GrantedAuthority> getAuthorities() {
|
|
|
+ return authorities;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public String getPassword() {
|
|
|
+ return password;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public boolean isEnabled() {
|
|
|
+ return enabled;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public boolean isAccountNonExpired() {
|
|
|
+ return accountNonExpired;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public boolean isAccountNonLocked() {
|
|
|
+ return accountNonLocked;
|
|
|
+ }
|
|
|
+
|
|
|
+ @JSONField(serialize = false)
|
|
|
+ public boolean isCredentialsNonExpired() {
|
|
|
+ return credentialsNonExpired;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void eraseCredentials() {
|
|
|
+ password = null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns {@code true} if the supplied object is a {@code UserDetails} instance with the
|
|
|
+ * same {@code username} value.
|
|
|
+ * <p>
|
|
|
+ * In other words, the objects are equal if they have the same username, representing
|
|
|
+ * the same principal.
|
|
|
+ */
|
|
|
+
|
|
|
+ public boolean equals(Object rhs) {
|
|
|
+ if (rhs instanceof UserDetails) {
|
|
|
+ return username.equals(((UserDetails) rhs).username);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Returns the hashcode of the {@code username}.
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public int hashCode() {
|
|
|
+ return username.hashCode();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String toString() {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ sb.append(super.toString()).append(": ");
|
|
|
+ sb.append("Username: ").append(this.username).append("; ");
|
|
|
+ sb.append("Password: [PROTECTED]; ");
|
|
|
+ sb.append("Enabled: ").append(this.enabled).append("; ");
|
|
|
+ sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
|
|
|
+ sb.append("credentialsNonExpired: ").append(this.credentialsNonExpired)
|
|
|
+ .append("; ");
|
|
|
+ sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
|
|
|
+
|
|
|
+ if (!authorities.isEmpty()) {
|
|
|
+ sb.append("Granted Authorities: ");
|
|
|
+
|
|
|
+ boolean first = true;
|
|
|
+ for (GrantedAuthority auth : authorities) {
|
|
|
+ if (!first) {
|
|
|
+ sb.append(",");
|
|
|
+ }
|
|
|
+ first = false;
|
|
|
+
|
|
|
+ sb.append(auth);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ sb.append("Not granted any authorities");
|
|
|
+ }
|
|
|
+
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean isPlatForm() {
|
|
|
+ if (domain == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ return domain.equals(Domain.PLATFORM);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static class AuthorityComparator implements Comparator<GrantedAuthority>,
|
|
|
+ Serializable {
|
|
|
+ private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
|
|
|
+
|
|
|
+ public int compare(GrantedAuthority g1, GrantedAuthority g2) {
|
|
|
+ if (g2.getAuthority() == null) {
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (g1.getAuthority() == null) {
|
|
|
+ return 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ return g1.getAuthority().compareTo(g2.getAuthority());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|