|
|
@@ -0,0 +1,40 @@
|
|
|
+package com.middle.platform.satoken.config;
|
|
|
+
|
|
|
+import cn.dev33.satoken.context.SaHolder;
|
|
|
+import cn.dev33.satoken.filter.SaServletFilter;
|
|
|
+import com.middle.platform.common.utils.Result;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+@Configuration
|
|
|
+public class SaTokenConfig {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 注册 [Sa-Token全局过滤器]
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public SaServletFilter getSaServletFilter() {
|
|
|
+ return new SaServletFilter()
|
|
|
+ // 指定 拦截路由 与 放行路由
|
|
|
+ .addInclude("/**")
|
|
|
+ .addExclude("/auth/login","/auth/logout")
|
|
|
+ // 异常处理函数:每次认证函数发生异常时执行此函数
|
|
|
+ .setError(e -> {
|
|
|
+ SaHolder.getResponse().setHeader("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ return Result.failed(e.getMessage());
|
|
|
+ })
|
|
|
+
|
|
|
+ // 前置函数:在每次认证函数之前执行(BeforeAuth 不受 includeList 与 excludeList 的限制,所有请求都会进入)
|
|
|
+ .setBeforeAuth(r -> {
|
|
|
+ // ---------- 设置一些安全响应头 ----------
|
|
|
+ SaHolder.getResponse()
|
|
|
+ // 是否启用浏览器默认XSS防护: 0=禁用 | 1=启用 | 1; mode=block 启用, 并在检查到XSS攻击时,停止渲染页面
|
|
|
+ .setHeader("X-XSS-Protection", "1; mode=block")
|
|
|
+ // 禁用浏览器内容嗅探
|
|
|
+ .setHeader("X-Content-Type-Options", "nosniff")
|
|
|
+ ;
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|