2024.4.12
在SaTlkenConfigure.java中解决了跨域,修改了鉴权方法,不需要注解鉴权了
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
package com.hongliang.videotask.config;
|
||||
|
||||
import cn.dev33.satoken.context.SaHolder;
|
||||
import cn.dev33.satoken.filter.SaServletFilter;
|
||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||
import cn.dev33.satoken.router.SaRouter;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
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;
|
||||
@@ -13,4 +19,38 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
// 注册 Sa-Token 拦截器,打开注解式鉴权功能
|
||||
registry.addInterceptor(new SaInterceptor()).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 [Sa-Token全局过滤器]
|
||||
*/
|
||||
@Bean
|
||||
public SaServletFilter getSaServletFilter() {
|
||||
return new SaServletFilter()
|
||||
// 指定 拦截路由 与 放行路由
|
||||
.addInclude("/**").addExclude("/user/login").addExclude("/user/isLogin").addExclude("/user/logout")
|
||||
// 认证函数: 每次请求执行
|
||||
.setAuth(obj -> {
|
||||
System.out.println("---------- 进入Sa-Token全局认证 -----------");
|
||||
// 登录认证 -- 拦截所有路由,并排除/user/login 用于开放登录
|
||||
SaRouter.match("/**", "/user/login", () -> StpUtil.checkLogin());
|
||||
})
|
||||
// 异常处理函数:每次认证函数发生异常时执行此函数
|
||||
.setError(e -> {
|
||||
System.out.println("---------- 进入Sa-Token异常处理 -----------");
|
||||
return SaResult.error(e.getMessage());
|
||||
})
|
||||
// 前置函数:在每次认证函数之前执行
|
||||
.setBeforeAuth(obj -> {
|
||||
// ---------- 设置跨域响应头 ----------
|
||||
SaHolder.getResponse()
|
||||
// 允许指定域访问跨域资源
|
||||
.setHeader("Access-Control-Allow-Origin", "*")
|
||||
// 允许所有请求方式
|
||||
.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE")
|
||||
// 有效时间
|
||||
.setHeader("Access-Control-Max-Age", "3600")
|
||||
// 允许的header参数
|
||||
.setHeader("Access-Control-Allow-Headers", "*");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,6 @@ public class NodeControl {
|
||||
@Resource
|
||||
private NodeServiceImpl nodeService;
|
||||
|
||||
@SaCheckLogin
|
||||
@PostMapping("list")
|
||||
public ResponseEntity<?> GetNodeListByCondition(@RequestBody String condition){
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"success",
|
||||
|
||||
@@ -57,7 +57,6 @@ public class UserControl {
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功退出",null));
|
||||
}
|
||||
|
||||
@SaCheckLogin
|
||||
@GetMapping("/user/{id}")
|
||||
public ResponseEntity<?> GetUserByID(@PathVariable("id") int id){
|
||||
return ResponseEntity.ok(new Response(ResponseCode.OK,"成功",userService.GetUserByID(id)));
|
||||
|
||||
Reference in New Issue
Block a user