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

com.github.rexsheng.springboot.faster.system.auth.domain.SysUserDetail Maven / Gradle / Ivy

The newest version!
package com.github.rexsheng.springboot.faster.system.auth.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.github.rexsheng.springboot.faster.common.constant.CommonConstant;
import com.github.rexsheng.springboot.faster.common.domain.DomainFactory;
import com.github.rexsheng.springboot.faster.system.auth.application.AuthService;
import com.github.rexsheng.springboot.faster.system.auth.application.security.TokenExpireProperties;
import com.github.rexsheng.springboot.faster.util.DateUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.security.core.CredentialsContainer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

@Component
@JsonIgnoreProperties(ignoreUnknown = true)
@ConditionalOnClass(UserDetails.class)
public class SysUserDetail implements UserDetails, CredentialsContainer, Serializable {

    private Long userId;

    private String account;

    private String userName;

    @JsonIgnore
    private String password;

    private String mail;

    private String phone;

    private Integer deptId;

    private List deptIds;

    private List authorities;

    private Date accountLockedTime;

    private Date accountExpiredTime;

    private Date credentialsExpiredTime;

    private List roles;

    private List menus;

    private Integer status;

    private Long tokenId;

    private String tokenBusinessKey;

    private Date lastLoginTime;

    private Date lastPasswordTime;

    private Date loginTime;

    public SysUserDetail() {
    }

    public SysUserDetail(Long userId, String account, String userName, String password){
        this(userId,account,userName,password,null);
    }

    public SysUserDetail(Long userId, String account, String userName, String password, List authorities){
        this(userId, account, userName, password, authorities, CommonConstant.STATUS_RUNNING);
    }

    public SysUserDetail(Long userId, String account, String userName, String password, List authorities, Integer status){
        this.userId=userId;
        this.account=account;
        this.userName=userName;
        this.password=password;
        this.authorities=authorities==null?new ArrayList<>():authorities.stream().map(SimpleGrantedAuthority::new).collect(Collectors.toList());
        this.status=status;
    }

    public boolean validatePassword(String userInputPassword){
        return DomainFactory.create(PasswordEncoder.class).matches(userInputPassword,this.getPassword());
    }

    public static SysUserDetail ofVirtual(String principle, String credentials){
        SysUserDetail user=new SysUserDetail(-1L,principle,principle, credentials);
        return user;
    }

