149 lines
3.8 KiB
Java
149 lines
3.8 KiB
Java
package com.changhu.common.utils;
|
|
|
|
import cn.dev33.satoken.stp.SaLoginConfig;
|
|
import cn.dev33.satoken.stp.SaTokenInfo;
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.crypto.SecureUtil;
|
|
import com.changhu.common.annotation.UserType;
|
|
import com.changhu.common.enums.ResultCode;
|
|
import com.changhu.common.exception.MessageException;
|
|
import com.changhu.enums.ClientType;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
import java.util.Optional;
|
|
|
|
/**
|
|
* author: luozhun
|
|
* desc: some...
|
|
* createTime: 2023/12/15 11:30
|
|
*/
|
|
@Slf4j
|
|
public class UserUtil {
|
|
|
|
public static final String DEFAULT_PASSWORD = "123456";
|
|
|
|
public static final Long ERROR_USER_ID = -1L;
|
|
|
|
/**
|
|
* 用户登录 保存信息
|
|
*
|
|
* @param userId 用户id
|
|
*/
|
|
public static void login(Long userId) {
|
|
StpUtil.login(userId);
|
|
}
|
|
|
|
/**
|
|
* 登录并且返回SaTokenInfo
|
|
*
|
|
* @param userId 用户id
|
|
* @return SaTokenInfo
|
|
*/
|
|
public static SaTokenInfo loginAndTokenInfo(Long userId) {
|
|
login(userId);
|
|
return getTokenInfo();
|
|
}
|
|
|
|
public static SaTokenInfo loginAndTokenInfo(Long userId, UserType userType, Long unitId) {
|
|
StpUtil.login(userId, SaLoginConfig.setExtra("userType", userType).setExtra("unitId", unitId));
|
|
return getTokenInfo();
|
|
}
|
|
|
|
/**
|
|
* 获取客户端类型
|
|
*/
|
|
public static UserType getUserType() {
|
|
Object clientType = StpUtil.getExtra("userType");
|
|
if (clientType instanceof String ct) {
|
|
return UserType.valueOf(ct);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* 获取单位id
|
|
*/
|
|
public static Long getUnitId() {
|
|
return Long.parseLong(StpUtil.getExtra("unitId") + "");
|
|
}
|
|
|
|
/**
|
|
* 用户登出
|
|
*/
|
|
public static void logout() {
|
|
StpUtil.logout();
|
|
}
|
|
|
|
/**
|
|
* 获取用户id
|
|
*/
|
|
public static Long getUserId() {
|
|
return StpUtil.getLoginIdAsLong();
|
|
}
|
|
|
|
/**
|
|
* 获取当前用户token
|
|
*
|
|
* @return token
|
|
*/
|
|
public static String getToken() {
|
|
return StpUtil.getTokenValue();
|
|
}
|
|
|
|
/**
|
|
* 根据token获取id
|
|
*
|
|
* @param token token
|
|
* @return id
|
|
*/
|
|
public static Long getUserIdByToken(String token) {
|
|
return Long.parseLong(Optional.ofNullable(StpUtil.getLoginIdByToken(token))
|
|
.map(Object::toString)
|
|
.orElseThrow(() -> new MessageException(ResultCode.NOT_TOKEN)));
|
|
}
|
|
|
|
/**
|
|
* 获取token信息
|
|
*
|
|
* @return token信息
|
|
*/
|
|
public static SaTokenInfo getTokenInfo() {
|
|
return StpUtil.getTokenInfo();
|
|
}
|
|
|
|
/**
|
|
* 加密明文密码
|
|
*
|
|
* @param plainTextPassWord 明文密码
|
|
* @return 盐 + 加密后的密码
|
|
*/
|
|
public static String passWordEncrypt(String plainTextPassWord) {
|
|
//随机盐值
|
|
String salt = IdUtil.simpleUUID();
|
|
//生成密文密码
|
|
String ciphertextPassWord = SecureUtil.md5(salt + plainTextPassWord);
|
|
return salt + "$$" + ciphertextPassWord;
|
|
}
|
|
|
|
/**
|
|
* 解密密码 校验密码是否正确
|
|
*
|
|
* @param plainTextPassWord 明文密码
|
|
* @param salt 盐
|
|
* @param ciphertextPassWord 密文密码
|
|
* @return 比对结果
|
|
*/
|
|
public static boolean verifyPassWord(String plainTextPassWord, String salt, String ciphertextPassWord) {
|
|
boolean result = false;
|
|
if (StrUtil.isAllNotEmpty(plainTextPassWord, salt, ciphertextPassWord)) {
|
|
// 使用同样的加密算法和随机盐值生成最终加密的密码
|
|
if (StrUtil.equals(SecureUtil.md5(salt + plainTextPassWord), ciphertextPassWord)) {
|
|
result = true;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|