Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, * Version 2.0 (the "License"); you may not use this file except in * compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kunckle.jetpower.core.user.entity; import com.kunckle.jetpower.core.base.entity.BaseEntity; import org.springframework.security.core.CredentialsContainer; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.SpringSecurityCoreVersion; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.util.Assert; import javax.persistence.*; import java.io.Serializable; import java.util.*; /** * */ @MappedSuperclass public class User extends BaseEntity<Integer> implements UserDetails, CredentialsContainer { private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID; @Column(name = "password") private String password; @Column(name = "username", nullable = false, unique = true) private String username; @Column(name = "registered", updatable = false) private Date userRegistered; @Column(name = "accountNonExpired", nullable = false) private boolean accountNonExpired; @Column(name = "accountNonLocked", nullable = false) private boolean accountNonLocked; @Column(name = "credentialsNonExpired", nullable = false) private boolean credentialsNonExpired; @Column(name = "enabled", nullable = false) private boolean enabled; @ManyToMany(fetch = FetchType.LAZY, targetEntity = UserAuthority.class) @JoinTable(name = "user_to_authorities") private Collection<? extends GrantedAuthority> authorities = new HashSet<>(); /** * non-public */ public User() { } /** * Calls the more complex constructor with all boolean arguments set to {@code true}. */ public User(String username, String password, Date userRegistered, Collection<? extends GrantedAuthority> authorities, Integer id) { this(username, password, userRegistered, true, true, true, true, authorities, id); } /** * Construct the <code>User</code> with the details required by * {@link org.springframework.security.authentication.dao.DaoAuthenticationProvider}. * * @param username the username presented to the * <code>DaoAuthenticationProvider</code> * @param password the password that should be presented to the * <code>DaoAuthenticationProvider</code> * @param enabled set to <code>true</code> if the user is enabled * @param accountNonExpired set to <code>true</code> if the account has not * expired * @param credentialsNonExpired set to <code>true</code> if the credentials * have not expired * @param accountNonLocked set to <code>true</code> if the account is not * locked * @param authorities the authorities that should be granted to the caller * if they presented the correct username and password and the user * is enabled. Not null. * @throws IllegalArgumentException if a <code>null</code> value was passed * either as a parameter or as an element in the * <code>GrantedAuthority</code> collection */ public User(String username, String password, Date userRegistered, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities, Integer id) { 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.userRegistered = userRegistered; this.enabled = enabled; this.accountNonExpired = accountNonExpired; this.credentialsNonExpired = credentialsNonExpired; this.accountNonLocked = accountNonLocked; this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities)); this.setId(id); } 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.getUserSecurityRoles() contract and SEC-717) SortedSet<GrantedAuthority> sortedAuthorities = new TreeSet<>(new AuthorityComparator()); for (GrantedAuthority grantedAuthority : authorities) { Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements"); sortedAuthorities.add(grantedAuthority); } return sortedAuthorities; } /** * ??? * * @return ??? */ @Override public Collection<? extends GrantedAuthority> getAuthorities() { return authorities; } public void setAuthorities(Collection<? extends GrantedAuthority> userSecurityRoles) { this.authorities = userSecurityRoles; } /** * ?? * * @return ? */ @Override public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } /** * ??? * * @return ?? */ @Override public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } /** * ?? * * @return true - false - ? */ @Override public boolean isEnabled() { return enabled; } public void setEnabled(boolean enabled) { this.enabled = enabled; } /** * ? * * @return true - false - ? */ @Override public boolean isAccountNonExpired() { return accountNonExpired; } public void setAccountNonExpired(boolean accountNonExpired) { this.accountNonExpired = accountNonExpired; } /** * ?? * * @return true - false - ? */ @Override public boolean isAccountNonLocked() { return accountNonLocked; } public void setAccountNonLocked(boolean accountNonLocked) { this.accountNonLocked = accountNonLocked; } /** * ?? * * @return true - false - ? */ @Override public boolean isCredentialsNonExpired() { return credentialsNonExpired; } public void setCredentialsNonExpired(boolean credentialsNonExpired) { this.credentialsNonExpired = credentialsNonExpired; } /** * ? */ @Override public void eraseCredentials() { password = null; } /** * ? * * @return */ public Date getUserRegistered() { return userRegistered; } public void setUserRegistered(Date userRegistered) { this.userRegistered = userRegistered; } /** * Returns {@code true} if the supplied object is a {@code User} 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. */ @Override public boolean equals(Object rhs) { return rhs instanceof User && username.equals(((User) rhs).username); } /** * Returns the hashcode of the {@code username}. */ @Override public int hashCode() { return username.hashCode(); } @Override 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(); } private static class AuthorityComparator implements Comparator<GrantedAuthority>, 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()); } } }