    @Override
    public Collection getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    /**
     * 用户标识ID
     * @return
     */
    @Override
    public String getUsername() {
        return userId.toString();
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return accountExpiredTime==null || DateUtil.toLocalDateTime(accountExpiredTime).isAfter(DateUtil.currentDateTime());
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return accountLockedTime==null || DateUtil.toLocalDateTime(accountLockedTime).isBefore(DateUtil.currentDateTime());
    }

    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return credentialsExpiredTime==null || DateUtil.toLocalDateTime(credentialsExpiredTime).isAfter(DateUtil.currentDateTime());
    }

    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return CommonConstant.STATUS_RUNNING.equals(status);
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getAccount() {
        return account;
    }

    public void setAuthorities(List authorities) {
        this.authorities = authorities;
    }

    public Date getAccountLockedTime() {
        return accountLockedTime;
    }

    public void setAccountLockedTime(Date accountLockedTime) {
        this.accountLockedTime = accountLockedTime;
    }

    public Date getAccountExpiredTime() {
        return accountExpiredTime;
    }

    public void setAccountExpiredTime(Date accountExpiredTime) {
        this.accountExpiredTime = accountExpiredTime;
    }

    public Date getCredentialsExpiredTime() {
        return credentialsExpiredTime;
    }

    public void setCredentialsExpiredTime(Date credentialsExpiredTime) {
        this.credentialsExpiredTime = credentialsExpiredTime;
    }

    @JsonIgnore
    public List getStringAuthorities() {
        return authorities==null?null:authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
    }

    public String getMail() {
        return mail;
    }

    public void setMail(String mail) {
        this.mail = mail;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Integer getDeptId() {
        return deptId;
    }

    public void setDeptId(Integer deptId) {
        this.deptId = deptId;
    }

    public List getDeptIds() {
        return deptIds;
    }

    public void setDeptIds(List deptIds) {
        this.deptIds = deptIds;
    }

    public List getRoles() {
        return roles;
    }

    public void setRoles(List roles) {
        this.roles = roles;
    }

    public List getMenus() {
        return menus;
    }

    public void setMenus(List menus) {
        this.menus = menus;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Long getTokenId() {
        return tokenId;
    }

    public void setTokenId(Long tokenId) {
        this.tokenId = tokenId;
    }

    public String getTokenBusinessKey() {
        return tokenBusinessKey;
    }

    public void setTokenBusinessKey(String tokenBusinessKey) {
        this.tokenBusinessKey = tokenBusinessKey;
    }

    public Date getLoginTime() {
        return loginTime;
    }

    public void setLoginTime(Date loginTime) {
        this.loginTime = loginTime;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public Date getLastPasswordTime() {
        return lastPasswordTime;
    }

    public void setLastPasswordTime(Date lastPasswordTime) {
        this.lastPasswordTime = lastPasswordTime;
    }

    @Override
    public void eraseCredentials() {
        this.password=null;
    }

    @JsonIgnore
    public Boolean isRefreshTokenSupport(){
        return DomainFactory.create(TokenExpireProperties.class).getRefreshExpires().getSeconds()>0;
    }
    public Map createSession(){
        return DomainFactory.create(AuthService.class).createSessionToken(this);
    }

    public static class SysUserMenu {

        private Integer menuId;

        private String menuName;

        private Integer menuType;

        private Integer menuOrder;

        private String menuIcon;

        private String menuPath;

        private String menuPathQuery;

        private String component;

        private Integer parentId;

        private Boolean visible;

        private Boolean frame;

        private Boolean cache;

        private Boolean fullscreen;

        private Boolean tag;

        private List children;

        public Integer getMenuId() {
            return menuId;
        }

        public void setMenuId(Integer menuId) {
            this.menuId = menuId;
        }

        public String getMenuName() {
            return menuName;
        }

        public void setMenuName(String menuName) {
            this.menuName = menuName;
        }

        public Integer getMenuType() {
            return menuType;
        }

        public void setMenuType(Integer menuType) {
            this.menuType = menuType;
        }

        public Integer getMenuOrder() {
            return menuOrder;
        }

        public void setMenuOrder(Integer menuOrder) {
            this.menuOrder = menuOrder;
        }

        public String getMenuIcon() {
            return menuIcon;
        }

        public void setMenuIcon(String menuIcon) {
            this.menuIcon = menuIcon;
        }

        public String getMenuPath() {
            return menuPath;
        }

        public void setMenuPath(String menuPath) {
            this.menuPath = menuPath;
        }

        public String getMenuPathQuery() {
            return menuPathQuery;
        }

        public void setMenuPathQuery(String menuPathQuery) {
            this.menuPathQuery = menuPathQuery;
        }

        public String getComponent() {
            return component;
        }

        public void setComponent(String component) {
            this.component = component;
        }

        public Integer getParentId() {
            return parentId;
        }

        public void setParentId(Integer parentId) {
            this.parentId = parentId;
        }

        public Boolean getVisible() {
            return visible;
        }

        public void setVisible(Boolean visible) {
            this.visible = visible;
        }

        public Boolean getFrame() {
            return frame;
        }

        public void setFrame(Boolean frame) {
            this.frame = frame;
        }

        public Boolean getCache() {
            return cache;
        }

        public void setCache(Boolean cache) {
            this.cache = cache;
        }

        public Boolean getFullscreen() {
            return fullscreen;
        }

        public void setFullscreen(Boolean fullscreen) {
            this.fullscreen = fullscreen;
        }

        public Boolean getTag() {
            return tag;
        }

        public void setTag(Boolean tag) {
            this.tag = tag;
        }

        public List getChildren() {
            return children;
        }

        public void setChildren(List children) {
            this.children = children;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy