cn.bestwu.simpleframework.security.CustomUserDetails Maven / Gradle / Ivy
package cn.bestwu.simpleframework.security;
import java.io.Serializable;
import java.util.Collection;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.Assert;
/**
* 自定义 UserDetails
*
* @author Peter Wu
*/
public class CustomUserDetails implements UserDetails {
private static final long serialVersionUID = 1L;
private ISecurityUser user;
private Collection extends GrantedAuthority> authorities;
public CustomUserDetails(ISecurityUser user,
Collection extends GrantedAuthority> authorities) {
this.user = user;
this.authorities = authorities;
}
@Override
public Collection extends GrantedAuthority> getAuthorities() {
return sortAuthorities(authorities);
}
@Override
public String getPassword() {
return user.getPassword();
}
@Override
public String getUsername() {
return user.getUsername();
}
@Override
public boolean isAccountNonExpired() {
return true;
}
@Override
public boolean isAccountNonLocked() {
return true;
}
@Override
public boolean isCredentialsNonExpired() {
return true;
}
@Override
public boolean isEnabled() {
return true;
}
//--------------------------------------------
public void setAuthorities(
Collection extends GrantedAuthority> authorities) {
this.authorities = authorities;
}
public void setUser(ISecurityUser user) {
this.user = user;
}
public ISecurityUser getUser() {
return user;
}
//--------------------------------------------
private static SortedSet 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 sortedAuthorities = new TreeSet<>(
new AuthorityComparator());
for (GrantedAuthority grantedAuthority : authorities) {
Assert.notNull(grantedAuthority,
"GrantedAuthority list cannot contain any null elements");
sortedAuthorities.add(grantedAuthority);
}
sortedAuthorities.add(new SimpleGrantedAuthority("authenticated"));
return sortedAuthorities;
}
private static class AuthorityComparator implements Comparator,
Serializable {
private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;
public int compare(GrantedAuthority g1, GrantedAuthority g2) {
// Neither should ever be null as each entry is checked before adding it to
// the set.
// If the authority is null, it is a custom authority and should precede
// others.
if (g2.getAuthority() == null) {
return -1;
}
if (g1.getAuthority() == null) {
return 1;
}
return g1.getAuthority().compareTo(g2.getAuthority());
}
}
//--------------------------------------------
@Override
public boolean equals(Object rhs) {
if (rhs instanceof CustomUserDetails) {
return getUsername().equals(((CustomUserDetails) rhs).getUsername());
}
return false;
}
/**
* Returns the hashcode of the {@code username}.
*/
@Override
public int hashCode() {
return getUsername().hashCode();
}
}