All Downloads are FREE. Search and download functionalities are using the official Maven repository.

top.zenyoung.security.webflux.model.TokenUserDetails Maven / Gradle / Ivy

There is a newer version: 2.0.2
Show newest version
package top.zenyoung.security.webflux.model;

import com.google.common.base.Strings;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;
import top.zenyoung.common.model.Status;
import top.zenyoung.security.model.UserPrincipal;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 令牌用户认证数据
 *
 * @author yangyong
 * @version 1.0
 *  2020/3/19 4:25 下午
 **/
@Data
@EqualsAndHashCode(callSuper = true)
public class TokenUserDetails extends UserPrincipal implements UserDetails {
    /**
     * 密码
     */
    private String password;
    /**
     * 状态
     */
    private Status status = Status.Enable;

    public TokenUserDetails(@Nonnull final UserPrincipal principal, @Nullable final String password, @Nullable final Status status) {
        BeanUtils.copyProperties(principal, this);
        if (!Strings.isNullOrEmpty(password)) {
            setPassword(password);
        }
        if (status != null) {
            setStatus(status);
        }
    }

    public TokenUserDetails(@Nonnull final UserPrincipal principal) {
        this(principal, null, null);
    }

    @Override
    public Collection getAuthorities() {
        final List roles = getRoles();
        if (!CollectionUtils.isEmpty(roles)) {
            return roles.stream()
                    .filter(role -> !Strings.isNullOrEmpty(role))
                    .map(SimpleGrantedAuthority::new)
                    .collect(Collectors.toList());
        }
        return null;
    }

    @Override
    public String getUsername() {
        return getAccount();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return getStatus() != null && Status.Enable == this.getStatus();
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy