|
|
@ -2,14 +2,21 @@ package com.glxp.udi.admin.config.saToken;
|
|
|
|
|
|
|
|
|
|
|
|
import cn.dev33.satoken.config.SaTokenConfig;
|
|
|
|
import cn.dev33.satoken.config.SaTokenConfig;
|
|
|
|
import cn.dev33.satoken.context.SaHolder;
|
|
|
|
import cn.dev33.satoken.context.SaHolder;
|
|
|
|
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
|
|
|
|
|
|
|
import cn.dev33.satoken.exception.NotPermissionException;
|
|
|
|
import cn.dev33.satoken.filter.SaServletFilter;
|
|
|
|
import cn.dev33.satoken.filter.SaServletFilter;
|
|
|
|
import cn.dev33.satoken.router.SaRouter;
|
|
|
|
import cn.dev33.satoken.router.SaRouter;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
import cn.dev33.satoken.stp.StpUtil;
|
|
|
|
import cn.dev33.satoken.util.SaResult;
|
|
|
|
import cn.dev33.satoken.util.SaResult;
|
|
|
|
|
|
|
|
import cn.hutool.http.ContentType;
|
|
|
|
|
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
|
|
|
|
|
import com.glxp.udi.admin.common.enums.ResultEnum;
|
|
|
|
|
|
|
|
import com.glxp.udi.admin.common.util.ResultVOUtils;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
import org.springframework.context.annotation.Primary;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
|
@ -29,7 +36,8 @@ public class SaTokenConfigure {
|
|
|
|
config.setTimeout(24 * 60 * 60); // token有效期,单位s 默认1天
|
|
|
|
config.setTimeout(24 * 60 * 60); // token有效期,单位s 默认1天
|
|
|
|
config.setActivityTimeout(30 * 60); // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
|
|
|
|
config.setActivityTimeout(30 * 60); // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
|
|
|
|
config.setIsConcurrent(false); // 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
|
|
|
config.setIsConcurrent(false); // 是否允许同一账号并发登录 (为true时允许一起登录, 为false时新登录挤掉旧登录)
|
|
|
|
config.setIsShare(true); // 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
|
|
|
config.setMaxLoginCount(5); //同一账号最大登录数量,-1代表不限 (只有在 isConcurrent=true, isShare=false 时此配置才有效)
|
|
|
|
|
|
|
|
config.setIsShare(false); // 在多人登录同一账号时,是否共用一个token (为true时所有登录共用一个token, 为false时每次登录新建一个token)
|
|
|
|
config.setTokenStyle("simple-uuid"); // token风格
|
|
|
|
config.setTokenStyle("simple-uuid"); // token风格
|
|
|
|
config.setIsLog(false); // 是否输出操作日志
|
|
|
|
config.setIsLog(false); // 是否输出操作日志
|
|
|
|
config.setIsReadCookie(false);
|
|
|
|
config.setIsReadCookie(false);
|
|
|
@ -51,17 +59,20 @@ public class SaTokenConfigure {
|
|
|
|
|
|
|
|
|
|
|
|
// 指定 拦截路由 与 放行路由
|
|
|
|
// 指定 拦截路由 与 放行路由
|
|
|
|
.addInclude("/**")
|
|
|
|
.addInclude("/**")
|
|
|
|
.addExclude("/favicon.ico") /* 排除掉 /favicon.ico */
|
|
|
|
.addExclude("/favicon.ico","/","/actuator","/actuator/**","/v2/api-docs","/doc.html","/webjars/**","/swagger-resources") /* 排除掉 /favicon.ico */
|
|
|
|
// 认证函数: 每次请求执行
|
|
|
|
// 认证函数: 每次请求执行
|
|
|
|
.setAuth(obj -> {
|
|
|
|
.setAuth(obj -> {
|
|
|
|
// 登录认证 -- 拦截所有路由,并排除白名单 用于开放登录
|
|
|
|
// 登录认证 -- 拦截所有路由,并排除白名单 用于开放登录
|
|
|
|
// SaRouter.match("/**", String.valueOf(whiteList), () -> StpUtil.checkLogin());
|
|
|
|
|
|
|
|
SaRouter.match("/**").notMatch(whiteList).check(() -> StpUtil.checkLogin());
|
|
|
|
SaRouter.match("/**").notMatch(whiteList).check(() -> StpUtil.checkLogin());
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// 异常处理函数:每次认证函数发生异常时执行此函数
|
|
|
|
// 异常处理函数:每次认证函数发生异常时执行此函数
|
|
|
|
.setError(e -> {
|
|
|
|
.setError(e -> {
|
|
|
|
// SaHolder.getResponse().setHeader("Content-Type", ContentType.JSON.getValue() + ";charset=utf-8");
|
|
|
|
SaHolder.getResponse().setHeader("Content-Type", ContentType.build(ContentType.JSON, StandardCharsets.UTF_8));
|
|
|
|
|
|
|
|
if (e instanceof NotPermissionException) {
|
|
|
|
|
|
|
|
return JSONUtil.toJsonStr(ResultVOUtils.error(ResultEnum.AUTH_FAILED.getCode(), "没有权限"));
|
|
|
|
|
|
|
|
} else if (e instanceof NotLoginException) {
|
|
|
|
|
|
|
|
return JSONUtil.toJsonStr(ResultVOUtils.error(ResultEnum.LOGIN_VERIFY_FALL));
|
|
|
|
|
|
|
|
}
|
|
|
|
return SaResult.error(e.getMessage());
|
|
|
|
return SaResult.error(e.getMessage());
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|