100 lines
2.3 KiB
Java
100 lines
2.3 KiB
Java
package com.bananasound.server.config.security;
|
|
|
|
import com.fasterxml.jackson.annotation.JsonIgnore;
|
|
import com.bananasound.server.modules.user.entity.UserEntity;
|
|
|
|
import org.springframework.security.core.GrantedAuthority;
|
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Builder;
|
|
import lombok.Data;
|
|
import lombok.NoArgsConstructor;
|
|
|
|
import java.util.Collection;
|
|
import java.util.List;
|
|
import java.util.Objects;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Data
|
|
@AllArgsConstructor
|
|
@NoArgsConstructor
|
|
@Builder
|
|
public class SecurityUserDetails implements UserDetails {
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
private Long id;
|
|
|
|
private String name;
|
|
|
|
private String username;
|
|
|
|
@JsonIgnore
|
|
private String email;
|
|
|
|
@JsonIgnore
|
|
private String password;
|
|
|
|
private Collection<? extends GrantedAuthority> authorities;
|
|
|
|
@Override
|
|
public String getUsername() {
|
|
return username;
|
|
}
|
|
|
|
@Override
|
|
public String getPassword() {
|
|
return password;
|
|
}
|
|
|
|
@Override
|
|
public Collection<? extends GrantedAuthority> getAuthorities() {
|
|
return authorities;
|
|
}
|
|
|
|
@Override
|
|
public boolean isAccountNonExpired() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isAccountNonLocked() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isCredentialsNonExpired() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean isEnabled() {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public boolean equals(Object o) {
|
|
if (this == o)
|
|
return true;
|
|
if (o == null || getClass() != o.getClass())
|
|
return false;
|
|
SecurityUserDetails that = (SecurityUserDetails) o;
|
|
return Objects.equals(id, that.id);
|
|
}
|
|
|
|
@Override
|
|
public int hashCode() {
|
|
return Objects.hash(id);
|
|
}
|
|
|
|
public static SecurityUserDetails create(UserEntity user) {
|
|
List<GrantedAuthority> authorities = user.getRoles().stream()
|
|
.map(role -> new SimpleGrantedAuthority(role.getName().name())).collect(Collectors.toList());
|
|
|
|
return SecurityUserDetails.builder().id(user.getId()).username(user.getUsername()).password(user.getPassword())
|
|
.authorities(authorities).build();
|
|
}
|
|
|
|
}
|