0
我有這個奇怪的場景 - 我有這個PlatformUser
類實現Principal
getName()
,返回它的email
。這是授權所必需的。我希望能夠基於公共屬性name
和email
對PlatformUser
進行序列化和反序列化。我應該如何註釋我的課程,以使其工作。作爲解決方法,我必須將屬性name
更改爲fullName
,但這違反了我的問題的目的。謝謝!公共財產在json序列化中被公共getter所掩蓋
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import javax.persistence.*;
import javax.security.auth.Subject;
import java.security.Principal;
import java.util.*;
@Entity
public class PlatformUser implements Principal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
public String name;
public String email;
public String role = getUserRole().getRole().toString().toLowerCase();
@Transient
private List<GrantedAuthority> authorities;
public PlatformUser(){}
public PlatformUser(String email, List<GrantedAuthority> authorities) {
this.email = email;
this.authorities = authorities;
}
@Override
public String getName() {
return email;
}
@Override
public boolean implies(Subject subject) {
return false;
}
public static PlatformUser create(String email, List<GrantedAuthority> authorities) {
if (StringUtils.isBlank(email)) throw new IllegalArgumentException("Email is blank: " + email);
return new PlatformUser(email, authorities);
}
public Set<GrantedAuthority> getAuthorities() {
UserRole role = this.getUserRole();
Set<GrantedAuthority> authorities = new HashSet<>();
authorities.add(new SimpleGrantedAuthority(role.getRole().authority()));
return authorities;
}
public void setAuthorities(List<GrantedAuthority> authorities) {
this.authorities = authorities;
}
public UserRole getUserRole() {
return new UserRole(id, Role.ADMIN);
}
}