feat: 新增 http 请求处理器模块

This commit is contained in:
click33 2025-04-26 23:24:31 +08:00
parent abdfb2305d
commit 0ae51a1b56
6 changed files with 144 additions and 0 deletions

View File

@ -26,6 +26,8 @@ import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
import cn.dev33.satoken.error.SaErrorCode;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.http.SaHttpTemplate;
import cn.dev33.satoken.http.SaHttpTemplateDefaultImpl;
import cn.dev33.satoken.json.SaJsonTemplate;
import cn.dev33.satoken.json.SaJsonTemplateDefaultImpl;
import cn.dev33.satoken.listener.SaTokenEventCenter;
@ -201,6 +203,25 @@ public class SaManager {
return saJsonTemplate;
}
/**
* HTTP 转换器
*/
private volatile static SaHttpTemplate saHttpTemplate;
public static void setSaHttpTemplate(SaHttpTemplate saHttpTemplate) {
SaManager.saHttpTemplate = saHttpTemplate;
SaTokenEventCenter.doRegisterComponent("SaHttpTemplate", saHttpTemplate);
}
public static SaHttpTemplate getSaHttpTemplate() {
if (saHttpTemplate == null) {
synchronized (SaManager.class) {
if (saHttpTemplate == null) {
SaManager.saHttpTemplate = new SaHttpTemplateDefaultImpl();
}
}
}
return saHttpTemplate;
}
/**
* 序列化器
*/

View File

@ -37,6 +37,9 @@ public interface SaErrorCode {
/** JSON 转换器未实现 */
int CODE_10003 = 10003;
/** HTTP 请求处理器未实现 */
int CODE_10004 = 10004;
/** 未能从全局 StpLogic 集合中找到对应 type 的 StpLogic */
int CODE_10011 = 10011;

View File

@ -0,0 +1,45 @@
/*
* 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.http;
import java.util.Map;
/**
* Http 请求处理器
*
* @author click33
* @since 1.42.0
*/
public interface SaHttpTemplate {
/**
* get 请求
*
* @param url /
* @return /
*/
String get(String url);
/**
* post 请求form-data 格式参数
*
* @param url /
* @param params /
* @return /
*/
String postByFormData(String url, Map<String, Object> params);
}

View File

@ -0,0 +1,52 @@
/*
* 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.http;
import cn.dev33.satoken.error.SaErrorCode;
import cn.dev33.satoken.exception.NotImplException;
import java.util.Map;
/**
* Http 请求处理器默认实现类
*
* @author click33
* @since 1.42.0
*/
public class SaHttpTemplateDefaultImpl implements SaHttpTemplate {
public static final String ERROR_MESSAGE = "HTTP 请求处理器未实现";
/**
* get 请求
*
* @param url /
* @return /
*/
@Override
public String get(String url) {
throw new NotImplException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10004);
}
/**
* post 请求form-data 格式参数
*/
@Override
public String postByFormData(String url, Map<String, Object> params) {
throw new NotImplException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10004);
}
}

View File

@ -23,6 +23,7 @@ import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.fun.strategy.SaCorsHandleFunction;
import cn.dev33.satoken.http.SaHttpTemplate;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicTemplate;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
import cn.dev33.satoken.httpauth.digest.SaHttpDigestTemplate;
@ -199,6 +200,17 @@ public class SaBeanInject {
SaManager.setSaJsonTemplate(saJsonTemplate);
}
/**
* 注入自定义的 Http 转换器 Bean
*
* @param saHttpTemplate Http 转换器
*/
@Condition(onBean = SaHttpTemplate.class)
@Bean
public void setSaHttpTemplate(SaHttpTemplate saHttpTemplate) {
SaManager.setSaHttpTemplate(saHttpTemplate);
}
/**
* 注入自定义的序列化器 Bean
*

View File

@ -23,6 +23,7 @@ import cn.dev33.satoken.config.SaTokenConfig;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.dao.SaTokenDao;
import cn.dev33.satoken.fun.strategy.SaCorsHandleFunction;
import cn.dev33.satoken.http.SaHttpTemplate;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicTemplate;
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
import cn.dev33.satoken.httpauth.digest.SaHttpDigestTemplate;
@ -188,6 +189,16 @@ public class SaBeanInject {
SaManager.setSaJsonTemplate(saJsonTemplate);
}
/**
* 注入自定义的 Http 转换器 Bean
*
* @param saHttpTemplate /
*/
@Autowired(required = false)
public void setSaHttpTemplate(SaHttpTemplate saHttpTemplate) {
SaManager.setSaHttpTemplate(saHttpTemplate);
}
/**
* 注入自定义的序列化器 Bean
*