Merge remote-tracking branch 'origin/dev_1.0.0' into dev_1.0.0
commit
19b1e27d19
@ -0,0 +1,18 @@
|
|||||||
|
package com.supervision.knowsub.controller;
|
||||||
|
|
||||||
|
import com.supervision.knowsub.domain.UserInfo;
|
||||||
|
import com.supervision.knowsub.util.UserUtil;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("test")
|
||||||
|
public class TestController {
|
||||||
|
|
||||||
|
@GetMapping("testToken")
|
||||||
|
public void testToken(){
|
||||||
|
UserInfo user = UserUtil.getUser();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,30 +0,0 @@
|
|||||||
/*
|
|
||||||
* 文 件 名: ThreadCache
|
|
||||||
* 版 权:
|
|
||||||
* 描 述: <描述>
|
|
||||||
* 修 改 人: RedName
|
|
||||||
* 修改时间: 2023/9/4
|
|
||||||
* 跟踪单号: <跟踪单号>
|
|
||||||
* 修改单号: <修改单号>
|
|
||||||
* 修改内容: <修改内容>
|
|
||||||
*/
|
|
||||||
package com.supervision.knowsub.config;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <功能详细描述>
|
|
||||||
*
|
|
||||||
* @author ljt
|
|
||||||
* @version [版本号, 2023/9/4]
|
|
||||||
* @see [相关类/方法]
|
|
||||||
* @since [产品/模块版本]
|
|
||||||
*/
|
|
||||||
public class ThreadCache {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 用户ID
|
|
||||||
*/
|
|
||||||
public static final ThreadLocal<String> USER = new ThreadLocal<>();
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,29 +1,45 @@
|
|||||||
package com.supervision.knowsub.util;
|
package com.supervision.knowsub.util;
|
||||||
|
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.json.JSONObject;
|
||||||
import cn.hutool.json.JSONUtil;
|
import cn.hutool.json.JSONUtil;
|
||||||
import com.supervision.knowsub.config.ThreadCache;
|
import cn.hutool.jwt.JWT;
|
||||||
|
import cn.hutool.jwt.JWTUtil;
|
||||||
import com.supervision.knowsub.domain.UserInfo;
|
import com.supervision.knowsub.domain.UserInfo;
|
||||||
import com.supervision.knowsub.exception.BusinessException;
|
import com.supervision.knowsub.exception.BusinessException;
|
||||||
import lombok.Data;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
|
import org.apache.commons.lang3.ObjectUtils;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
public class UserUtil {
|
public class UserUtil {
|
||||||
|
|
||||||
public static UserInfo getUser(){
|
public static UserInfo getUser() {
|
||||||
String userStr = ThreadCache.USER.get();
|
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||||
UserInfo bean = JSONUtil.toBean(userStr, UserInfo.class);
|
if (ObjectUtils.isEmpty(requestAttributes)) {
|
||||||
if (ObjectUtil.isEmpty(bean)){
|
throw new BusinessException("未获取到用户信息");
|
||||||
|
}
|
||||||
|
HttpServletRequest request = requestAttributes.getRequest();
|
||||||
|
|
||||||
|
String token = request.getHeader("token");
|
||||||
|
if (StrUtil.isBlank(token)) {
|
||||||
|
token = (String) request.getAttribute("token");
|
||||||
|
|
||||||
|
}
|
||||||
|
JWT jwt = JWTUtil.parseToken(token);
|
||||||
|
JSONObject claimsJson = jwt.getPayload().getClaimsJson();
|
||||||
|
// String userStr = ThreadCache.USER.get();
|
||||||
|
UserInfo bean = JSONUtil.toBean(claimsJson.toString(), UserInfo.class);
|
||||||
|
if (ObjectUtil.isEmpty(bean)) {
|
||||||
throw new BusinessException("未获取到用户信息");
|
throw new BusinessException("未获取到用户信息");
|
||||||
}
|
}
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getUserToken(){
|
|
||||||
return ThreadCache.USER.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static boolean checkUserPassword(String password,String userPd){
|
public static boolean checkUserPassword(String password, String userPd) {
|
||||||
return SM2Util.verify(password,userPd);
|
return SM2Util.verify(password, userPd);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue