🔨 对外服务 api 配置.

This commit is contained in:
lijiahangmax
2025-12-09 10:06:24 +08:00
parent 40afc03203
commit f4ddeb3e6c
10 changed files with 122 additions and 7 deletions

View File

@@ -6,6 +6,8 @@ SPRING_PROFILES_ACTIVE=prod
DEMO_MODE=false
API_CORS=true
API_HOST=0.0.0.0
# API_URL=http://127.0.0.1:9700/orion-visor/api
API_IP_HEADERS=X-Forwarded-For,X-Real-IP
API_EXPOSE_TOKEN=pmqeHOyZaumHm0Wt
SECRET_KEY=uQeacXV8b3isvKLK

View File

@@ -50,6 +50,7 @@ services:
SECRET_KEY: ${SECRET_KEY:-uQeacXV8b3isvKLK}
API_EXPOSE_TOKEN: ${API_EXPOSE_TOKEN:-pmqeHOyZaumHm0Wt}
API_IP_HEADERS: ${API_IP_HEADERS:-X-Forwarded-For,X-Real-IP}
API_HOST: ${API_HOST:-0.0.0.0}
API_CORS: ${API_CORS:-true}
DEMO_MODE: ${DEMO_MODE:-false}
volumes:

View File

@@ -55,4 +55,6 @@ public interface Const extends cn.orionsec.kit.lang.constant.Const, FieldConst,
int BATCH_COUNT = 500;
String IP_0000 = "0.0.0.0";
}

View File

@@ -29,6 +29,7 @@ import org.dromara.visor.common.constant.AutoConfigureOrderConst;
import org.dromara.visor.common.constant.FilterOrderConst;
import org.dromara.visor.common.web.WebFilterCreator;
import org.dromara.visor.framework.web.configuration.config.ExposeApiConfig;
import org.dromara.visor.framework.web.configuration.config.OrionApiConfig;
import org.dromara.visor.framework.web.core.aspect.DemoDisableApiAspect;
import org.dromara.visor.framework.web.core.aspect.ExposeApiAspect;
import org.dromara.visor.framework.web.core.filter.TraceIdFilter;
@@ -71,7 +72,7 @@ import java.util.List;
@DependsOn({"executorContext"})
@AutoConfiguration
@AutoConfigureOrder(AutoConfigureOrderConst.FRAMEWORK_WEB)
@EnableConfigurationProperties(ExposeApiConfig.class)
@EnableConfigurationProperties({ExposeApiConfig.class, OrionApiConfig.class})
public class OrionWebAutoConfiguration implements WebMvcConfigurer {
@Value("${orion.prefix}")

View File

@@ -36,6 +36,11 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties("orion.api.expose")
public class ExposeApiConfig {
/**
* 对外服务地址
*/
private String host;
/**
* 对外服务请求头
*/

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 2023 - present Dromara, All rights reserved.
*
* https://visor.dromara.org
* https://visor.dromara.org.cn
* https://visor.orionsec.cn
*
* Members:
* Jiahang Li - ljh1553488six@139.com - author
*
* 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 org.dromara.visor.framework.web.configuration.config;
import cn.orionsec.kit.lang.utils.Strings;
import cn.orionsec.kit.lang.utils.net.IPs;
import lombok.Data;
import org.dromara.visor.common.constant.Const;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* api 配置属性
*
* @author Jiahang Li
* @version 1.0.0
* @since 2025/12/8 14:00
*/
@Data
@ConfigurationProperties("orion.api")
public class OrionApiConfig {
private static final String URL_TEMPLATE = "http://{}:{}{}";
/**
* 公共 api 前缀
*/
private String prefix;
/**
* 服务端主机地址
*/
private String host;
/**
* 服务端口
*/
@Value("${server.port}")
private Integer port;
/**
* 服务端 url
*/
private String url;
public String getHost() {
if (Const.IP_0000.equalsIgnoreCase(host)) {
// 本机
return IPs.IP;
} else {
return host;
}
}
public String getUrl() {
if (!Strings.isBlank(url)) {
return url;
}
// 构建
return Strings.format(URL_TEMPLATE, this.getHost(), port, prefix);
}
}

View File

@@ -33,6 +33,16 @@
"type": "java.lang.Boolean",
"description": "是否开启 cors 过滤器."
},
{
"name": "orion.api.host",
"type": "java.lang.String",
"description": "服务端主机地址."
},
{
"name": "orion.api.url",
"type": "java.lang.String",
"description": "服务端接口地址."
},
{
"name": "orion.api.ip-headers",
"type": "java.lang.String",

View File

@@ -74,6 +74,10 @@ orion:
api:
# 是否允许跨域
cors: ${API_CORS:true}
# 服务端主机地址
host: ${API_HOST:0.0.0.0}
# 服务端接口地址 默认自动生成
url: ${API_URL:}
# 获取 IP 的请求头
ip-headers: ${API_IP_HEADERS:X-Forwarded-For,X-Real-IP}
# 对外服务

View File

@@ -175,6 +175,10 @@ orion:
prefix: ${orion.prefix}/api
# 是否允许跨域
cors: true
# 服务端主机地址
host: 0.0.0.0
# 服务端接口地址 默认自动生成
url:
# 获取 IP 的请求头
ip-headers: X-Forwarded-For,X-Real-IP
# 对外服务

View File

@@ -31,7 +31,6 @@ import cn.orionsec.kit.lang.utils.io.FileReaders;
import cn.orionsec.kit.lang.utils.io.Files1;
import cn.orionsec.kit.lang.utils.io.compress.CompressTypeEnum;
import cn.orionsec.kit.lang.utils.io.compress.FileDecompressor;
import cn.orionsec.kit.lang.utils.net.IPs;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.constant.Const;
import org.dromara.visor.common.constant.ErrorMessage;
@@ -41,6 +40,8 @@ import org.dromara.visor.common.utils.Assert;
import org.dromara.visor.common.utils.PathUtils;
import org.dromara.visor.framework.biz.operator.log.core.utils.OperatorLogs;
import org.dromara.visor.framework.redis.core.utils.RedisStrings;
import org.dromara.visor.framework.web.configuration.config.ExposeApiConfig;
import org.dromara.visor.framework.web.configuration.config.OrionApiConfig;
import org.dromara.visor.module.asset.convert.HostConvert;
import org.dromara.visor.module.asset.dao.HostAgentLogDAO;
import org.dromara.visor.module.asset.dao.HostDAO;
@@ -53,7 +54,6 @@ import org.dromara.visor.module.asset.enums.*;
import org.dromara.visor.module.asset.handler.agent.intstall.AgentInstaller;
import org.dromara.visor.module.asset.handler.agent.model.AgentInstallParams;
import org.dromara.visor.module.asset.service.HostAgentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
@@ -77,8 +77,11 @@ public class HostAgentServiceImpl implements HostAgentService {
private String localVersion;
@Value("${orion.api.expose.token}")
private String exposeToken;
@Resource
private OrionApiConfig orionApiConfig;
@Resource
private ExposeApiConfig exposeApiConfig;
@Resource
private HostDAO hostDAO;
@@ -302,8 +305,8 @@ public class HostAgentServiceImpl implements HostAgentService {
*/
private Map<String, String> getReplaceVars() {
Map<String, String> map = new HashMap<>();
map.put("SERVER_HOST", IPs.IP);
map.put("SERVER_TOKEN", exposeToken);
map.put("SERVER_URL", orionApiConfig.getUrl());
map.put("SERVER_TOKEN", exposeApiConfig.getToken());
return map;
}