| 20 | import java.util.Collections; |
| 21 | |
| 22 | @Data |
| 23 | @Builder |
| 24 | @NoArgsConstructor |
| 25 | @AllArgsConstructor |
| 26 | public class UserPrincipal implements UserDetails { |
| 27 | |
| 28 | private Long id; |
| 29 | private String username; |
| 30 | private String password; |
| 31 | private String email; |
| 32 | private String role; |
| 33 | private String preferredModel; |
| 34 | private Collection<? extends GrantedAuthority> authorities; |
| 35 | |
| 36 | /** |
| 37 | * 从 User 实体创建 UserPrincipal |
| 38 | */ |
| 39 | public static UserPrincipal create(User user) { |
| 40 | return UserPrincipal.builder() |
| 41 | .id(user.getId()) |
| 42 | .username(user.getUsername()) |
| 43 | .password(user.getPassword()) |
| 44 | .email(user.getEmail()) |
| 45 | .role(user.getRole().name()) |
| 46 | .preferredModel(user.getPreferredModel()) |
| 47 | .authorities(Collections.singletonList( |
| 48 | new SimpleGrantedAuthority("ROLE_" + user.getRole().name()) |
| 49 | )) |
| 50 | .build(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public Collection<? extends GrantedAuthority> getAuthorities() { |
| 55 | return authorities; |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String getPassword() { |
| 60 | return password; |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public String getUsername() { |
| 65 | return username; |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public boolean isAccountNonExpired() { |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public boolean isAccountNonLocked() { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public boolean isCredentialsNonExpired() { |
nothing calls this directly
no outgoing calls
no test coverage detected