feat:新增配置类,实现动态配置管理,自动化注入配置功能

This commit is contained in:
2025-05-30 15:55:21 +08:00
parent 28398be217
commit 9b7fa58fcf
24 changed files with 191 additions and 170 deletions
+10
View File
@@ -403,6 +403,16 @@
<artifactId>oauth2-oidc-sdk</artifactId>
<version>${oauth2-oidc-sdk.version}</version>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-wechat-common-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-wechat-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
@@ -3,10 +3,10 @@ package com.central.user.controller;
import com.central.common.context.TenantContextHolder;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.user.service.PrivateKeyService;
import com.central.user.service.WechatConfigService;
import com.central.user.service.WechatPayService;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.service.PrivateKeyService;
import com.zlt.wechat.service.WechatConfigService;
import com.zlt.wechat.service.WechatPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@@ -14,10 +14,10 @@ import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.util.Map;
@Slf4j
@@ -27,13 +27,13 @@ import java.util.Map;
@RequestMapping("/wechat/config")
public class SysWechatConfigController {
@Autowired
@Resource
private WechatConfigService wechatConfigService;
@Autowired
@Resource
private WechatPayService wechatPayService;
@Autowired
@Resource
private PrivateKeyService privateKeyService;
@Operation(summary = "查询列表")
@@ -109,4 +109,10 @@ public class SysWechatConfigController {
public Result<WechatConfig> getCurrentConfig(@PathVariable String type) {
return Result.succeed(wechatConfigService.getConfig(type, TenantContextHolder.getTenant()));
}
@Operation(summary = "获取租户密钥")
@GetMapping("/tenant/private-key")
public String getPrivateKey(@RequestParam("tenantId")String tenantId,@RequestParam String type) {
return privateKeyService.getPrivateKey(tenantId, type);
}
}
@@ -1,4 +1,4 @@
package com.zlt.wechat.mapper;
package com.central.user.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -1,4 +1,4 @@
package com.zlt.wechat.service;
package com.central.user.service;
import java.io.InputStream;
@@ -6,7 +6,7 @@ import java.io.InputStream;
* 私钥文件管理服务
*/
public interface PrivateKeyService {
/**
* 上传私钥文件
*
@@ -16,7 +16,7 @@ public interface PrivateKeyService {
* @return 文件访问路径
*/
String uploadPrivateKey(String tenantId, String type, InputStream inputStream);
/**
* 获取私钥文件
*
@@ -25,7 +25,7 @@ public interface PrivateKeyService {
* @return 私钥文件内容
*/
String getPrivateKey(String tenantId, String type);
/**
* 删除私钥文件
*
@@ -34,4 +34,4 @@ public interface PrivateKeyService {
* @return 是否成功
*/
boolean deletePrivateKey(String tenantId, String type);
}
}
@@ -1,55 +0,0 @@
package com.central.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.central.user.model.SysWechatConfig;
public interface SysWechatConfigService extends IService<SysWechatConfig> {
/**
* 获取微信配置
*
* @param type 配置类型
* @return 微信配置
*/
SysWechatConfig getConfig(String type);
/**
* 保存微信配置
*
* @param config 微信配置
* @return 是否成功
*/
boolean saveConfig(SysWechatConfig config);
/**
* 更新微信配置
*
* @param config 微信配置
* @return 是否成功
*/
boolean updateConfig(SysWechatConfig config);
/**
* 删除微信配置
*
* @param id 配置ID
* @return 是否成功
*/
boolean deleteConfig(Long id);
/**
* 切换当前使用的微信配置
*
* @param type 配置类型
* @return 是否成功
*/
boolean switchConfig(String type);
/**
* 获取当前使用的微信配置
*
* @param type 配置类型
* @return 微信配置
*/
SysWechatConfig getCurrentConfig(String type);
}
@@ -1,4 +1,4 @@
package com.zlt.wechat.service;
package com.central.user.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.central.common.model.PageResult;
@@ -1,4 +1,4 @@
package com.zlt.wechat.service;
package com.central.user.service;
import com.wechat.pay.java.service.payments.jsapi.model.PrepayWithRequestPaymentResponse;
import com.wechat.pay.java.service.payments.model.Transaction;
@@ -1,6 +1,6 @@
package com.zlt.wechat.service.impl;
package com.central.user.service.impl;
import com.zlt.wechat.service.PrivateKeyService;
import com.central.user.service.PrivateKeyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
@@ -30,11 +30,11 @@ public class PrivateKeyServiceImpl implements PrivateKeyService {
while ((line = reader.readLine()) != null) {
content.append(line).append("\n");
}
// 缓存私钥内容
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
redisTemplate.opsForValue().set(cacheKey, content.toString(), CACHE_EXPIRE_DAYS, TimeUnit.DAYS);
return cacheKey;
} catch (Exception e) {
throw new RuntimeException("上传私钥文件失败", e);
@@ -45,11 +45,11 @@ public class PrivateKeyServiceImpl implements PrivateKeyService {
public String getPrivateKey(String tenantId, String type) {
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
String privateKey = redisTemplate.opsForValue().get(cacheKey);
if (!StringUtils.hasText(privateKey)) {
throw new RuntimeException("私钥文件不存在");
}
return privateKey;
}
@@ -58,4 +58,4 @@ public class PrivateKeyServiceImpl implements PrivateKeyService {
String cacheKey = String.format(CACHE_KEY_PATTERN, tenantId, type);
return redisTemplate.delete(cacheKey);
}
}
}
@@ -1,61 +0,0 @@
package com.central.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.context.TenantContextHolder;
import com.central.user.mapper.SysWechatConfigMapper;
import com.central.user.model.SysWechatConfig;
import com.central.user.service.SysWechatConfigService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
@Service
public class SysWechatConfigServiceImpl extends ServiceImpl<SysWechatConfigMapper, SysWechatConfig>
implements SysWechatConfigService {
private static final String CACHE_NAME = "wechat:config";
private static final String CACHE_KEY_PATTERN = "#type";
@Override
@Cacheable(value = CACHE_NAME, key = CACHE_KEY_PATTERN, unless = "#result == null")
public SysWechatConfig getConfig(String type) {
return getOne(new LambdaQueryWrapper<SysWechatConfig>()
.eq(SysWechatConfig::getType, type)
.eq(SysWechatConfig::getTenantId, TenantContextHolder.getTenant())
.eq(SysWechatConfig::getStatus, 1));
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean saveConfig(SysWechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
return save(config);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean updateConfig(SysWechatConfig config) {
config.setTenantId(TenantContextHolder.getTenant());
return updateById(config);
}
@Override
@CacheEvict(value = CACHE_NAME, allEntries = true)
public boolean deleteConfig(Long id) {
return removeById(id);
}
@Override
@CacheEvict(value = CACHE_NAME, key = CACHE_KEY_PATTERN)
public boolean switchConfig(String type) {
// 这里可以添加切换配置的具体逻辑
return true;
}
@Override
@Cacheable(value = CACHE_NAME, key = "'current:' + #type", unless = "#result == null")
public SysWechatConfig getCurrentConfig(String type) {
return getConfig(type);
}
}
@@ -1,12 +1,13 @@
package com.zlt.wechat.service.impl;
package com.central.user.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.central.common.model.PageResult;
import com.central.user.mapper.WechatConfigMapper;
import com.central.user.service.WechatConfigService;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.mapper.WechatConfigMapper;
import com.zlt.wechat.service.WechatConfigService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.MapUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
@@ -15,6 +16,7 @@ import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
@RequiredArgsConstructor
@Service
public class WechatConfigServiceImpl extends ServiceImpl<WechatConfigMapper, WechatConfig> implements WechatConfigService {
@@ -1,4 +1,4 @@
//package com.zlt.wechat.service.impl;
package com.central.user.service.impl;//package com.zlt.wechat.service.impl;
//
//import com.wechat.pay.java.core.Config;
//import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
+2 -1
View File
@@ -22,6 +22,7 @@
<module>zlt-elasticsearch-spring-boot-starter</module>
<module>zlt-oss-spring-boot-starter</module>
<module>zlt-zookeeper-spring-boot-starter</module>
<!--<module>zlt-iot-spring-boot-starter</module>-->
<module>zlt-wechat-common-core</module>
<module>zlt-wechat-spring-boot-starter</module>
</modules>
</project>
@@ -0,0 +1,25 @@
package com.zlt.wechat.feign;
import com.central.common.constant.ServiceNameConstants;
import com.zlt.wechat.feign.fallback.PrivateKeyServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
/**
* @author zlt
*/
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = PrivateKeyServiceFallbackFactory.class, dismiss404 = true)
public interface PrivateKeyService {
/**
* feign rpc访问远程/wechat/config/{type}接口
* 查询微信配置对象WechatConfig
*
* @param type
* @return
*/
@GetMapping(value = "/wechat/config/{type}")
String getPrivateKey(@PathVariable("type") String type, @RequestParam("tenantId") String tenantId);
}
@@ -0,0 +1,29 @@
package com.zlt.wechat.feign;
import com.central.common.constant.ServiceNameConstants;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.feign.fallback.WechatConfigServiceFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* @author zlt
*/
@FeignClient(name = ServiceNameConstants.USER_SERVICE, fallbackFactory = WechatConfigServiceFallbackFactory.class, dismiss404 = true)
public interface WechatConfigService {
/**
* feign rpc访问远程/wechat/config/{type}接口
* 查询微信配置对象WechatConfig
*
* @param type
* @return
*/
@GetMapping(value = "/wechat/config/{type}")
WechatConfig getConfig(@PathVariable("type") String type,String tenantId);
@GetMapping(value = "/app/{id}")
WechatConfig getConfig(String appId);
}
@@ -0,0 +1,25 @@
package com.zlt.wechat.feign.fallback;
import com.zlt.wechat.feign.PrivateKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
/**
* userService降级工场
*
* @author zlt
* @date 2019/1/18
*/
@Slf4j
public class PrivateKeyServiceFallbackFactory implements FallbackFactory<PrivateKeyService> {
@Override
public PrivateKeyService create(Throwable throwable) {
return new PrivateKeyService() {
@Override
public String getPrivateKey(String tenantId,String type) {
return "";
}
};
}
}
@@ -0,0 +1,33 @@
package com.zlt.wechat.feign.fallback;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.feign.WechatConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
/**
* userService降级工场
*
* @author zlt
* @date 2019/1/18
*/
@Slf4j
public class WechatConfigServiceFallbackFactory implements FallbackFactory<WechatConfigService> {
@Override
public WechatConfigService create(Throwable throwable) {
return new WechatConfigService() {
@Override
public WechatConfig getConfig(String type,String tenantId) {
return new WechatConfig();
}
@Override
public WechatConfig getConfig(String appId) {
return null;
}
};
}
}
@@ -0,0 +1,4 @@
com.zlt.wechat.feign.fallback.PrivateKeyServiceFallbackFactory
com.zlt.wechat.feign.fallback.WechatConfigServiceFallbackFactory
@@ -13,8 +13,8 @@ import com.wechat.pay.java.service.payments.jsapi.model.SettleInfo;
import com.wechat.pay.java.service.profitsharing.ProfitsharingService;
import com.wechat.pay.java.service.refund.RefundService;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.service.WechatConfigService;
import com.zlt.wechat.service.PrivateKeyService;
import com.zlt.wechat.feign.PrivateKeyService;
import com.zlt.wechat.feign.WechatConfigService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
@@ -161,7 +161,6 @@ public class WechatAutoConfiguration {
executor.initialize();
return executor;
}
/**
* 获取当前商户ID
* 这里需要根据实际业务场景实现,可以从ThreadLocal、请求上下文等获取
@@ -1,2 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.zlt.wechat.config.WechatAutoConfiguration
com.zlt.wechat.config.WechatAutoConfiguration
+6 -3
View File
@@ -46,6 +46,11 @@
<artifactId>zlt-redis-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-wechat-common-core</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
@@ -94,9 +99,7 @@
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-wechat-common-core</artifactId>
<version>6.0.0</version>
<scope>compile</scope>
<artifactId>zlt-wechat-spring-boot-starter</artifactId>
</dependency>
</dependencies>
@@ -10,7 +10,7 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* @author zlt
*/
@EnableFeignClients
@EnableFeignClients(basePackages = {"com.central", "com.zlt.wechat.feign"})
@MapperScan(basePackages = "com.central.oauth.mapper")
@EnableFeignInterceptor
@EnableDiscoveryClient
@@ -74,7 +74,6 @@ public class AuthorizationServerConfig {
private final RegisteredClientService registeredClientService;
private final RedisSecurityContextRepository securityContextRepository;
private final AuthenticationSuccessHandler authenticationSuccessHandler;
/**
* spring security 默认的安全策略
*/
@@ -6,7 +6,7 @@ import com.central.common.model.SysUser;
import com.central.common.utils.LoginUserUtils;
import com.central.oauth.service.ZltUserDetailsService;
import com.zlt.wechat.entity.WechatConfig;
import com.zlt.wechat.service.WechatConfigService;
import com.zlt.wechat.feign.WechatConfigService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
@@ -64,7 +64,8 @@ public class UserDetailServiceImpl implements ZltUserDetailsService {
@Override
public WechatConfig loadMiniAppConfig(String appId) {
return wechatConfigService.getConfig(appId);
// return wechatConfigService.getConfig(appId);
return null;
}
@Override
@@ -9,43 +9,43 @@
export default {
dev: {
'/api-uaa/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-user/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-log/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-search/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-generator/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-file/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-order/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
'/api-device/': {
// target: 'http://119.45.169.13:9900',
target: 'http://127.0.0.1:9900',
target: 'http://119.45.169.13:9900',
// target: 'http://127.0.0.1:9900',
changeOrigin: true,
},
},