package com.cnbm.admin.entity; import java.io.Serializable; import java.util.Collection; import java.util.List; import java.util.stream.Collectors; import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UserDetails; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Data; import lombok.NoArgsConstructor; /** * @Author weihongyang * @Date 2022/6/8 1:56 PM * @Version 1.0 */ @Data @NoArgsConstructor @JsonIgnoreProperties(ignoreUnknown = true) public class LoginUser implements UserDetails,Serializable { private static final long serialVersionUID = 1L; private SysUserEntity sysUserEntity; private List permissions; public LoginUser(SysUserEntity sysUserEntity, List permissions) { this.sysUserEntity = sysUserEntity; this.permissions = permissions; } @JsonIgnore private List authorities; @Override public Collection getAuthorities() { if(authorities!=null){ return authorities; } //把permissions中String类型的权限信息封装成SimpleGrantedAuthority对象 // authorities = new ArrayList<>(); // for (String permission : permissions) { // SimpleGrantedAuthority authority = new SimpleGrantedAuthority(permission); // authorities.add(authority); // } authorities = permissions.stream() .map(SimpleGrantedAuthority::new) .collect(Collectors.toList()); return authorities; } @Override public String getPassword() { return sysUserEntity.getPassword(); } @Override public String getUsername() { return sysUserEntity.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; } }