package com.dsh.utils.login;
|
|
import cn.mb.cloud.common.core.exception.BusinessException;
|
import com.dsh.constant.AuthConstants;
|
import com.dsh.constant.MsgConstants;
|
import com.dsh.upms.model.bo.UserDetailBo;
|
import com.dsh.upms.model.vo.LoginUserVo;
|
import com.dsh.utils.CacheUtils;
|
import com.dsh.utils.DateUtils;
|
import com.dsh.utils.ObjectUtils;
|
import com.dsh.utils.ServletUtils;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.util.Assert;
|
|
import javax.servlet.http.HttpServletRequest;
|
import java.lang.reflect.Type;
|
import java.time.LocalDateTime;
|
import java.util.UUID;
|
|
/**
|
* * @author: lihong .
|
* * @email: 765907456@qq.com .
|
* * @createTime: 19-7-15 上午11:44 .
|
* * description: 登录帮助工具.
|
**/
|
@Slf4j
|
public final class LoginHelper {
|
/**
|
* admin后台操作记录userId Key
|
*/
|
public static final String ADMINUSERID = "adminUserId";
|
|
/**
|
* 获取用户.
|
*
|
* @return
|
*/
|
public static LoginUserVo getUser(String authorization) {
|
LoginUserVo userByCache = getUserByCache(authorization);
|
if (userByCache != null) {
|
return userByCache;
|
}
|
return null;
|
}
|
|
/**
|
* 转换
|
*
|
* @param convert
|
* @param types
|
* @param <T>
|
* @return
|
*/
|
public static <T> T convert(Convert<T> convert, Type... types) {
|
return convert.convert(ObjectUtils.newInstance((Class) ObjectUtils.buildType(types)));
|
}
|
|
/**
|
* 获取当前推送设备号.
|
* @return
|
*/
|
public static String getCurrentPushDevice(){
|
HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
|
String jPushDevice = httpServletRequest.getHeader(AuthConstants.J_PUSH_DEVICE);
|
return jPushDevice;
|
}
|
|
/**
|
* token生成.
|
*
|
* @return
|
*/
|
public static String tokenGenerate() {
|
return UUID.randomUUID().toString().replace("-", "");
|
}
|
|
/**
|
* 更新用户.
|
*/
|
public static void update(LoginUserVo loginUserVo, long expire) {
|
Assert.notNull(loginUserVo, "用户信息不能为空.");
|
log.info("{}更新了token缓存时间", loginUserVo.getName());
|
CacheUtils.put(loginUserVo.getToken(), loginUserVo, expire, CacheUtils.CacheTimeUnit.MILLISECONDS);
|
}
|
|
public static boolean isExpired(LoginUserVo loginUserVo) {
|
Assert.notNull(loginUserVo, "用户信息不能为空.");
|
//更新缓存中用户信息
|
long diff = DateUtils.diff(loginUserVo.getStartTime(), LocalDateTime.now());
|
if (diff < loginUserVo.getExpire()) {
|
return true;
|
} else {
|
CacheUtils.delete(loginUserVo.getToken());
|
return false;
|
}
|
}
|
|
/**
|
* 添加用户
|
*
|
* @param userBo
|
* @param token
|
* @param expire
|
*/
|
public static LoginUserVo addUser(UserDetailBo userBo, String token, Long expire) {
|
Assert.notNull(userBo, "用户信息不能为空.");
|
Assert.hasText(token, "令牌不能为空.");
|
Assert.notNull(expire, "过期时间不能为空");
|
LoginUserVo loginUserVo = covertUser(userBo, null);
|
loginUserVo.setToken(token);
|
loginUserVo.setExpire(expire);
|
loginUserVo.setStartTime(LocalDateTime.now());
|
CacheUtils.put(token, loginUserVo, expire, CacheUtils.CacheTimeUnit.MILLISECONDS);
|
return loginUserVo;
|
}
|
|
|
/**
|
* 移除用户信息
|
*/
|
public static void remove() {
|
String authorization = getCurrentUserToken();
|
CacheUtils.delete(authorization);
|
}
|
|
public static void remove(String tomke) {
|
CacheUtils.delete(tomke);
|
}
|
|
/**
|
* 用户是否存在.
|
*
|
* @param token
|
* @return
|
*/
|
public static boolean userExist(String token) {
|
if (StringUtils.isNotEmpty(token)) {
|
return CacheUtils.exist(token);
|
} else {
|
return false;
|
}
|
}
|
|
/**
|
* 查询当前用户是否存在.
|
*
|
* @return
|
*/
|
public static boolean userExist() throws BusinessException {
|
String authorization = getCurrentUserToken();
|
if (StringUtils.isEmpty(authorization)) {
|
throw new BusinessException(MsgConstants.USER_NOT_HAS_AUTH);
|
}
|
|
return userExist(authorization);
|
}
|
|
/**
|
* 获取用户当前的设备号
|
*
|
* @return
|
*/
|
public static String getCurrentDevice() {
|
HttpServletRequest request = ServletUtils.getHttpServletRequest();
|
return request.getHeader(AuthConstants.DEVICE_HEADER);
|
}
|
|
/**
|
* 设备号是否存在.
|
*
|
* @return
|
*/
|
public static boolean deviceExist() {
|
return true;
|
}
|
|
/**
|
* 转换用户信息.
|
*
|
* @param userBo
|
* @param loginUserVo
|
* @return
|
*/
|
public static LoginUserVo covertUser(UserDetailBo userBo, LoginUserVo loginUserVo) {
|
if (loginUserVo == null) {
|
loginUserVo = new LoginUserVo();
|
}
|
loginUserVo.setUsername(userBo.getUsername())
|
.setName(userBo.getName())
|
.setLockFlag(userBo.getLockFlag())
|
.setId(userBo.getId());
|
return loginUserVo;
|
}
|
|
|
/**
|
* 从缓存中获取用户
|
*
|
* @param token
|
* @return
|
*/
|
public static LoginUserVo getUserByCache(String token) {
|
if (StringUtils.isNotEmpty(token)) {
|
Boolean hasUser = CacheUtils.exist(token);
|
if (hasUser != null && hasUser) {
|
return CacheUtils.get(token);
|
}
|
}
|
return null;
|
}
|
|
/**
|
* 获取当前用户token
|
*
|
* @return
|
*/
|
private static String getCurrentUserToken() {
|
try {
|
HttpServletRequest request = ServletUtils.getHttpServletRequest();
|
//获取是否存在
|
String authorization = request.getHeader(AuthConstants.AUTH_HEADER);
|
return authorization;
|
}catch (Exception e){
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
|
}
|