repair: 2

This commit is contained in:
click33 2025-04-07 03:25:58 +08:00
parent 78de70bda6
commit de1239e9d2

View File

@ -0,0 +1,68 @@
/*
* 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.spring;
import cn.dev33.satoken.context.SaTokenContext;
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.servlet.model.SaRequestForServlet;
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
import cn.dev33.satoken.servlet.model.SaStorageForServlet;
/**
* <h2> 此为低版本(<1.42.0) 的上下文处理方案基于 Spring 内部工具类 RequestContextHolder 读写上下文仅做留档如无必要请勿使用 </h2>
*
* Sa-Token 上下文处理器 [ SpringMVC版本实现 ] SpringMVCSpringBoot 中使用 Sa-Token 必须注入此实现类否则会出现上下文无效异常
*
* @author click33
* @since 1.19.0
*/
public class SaTokenContextForSpring implements SaTokenContext {
/**
* 获取当前请求的 Request 包装对象
*/
@Override
public SaRequest getRequest() {
return new SaRequestForServlet(SpringMVCUtil.getRequest());
}
/**
* 获取当前请求的 Response 包装对象
*/
@Override
public SaResponse getResponse() {
return new SaResponseForServlet(SpringMVCUtil.getResponse());
}
/**
* 获取当前请求的 Storage 包装对象
*/
@Override
public SaStorage getStorage() {
return new SaStorageForServlet(SpringMVCUtil.getRequest());
}
/**
* 判断在本次请求中此上下文是否可用
*/
@Override
public boolean isValid() {
return SpringMVCUtil.isWeb();
}
}