微服务版后端初始化
This commit is contained in:
@ -0,0 +1,124 @@
|
||||
package com.xjrsoft.common.satoken;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.exception.NotPermissionException;
|
||||
import cn.dev33.satoken.exception.NotRoleException;
|
||||
import cn.dev33.satoken.filter.SaServletFilter;
|
||||
import cn.dev33.satoken.router.SaHttpMethod;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.temp.SaTempUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.xjrsoft.common.core.config.CommonPropertiesConfig;
|
||||
import com.xjrsoft.common.core.constant.GlobalConstant;
|
||||
import com.xjrsoft.common.core.enums.ResponseCode;
|
||||
import com.xjrsoft.common.core.exception.MyException;
|
||||
import com.xjrsoft.common.satoken.config.MagicApiConfig;
|
||||
import com.xjrsoft.common.satoken.interceptor.MagicApiWebLoginInterceptor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
/**
|
||||
* @Author: tzx
|
||||
* @Date: 2023/9/26 15:05
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@RequiredArgsConstructor
|
||||
public class SaTokenAutoConfiguration implements WebMvcConfigurer {
|
||||
|
||||
private final MagicApiConfig magicApiConfig;
|
||||
|
||||
private final CommonPropertiesConfig commonPropertiesConfig;
|
||||
|
||||
/**
|
||||
* 注册Sa-Token 的拦截器,打开注解式鉴权功能
|
||||
*/
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
// 注册MagicApi登录判断拦截器
|
||||
registry.addInterceptor(new MagicApiWebLoginInterceptor()).addPathPatterns(magicApiConfig.getWeb() + "/**");
|
||||
//satoken 注解鉴权拦截器
|
||||
// registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 注册 [Sa-Token全局过滤器]
|
||||
*/
|
||||
@Bean
|
||||
public SaServletFilter getSaServletFilter() {
|
||||
return new SaServletFilter()
|
||||
// 指定 拦截路由 与 放行路由
|
||||
.addInclude("/**")
|
||||
.addExclude(commonPropertiesConfig.getExcludeUrls().toArray(new String[0]))
|
||||
// 认证函数: 每次请求执行
|
||||
.setAuth(obj -> {
|
||||
// 登录认证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
|
||||
if (commonPropertiesConfig.getWhiteList().contains(SaHolder.getRequest().getUrl())) {
|
||||
return;
|
||||
}
|
||||
|
||||
String token = SaHolder.getRequest().getParam("token");
|
||||
if (StrUtil.isNotBlank(token)) {
|
||||
try {
|
||||
String tokenValue = SaTempUtil.parseToken(token, String.class);
|
||||
if (!tokenValue.contains(GlobalConstant.SECRET_KEY) || SaTempUtil.getTimeout(token) < 1) {
|
||||
throw new MyException("临时token 无效!");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MyException("临时token 无效!");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
SaRouter.match("/**", "/system/login", StpUtil::checkLogin);
|
||||
// 更多拦截处理方式,请参考“路由拦截式鉴权”章节
|
||||
})
|
||||
// 异常处理函数:每次认证函数发生异常时执行此函数
|
||||
.setError(e -> {
|
||||
if (e instanceof NotLoginException) { // 如果是未登录异常3
|
||||
return SaResult.get(ResponseCode.UN_AUTHORIZED.getCode(), ResponseCode.UN_AUTHORIZED.getMessage(), null);
|
||||
} else if (e instanceof NotRoleException) { // 如果是角色异常
|
||||
return SaResult.get(ResponseCode.REQ_REJECT.getCode(), ResponseCode.REQ_REJECT.getMessage(), null);
|
||||
} else if (e instanceof NotPermissionException) { // 如果是权限异常
|
||||
return SaResult.get(ResponseCode.REQ_REJECT.getCode(), ResponseCode.REQ_REJECT.getMessage(), null);
|
||||
} else { // 普通异常, 输出:500 + 异常信息
|
||||
return SaResult.get(ResponseCode.INTERNAL_SERVER_ERROR.getCode(), ResponseCode.INTERNAL_SERVER_ERROR.getMessage(), null);
|
||||
}
|
||||
})
|
||||
// 前置函数:在每次认证函数之前执行
|
||||
.setBeforeAuth(r -> {
|
||||
// ---------- 设置一些安全响应头 ----------
|
||||
SaHolder.getResponse()
|
||||
// 服务器名称
|
||||
.setServer("sa-server")
|
||||
// 是否可以在iframe显示视图: DENY=不可以 | SAMEORIGIN=同域下可以 | ALLOW-FROM uri=指定域名下可以
|
||||
.setHeader("X-Frame-Options", "SAMEORIGIN")
|
||||
// 是否启用浏览器默认XSS防护: 0=禁用 | 1=启用 | 1; mode=block 启用, 并在检查到XSS攻击时,停止渲染页面
|
||||
.setHeader("X-XSS-Protection", "1; mode=block")
|
||||
// 禁用浏览器内容嗅探
|
||||
.setHeader("X-Content-Type-Options", "nosniff")
|
||||
// 域名
|
||||
.setHeader("Access-Control-Allow-Origin", "*")
|
||||
// 请求方式
|
||||
.setHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT")
|
||||
// 缓存时间
|
||||
.setHeader("Access-Control-Max-Age", "3600")
|
||||
// 请求头
|
||||
.setHeader("Access-Control-Allow-Headers", "*");
|
||||
|
||||
//option 必须跳过 不然会影响 cors配置 而且 必须放在设置请求头后面
|
||||
SaRouter.match(SaHttpMethod.OPTIONS).back();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -0,0 +1,26 @@
|
||||
package com.xjrsoft.common.satoken.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* magic-api 配置
|
||||
* @author tzx
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@ConfigurationProperties("magic-api")
|
||||
public class MagicApiConfig {
|
||||
|
||||
/**
|
||||
* 页面地址 magic-api
|
||||
*/
|
||||
private String web;
|
||||
|
||||
/**
|
||||
* 接口前缀
|
||||
*/
|
||||
private String prefix;
|
||||
|
||||
}
|
||||
@ -0,0 +1,54 @@
|
||||
package com.xjrsoft.common.satoken.interceptor;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.hutool.core.net.url.UrlBuilder;
|
||||
import cn.hutool.core.util.CharsetUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* magicapi web页面 使用系统的登录
|
||||
*
|
||||
* @Author: tzx
|
||||
* @Date: 2022/11/1 12:16
|
||||
*/
|
||||
@Component
|
||||
public class MagicApiWebLoginInterceptor implements HandlerInterceptor {
|
||||
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
|
||||
// 根据parameter 判断是否有权限
|
||||
String parameter = request.getParameter(StpUtil.getTokenName());
|
||||
|
||||
if (StrUtil.isBlank(parameter)) {
|
||||
//根据Referer 判断是否有权限
|
||||
String refererKey = "Referer";
|
||||
String referer = request.getHeader(refererKey);
|
||||
|
||||
if (StrUtil.isBlank(referer)) {
|
||||
return false;
|
||||
} else {
|
||||
UrlBuilder urlBuilder = UrlBuilder.ofHttp(referer, CharsetUtil.CHARSET_UTF_8);
|
||||
CharSequence param = urlBuilder.getQuery().get(StpUtil.getTokenName());
|
||||
if (StrUtil.isBlank(param)) {
|
||||
return false;
|
||||
} else {
|
||||
Object loginIdByToken = StpUtil.getLoginIdByToken(param.toString());
|
||||
return !ObjectUtil.isNull(loginIdByToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
Object loginIdByToken = StpUtil.getLoginIdByToken(parameter);
|
||||
return !ObjectUtil.isNull(loginIdByToken);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
package com.xjrsoft.common.satoken.interceptor;
|
||||
|
||||
import feign.RequestInterceptor;
|
||||
import feign.RequestTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 自定义的Feign拦截器
|
||||
*
|
||||
* @author Saint
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class MyFeignRequestInterceptor implements RequestInterceptor {
|
||||
|
||||
|
||||
/**
|
||||
* 这里可以实现对请求的拦截,对请求添加一些额外信息之类的
|
||||
*
|
||||
* @param requestTemplate
|
||||
*/
|
||||
@Override
|
||||
public void apply(RequestTemplate requestTemplate) {
|
||||
// 1. obtain request
|
||||
final ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
|
||||
// 2. 兼容hystrix限流后,获取不到ServletRequestAttributes的问题(使拦截器直接失效)
|
||||
if (Objects.isNull(attributes)) {
|
||||
log.error("MyFeignRequestInterceptor is invalid!");
|
||||
return;
|
||||
}
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
|
||||
// 2. obtain request headers,and put it into openFeign RequestTemplate
|
||||
Enumeration<String> headerNames = request.getHeaderNames();
|
||||
if (Objects.nonNull(headerNames)) {
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String name = headerNames.nextElement();
|
||||
|
||||
if("content-length".equals(name)){
|
||||
continue;
|
||||
}
|
||||
String value = request.getHeader(name);
|
||||
|
||||
requestTemplate.header(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// todo 需要传递请求参数时放开
|
||||
// 3. obtain request body, and put it into openFeign RequestTemplate
|
||||
// Enumeration<String> bodyNames = request.getParameterNames();
|
||||
// StringBuffer body = new StringBuffer();
|
||||
// if (bodyNames != null) {
|
||||
// while (bodyNames.hasMoreElements()) {
|
||||
// String name = bodyNames.nextElement();
|
||||
// String value = request.getParameter(name);
|
||||
// body.append(name).append("=").append(value).append("&");
|
||||
// }
|
||||
// }
|
||||
// if (body.length() != 0) {
|
||||
// body.deleteCharAt(body.length() - 1);
|
||||
// requestTemplate.body(body.toString());
|
||||
// log.info("openfeign interceptor body:{}", body.toString());
|
||||
// }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1 @@
|
||||
com.xjrsoft.common.satoken.SaTokenAutoConfiguration
|
||||
Reference in New Issue
Block a user