Spring相关之SpringSecurity使用(四)

UserDetails

package com.lee.security.springsecurity;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * 通过认证后的用户信息
 * 
 */
@JsonIgnoreProperties({"authorities"})
public class SpringSecurityUserInfo implements UserDetails {

    private static final long serialVersionUID = -4843919363043160527L;

    private String userName;

    //用户对应的权限字符串
    private List grantedAuthorities;

    //用户是否允许登录
    private boolean isAccountNonExpired;

    //用户是否被锁定
    private boolean isAccountNonLocked;

    //用户证书是否可用
    private boolean isCredentialsNonExpired;

    //用户是否可用
    private boolean isEnabled;


    @Override
    public Collection getAuthorities() {

        return grantedAuthorities;
    }

    //返回密码 如果是SSO 可以直接返回NULL或空 因为单点登录中无需根据密码来验证
    @Override
    public String getPassword() {
        return null;
    }

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

    //用户是否允许登录
    //可以根据后续业务 添加该判断 从数据库中获取当前用户状态
    @Override
    public boolean isAccountNonExpired() {
        return isAccountNonExpired;
    }

    //用户是否被锁定
    //可以根据后续业务 添加该判断 从数据库中获取当前用户状态
    @Override
    public boolean isAccountNonLocked() {

        return isAccountNonLocked;
    }

    //用户证书是否可用
    //可以根据后续业务 添加该判断 从数据库中获取当前用户状态
    @Override
    public boolean isCredentialsNonExpired() {
        return isCredentialsNonExpired;
    }

    //用户是否可用
    //可以根据后续业务 添加该判断 从数据库中获取当前用户状态
    @Override
    public boolean isEnabled() {
        return isEnabled;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public List getGrantedAuthorities() {
        return grantedAuthorities;
    }

    public void setGrantedAuthorities(List grantedAuthorities) {
        this.grantedAuthorities = grantedAuthorities;
    }

    public void setAccountNonExpired(boolean accountNonExpired) {
        isAccountNonExpired = accountNonExpired;
    }

    public void setAccountNonLocked(boolean accountNonLocked) {
        isAccountNonLocked = accountNonLocked;
    }

    public void setCredentialsNonExpired(boolean credentialsNonExpired) {
        isCredentialsNonExpired = credentialsNonExpired;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public SpringSecurityUserInfo(String userName, List grantedAuthorities, boolean isAccountNonExpired, boolean isAccountNonLocked, boolean isCredentialsNonExpired, boolean isEnabled) {
        this.userName = userName;
        this.grantedAuthorities = grantedAuthorities;
        this.isAccountNonExpired = isAccountNonExpired;
        this.isAccountNonLocked = isAccountNonLocked;
        this.isCredentialsNonExpired = isCredentialsNonExpired;
        this.isEnabled = isEnabled;
    }

    public SpringSecurityUserInfo(String userName) {
        this.userName = userName;
    }
}

你可能感兴趣的