refactor: 重构所有 starter 组件的 SaTokenContext 上下文读写策略

This commit is contained in:
click33
2025-04-06 23:22:01 +08:00
parent 36cc99a70c
commit 55f0c94aec
92 changed files with 1538 additions and 1234 deletions

View File

@@ -188,7 +188,7 @@ public class SaRequestForServlet implements SaRequest {
@Override
public Object forward(String path) {
try {
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContextOrSecond().getResponse().getSource();
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContext().getResponse().getSource();
request.getRequestDispatcher(path).forward(request, response);
return null;
} catch (ServletException | IOException e) {

View File

@@ -0,0 +1,48 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.servlet.util;
import cn.dev33.satoken.util.SaTokenConsts;
import jakarta.servlet.ServletResponse;
import java.io.IOException;
/**
* Jakarta Servlet 操作工具类
*
* @author click33
* @since 1.42.0
*/
public class SaJakartaServletOperateUtil {
/**
* 写入结果到输出流
* @param response /
* @param result /
*/
public static void writeResult(ServletResponse response, String result) throws IOException {
// 写入输出流
// 请注意此处默认 Content-Type 为 text/plain如果需要返回 JSON 信息,需要在 return 前自行设置 Content-Type 为 application/json
// 例如SaHolder.getResponse().setHeader("Content-Type", "application/json;charset=UTF-8");
if(response.getContentType() == null) {
response.setContentType(SaTokenConsts.CONTENT_TYPE_TEXT_PLAIN);
}
response.getWriter().print(result);
response.getWriter().flush();
}
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.servlet.util;
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.fun.SaFunction;
import cn.dev33.satoken.servlet.model.SaRequestForServlet;
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
import cn.dev33.satoken.servlet.model.SaStorageForServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
/**
* SaTokenContext 上下文读写工具类
*
* @author click33
* @since 1.42.0
*/
public class SaTokenContextUtil {
/**
* 写入当前上下文
* @param request /
* @param response /
*/
public static void setContext(HttpServletRequest request, HttpServletResponse response) {
SaRequest req = new SaRequestForServlet(request);
SaResponse res = new SaResponseForServlet(response);
SaStorage stg = new SaStorageForServlet(request);
SaTokenContextForThreadLocalStorage.setBox(req, res, stg);
}
/**
* 清除当前上下文
*/
public static void clearContext() {
SaTokenContextForThreadLocalStorage.clearBox();
}
/**
* 写入上下文对象, 并在执行函数后将其清除
* @param request /
* @param response /
* @param fun /
*/
public static void setContext(HttpServletRequest request, HttpServletResponse response, SaFunction fun) {
try {
setContext(request, response);
fun.run();
} finally {
clearContext();
}
}
/**
* 获取当前 Box
* @return /
*/
public static Box getBox() {
return SaTokenContextForThreadLocalStorage.getBoxNotNull();
}
/**
* 获取当前 Request
* @return /
*/
public static HttpServletRequest getRequest() {
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
return (HttpServletRequest) box.getRequest().getSource();
}
/**
* 获取当前 Response
* @return /
*/
public static HttpServletResponse getResponse() {
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
return (HttpServletResponse) box.getResponse().getSource();
}
}