Guest User

Untitled

a guest
Feb 4th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.40 KB | None | 0 0
  1. package org.mm.MBlog.security.services;
  2.  
  3. import java.util.Collection;
  4. import java.util.List;
  5. import java.util.Objects;
  6. import java.util.stream.Collectors;
  7.  
  8. import org.springframework.security.core.GrantedAuthority;
  9. import org.springframework.security.core.authority.SimpleGrantedAuthority;
  10. import org.springframework.security.core.userdetails.UserDetails;
  11.  
  12. import org.mm.MBlog.security.models.User;
  13. import com.fasterxml.jackson.annotation.JsonIgnore;
  14.  
  15. public class UserDetailsImpl implements UserDetails {
  16.     private static final long serialVersionUID = 1L;
  17.     private Long id;
  18.     private String username;
  19.     private String email;
  20.     @JsonIgnore
  21.     private String password;
  22.  
  23.     private Collection<? extends GrantedAuthority> authorities;
  24.  
  25.     public UserDetailsImpl(Long id,
  26.                            String username,
  27.                            String email,
  28.                            String password,
  29.                            Collection<? extends GrantedAuthority> authorities){
  30.         this.id = id;
  31.         this.username = username;
  32.         this.email = email;
  33.         this.password = password;
  34.         this.authorities = authorities;
  35.     }
  36.  
  37.     public static UserDetailsImpl build(User user) {
  38.         List<GrantedAuthority> authorities = user.getRoles().stream().map(role -> new SimpleGrantedAuthority(role.getName().name())).collect(Collectors.toList());
  39.         return new UserDetailsImpl(user.getId(), user.getUsername(), user.getEmail(), user.getPassword(), authorities);
  40.     }
  41.  
  42.     @Override
  43.     public Collection<? extends GrantedAuthority> getAuthorities() { return authorities; }
  44.  
  45.     public Long getId() { return id; }
  46.  
  47.     public String getEmail() { return email; }
  48.  
  49.     @Override
  50.     public String getPassword() { return password; }
  51.  
  52.      @Override
  53.     public String getUsername() { return username; }
  54.  
  55.     @Override
  56.     public boolean isAccountNonExpired() { return true; }
  57.  
  58.     @Override
  59.     public boolean isAccountNonLocked() { return true; }
  60.  
  61.     @Override
  62.     public boolean isCredentialsNonExpired() { return true; }
  63.  
  64.     @Override
  65.     public boolean isEnabled() { return true; }
  66.  
  67.     @Override
  68.     public boolean equals(Object o) {
  69.         if (this == o){ return true; }
  70.         if (o == null || getClass() != o.getClass()){ return false; }
  71.  
  72.         UserDetailsImpl user = (UserDetailsImpl) o;
  73.         return Objects.equals(id, user.id);
  74.     }
  75. }
  76.  
Add Comment
Please, Sign In to add comment