init
parent
d4b4cb637e
commit
51966631fa
@ -1,84 +0,0 @@
|
|||||||
package com.supervision.config;
|
|
||||||
|
|
||||||
import cn.hutool.core.util.StrUtil;
|
|
||||||
import cn.hutool.json.JSONObject;
|
|
||||||
import cn.hutool.json.JSONUtil;
|
|
||||||
import cn.hutool.jwt.JWT;
|
|
||||||
import cn.hutool.jwt.JWTUtil;
|
|
||||||
import com.supervision.exception.BusinessException;
|
|
||||||
import com.supervision.util.TokenUtil;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
import org.springframework.web.servlet.HandlerInterceptor;
|
|
||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
@Slf4j
|
|
||||||
public class JwtInterceptor implements HandlerInterceptor {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
|
||||||
//请求消息头获取用户ID
|
|
||||||
String token = request.getHeader("token");
|
|
||||||
if (StrUtil.isBlank(token)) {
|
|
||||||
// 如果是swagger来的接口,说明这里是测试的,会伪造一个用户
|
|
||||||
if (StrUtil.isNotBlank(request.getHeader("Knife4j-Gateway-Code"))) {
|
|
||||||
cacheAuth(JWTUtil.parseToken(devActiveUser()));
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
throw new BusinessException("当前用户未登录", HttpStatus.UNAUTHORIZED.value());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
JWT jwt = JWTUtil.parseToken(token);
|
|
||||||
// 校验token是否过期,如果过期了,需要提示过期重新登录
|
|
||||||
checkTokenExpire(jwt);
|
|
||||||
cacheAuth(jwt);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,
|
|
||||||
@Nullable Exception ex) throws Exception {
|
|
||||||
// 请求结束,将用户信息从thread中移除
|
|
||||||
clearAuth();
|
|
||||||
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void checkTokenExpire(JWT jwt) {
|
|
||||||
Object expireTime = jwt.getPayload("expireTime");
|
|
||||||
long l = Long.parseLong(String.valueOf(expireTime));
|
|
||||||
// 校验是否比当前时间大
|
|
||||||
long currentTimeMillis = System.currentTimeMillis();
|
|
||||||
if (currentTimeMillis > l) {
|
|
||||||
throw new BusinessException("用户登录已过期,请重新登录", HttpStatus.UNAUTHORIZED.value());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private void cacheAuth(JWT jwt) {
|
|
||||||
try {
|
|
||||||
JSONObject claimsJson = jwt.getPayload().getClaimsJson();
|
|
||||||
ThreadCache.USER.set(claimsJson.toString());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("用户信息异常", e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private String devActiveUser() {
|
|
||||||
Map<String, Object> map = new HashMap<>();
|
|
||||||
map.put("id", "1");
|
|
||||||
map.put("account", "test");
|
|
||||||
map.put("name", "测试账户");
|
|
||||||
return TokenUtil.creatToken(JSONUtil.toJsonStr(map));
|
|
||||||
}
|
|
||||||
|
|
||||||
private void clearAuth() {
|
|
||||||
ThreadCache.USER.remove();
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,62 @@
|
|||||||
|
package com.supervision.config;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
|
import cn.dev33.satoken.filter.SaServletFilter;
|
||||||
|
import cn.dev33.satoken.router.SaRouter;
|
||||||
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
|
import cn.dev33.satoken.util.SaResult;
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.util.ArrayUtil;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class SaTokenConfigure {
|
||||||
|
|
||||||
|
public String[] ignorePathPatterns() {
|
||||||
|
List<String> paths = new ArrayList<>();
|
||||||
|
paths.add("/swagger-resources/**");
|
||||||
|
paths.add("/webjars/**");
|
||||||
|
paths.add("/v2/**");
|
||||||
|
paths.add("/swagger-ui.html/**");
|
||||||
|
paths.add("/doc.html/**");
|
||||||
|
paths.add("/error");
|
||||||
|
paths.add("/favicon.ico");
|
||||||
|
paths.add("/user/login");
|
||||||
|
return ArrayUtil.toArray(paths,String.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注册 [Sa-Token全局过滤器]
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public SaServletFilter getSaServletFilter() {
|
||||||
|
return new SaServletFilter()
|
||||||
|
|
||||||
|
// 指定 拦截路由 与 放行路由
|
||||||
|
.addInclude("/**")
|
||||||
|
// 排除掉部分不需要登录的路由
|
||||||
|
.addExclude(ignorePathPatterns())
|
||||||
|
|
||||||
|
// 认证函数: 每次请求执行
|
||||||
|
.setAuth(obj -> {
|
||||||
|
System.out.println("---------- 进入Sa-Token全局认证 -----------");
|
||||||
|
// 登录认证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
|
||||||
|
SaRouter.match("/**", "/user/doLogin", StpUtil::checkLogin);
|
||||||
|
|
||||||
|
// 更多拦截处理方式,请参考“路由拦截式鉴权”章节 */
|
||||||
|
})
|
||||||
|
|
||||||
|
// 异常处理函数:每次认证函数发生异常时执行此函数
|
||||||
|
.setError(e -> {
|
||||||
|
System.out.println("---------- 进入Sa-Token异常处理 -----------");
|
||||||
|
return SaResult.error(e.getMessage());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,58 +0,0 @@
|
|||||||
/*
|
|
||||||
* 文 件 名: CustomException
|
|
||||||
* 版 权:
|
|
||||||
* 描 述: <描述>
|
|
||||||
* 修 改 人: RedName
|
|
||||||
* 修改时间: 2022/8/5
|
|
||||||
* 跟踪单号: <跟踪单号>
|
|
||||||
* 修改单号: <修改单号>
|
|
||||||
* 修改内容: <修改内容>
|
|
||||||
*/
|
|
||||||
package com.supervision.exception;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* <功能详细描述>
|
|
||||||
* 自定义异常
|
|
||||||
*
|
|
||||||
* @author ljt
|
|
||||||
* @version [版本号, 2022/8/5]
|
|
||||||
* @see [相关类/方法]
|
|
||||||
* @since [产品/模块版本]
|
|
||||||
*/
|
|
||||||
@Slf4j
|
|
||||||
public class HumanException extends RuntimeException {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 房间状态获取异常时,前端刷新房间,重新进入
|
|
||||||
*/
|
|
||||||
private static final Integer HUMAN_ERROR = 6001;
|
|
||||||
/**
|
|
||||||
* 异常编码
|
|
||||||
*/
|
|
||||||
private final Integer code;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 异常信息
|
|
||||||
*/
|
|
||||||
private final String message;
|
|
||||||
|
|
||||||
private HumanException(Integer code, String message) {
|
|
||||||
this.code = code;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static HumanException humanError(String message) {
|
|
||||||
return new HumanException(HUMAN_ERROR, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Integer getCode() {
|
|
||||||
return code;
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue