package es.alvsanand.webpage.model.security; import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; public class UserDetailsImpl implements UserDetails { /** * */ private static final long serialVersionUID = -257637803931652165L; private String password; private final String username; private final Set<GrantedAuthority> authorities; private boolean authenticated; public UserDetailsImpl(String username, String password, 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.authorities = Collections.unmodifiableSet(sortAuthorities(authorities)); } public Collection<GrantedAuthority> getAuthorities() { return authorities; } public String getPassword() { return password; } public String getUsername() { return username; } public boolean isAuthenticated() { return authenticated; } public void setAuthenticated(boolean authenticated) throws IllegalArgumentException { this.authenticated = authenticated; } public String getName() { return username; } private static SortedSet<GrantedAuthority> sortAuthorities(Collection<? extends GrantedAuthority> authorities) { if(authorities==null) throw new IllegalArgumentException("Cannot pass a null GrantedAuthority collection"); SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<GrantedAuthority>(new AuthorityComparator()); for (GrantedAuthority grantedAuthority : authorities) { if(grantedAuthority==null) throw new IllegalArgumentException("GrantedAuthority list cannot contain any null elements"); sortedAuthorities.add(grantedAuthority); } return sortedAuthorities; } private static class AuthorityComparator implements Comparator<GrantedAuthority>, Serializable { /** * */ private static final long serialVersionUID = 6478555450565907178L; 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()); } } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + (authenticated ? 1231 : 1237); result = prime * result + ((authorities == null) ? 0 : authorities.hashCode()); result = prime * result + ((password == null) ? 0 : password.hashCode()); result = prime * result + ((username == null) ? 0 : username.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } UserDetailsImpl other = (UserDetailsImpl) obj; if (authenticated != other.authenticated) { return false; } if (authorities == null) { if (other.authorities != null) { return false; } } else if (!authorities.equals(other.authorities)) { return false; } if (password == null) { if (other.password != null) { return false; } } else if (!password.equals(other.password)) { return false; } if (username == null) { if (other.username != null) { return false; } } else if (!username.equals(other.username)) { return false; } return true; } @Override public String toString() { return "UserDetailsImpls [password=" + password + ", username=" + username + ", authorities=" + authorities + ", authenticated=" + authenticated + "]"; } }