微信中间访问 Token 的存储变量移动到 WeChat-J common 模块中

This commit is contained in:
YuCheng Hu 2023-01-29 16:53:51 -05:00
parent dc9d02f0dc
commit 206015cc41
5 changed files with 70 additions and 119 deletions

View File

@ -1,10 +1,10 @@
package com.ossez.wechat.demo.config;
import com.ossez.wechat.common.config.ConfigStorage;
import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
import com.ossez.wechat.oa.config.ConfigStorage;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
@ -28,7 +28,7 @@ public class WeChatConfiguration {
@Bean
@ConditionalOnMissingBean
public WeChatOfficialAccountService weChatOfficialAccountService(ConfigStorage configStorage, WeChatOfficialAccountProperties weChatOfficialAccountProperties) {
HttpClientCategory httpClientCategory = weChatOfficialAccountProperties.getConfigStorage().getHttpClientCategory();
HttpClientCategory httpClientCategory = weChatOfficialAccountProperties.getWeChatDataStorage().getHttpClientCategory();
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
weChatOfficialAccountService.setWxMpConfigStorage(configStorage);

View File

@ -1,29 +1,22 @@
package com.ossez.wechat.demo.config;
import com.ossez.wechat.common.config.ConfigStorage;
import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.properties.RedisProperties;
import com.ossez.wechat.demo.model.entity.WeChatDataStorage;
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
import com.google.common.collect.Sets;
import com.ossez.wechat.oa.config.ConfigStorage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps;
import com.ossez.wechat.common.redis.WxRedisOps;
import com.ossez.wechat.oa.config.WxMpHostConfig;
import com.ossez.wechat.oa.config.DefaultConfigStorage;
import com.ossez.wechat.oa.config.impl.RedisConfigStorage;
import com.ossez.wechat.common.config.WxMpHostConfig;
import com.ossez.wechat.common.config.DefaultConfigStorage;
import com.ossez.wechat.common.config.RedisConfigStorage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolAbstract;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import java.util.Set;
/**
* 微信公众号存储策略自动配置.
@ -36,12 +29,12 @@ import java.util.Set;
public class WeChatStorageAutoConfiguration {
private final ApplicationContext applicationContext;
private final WeChatOfficialAccountProperties wxMpProperties;
private final WeChatOfficialAccountProperties weChatOfficialAccountProperties;
@Bean
@ConditionalOnMissingBean(ConfigStorage.class)
public ConfigStorage wxMpConfigStorage() {
StorageCategory type = wxMpProperties.getConfigStorage().getType();
StorageCategory type = weChatOfficialAccountProperties.getWeChatDataStorage().getType();
ConfigStorage config;
switch (type) {
case REDIS:
@ -52,11 +45,11 @@ public class WeChatStorageAutoConfiguration {
break;
}
// wx host config
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
if (null != weChatOfficialAccountProperties.getHosts() && StringUtils.isNotEmpty(weChatOfficialAccountProperties.getHosts().getApiHost())) {
WxMpHostConfig hostConfig = new WxMpHostConfig();
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
hostConfig.setApiHost(weChatOfficialAccountProperties.getHosts().getApiHost());
hostConfig.setMpHost(weChatOfficialAccountProperties.getHosts().getMpHost());
hostConfig.setOpenHost(weChatOfficialAccountProperties.getHosts().getOpenHost());
config.setHostConfig(hostConfig);
}
return config;
@ -64,7 +57,7 @@ public class WeChatStorageAutoConfiguration {
private ConfigStorage defaultConfigStorage() {
DefaultConfigStorage config = new DefaultConfigStorage();
setWxMpInfo(config);
configWeChatServer(config);
return config;
}
@ -89,53 +82,28 @@ public class WeChatStorageAutoConfiguration {
}
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
RedisConfigStorage wxMpRedisConfig = new RedisConfigStorage(redisOps,
wxMpProperties.getConfigStorage().getKeyPrefix());
RedisConfigStorage redisConfigStorage = new RedisConfigStorage(redisOps,
weChatOfficialAccountProperties.getWeChatDataStorage().getKeyPrefix());
setWxMpInfo(wxMpRedisConfig);
return wxMpRedisConfig;
configWeChatServer(redisConfigStorage);
return redisConfigStorage;
}
private void setWxMpInfo(DefaultConfigStorage config) {
WeChatOfficialAccountProperties properties = wxMpProperties;
WeChatOfficialAccountProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
config.setAppId(properties.getAppId());
config.setSecret(properties.getSecret());
config.setToken(properties.getToken());
config.setAesKey(properties.getAesKey());
private void configWeChatServer(DefaultConfigStorage defaultConfigStorage) {
WeChatOfficialAccountProperties properties = weChatOfficialAccountProperties;
WeChatDataStorage weChatDataStorage = weChatOfficialAccountProperties.getWeChatDataStorage();
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
defaultConfigStorage.setAppId(properties.getAppId());
defaultConfigStorage.setSecret(properties.getSecret());
defaultConfigStorage.setToken(properties.getToken());
defaultConfigStorage.setAesKey(properties.getAesKey());
defaultConfigStorage.setHttpProxyHost(weChatDataStorage.getHttpProxyHost());
defaultConfigStorage.setHttpProxyUsername(weChatDataStorage.getHttpProxyUsername());
defaultConfigStorage.setHttpProxyPassword(weChatDataStorage.getHttpProxyPassword());
if (weChatDataStorage.getHttpProxyPort() != null) {
defaultConfigStorage.setHttpProxyPort(weChatDataStorage.getHttpProxyPort());
}
}
private JedisPoolAbstract getJedisPool() {
RedisProperties redis = wxMpProperties.getConfigStorage().getRedis();
JedisPoolConfig config = new JedisPoolConfig();
if (redis.getMaxActive() != null) {
config.setMaxTotal(redis.getMaxActive());
}
if (redis.getMaxIdle() != null) {
config.setMaxIdle(redis.getMaxIdle());
}
if (redis.getMaxWaitMillis() != null) {
config.setMaxWaitMillis(redis.getMaxWaitMillis());
}
if (redis.getMinIdle() != null) {
config.setMinIdle(redis.getMinIdle());
}
config.setTestOnBorrow(true);
config.setTestWhileIdle(true);
if (StringUtils.isNotEmpty(redis.getSentinelIps())) {
Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(","));
return new JedisSentinelPool(redis.getSentinelName(), sentinels);
}
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
redis.getDatabase());
}
}

View File

@ -40,6 +40,7 @@ public class WeChatController {
@GetMapping("/token")
@ResponseBody
public String getAccessToken() throws WxErrorException {
log.debug("Get access token from WeChat");
return weChatOfficialAccountService.getAccessToken(true);
}

View File

@ -0,0 +1,30 @@
package com.ossez.wechat.demo.model.entity;
import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.properties.RedisProperties;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable;
import static com.ossez.wechat.demo.common.enums.StorageCategory.MEM;
@Getter
@Setter
@Accessors(chain = true)
public class WeChatDataStorage implements Serializable {
private static final long serialVersionUID = -94405301936095366L;
private StorageCategory type = MEM;
private String keyPrefix = "wx";
@NestedConfigurationProperty
private final RedisProperties redis = new RedisProperties();
private HttpClientCategory httpClientCategory = HttpClientCategory.OK_HTTP;
private String httpProxyHost;
private Integer httpProxyPort;
private String httpProxyUsername;
private String httpProxyPassword;
}

View File

@ -3,6 +3,7 @@ package com.ossez.wechat.demo.properties;
import com.ossez.wechat.demo.common.enums.HttpClientCategory;
import com.ossez.wechat.demo.common.enums.StorageCategory;
import com.ossez.wechat.demo.model.entity.WeChatDataStorage;
import com.ossez.wechat.demo.model.entity.WeChatHost;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ -14,67 +15,18 @@ import static com.ossez.wechat.demo.common.enums.StorageCategory.MEM;
/**
* WeChat Official Account Config
*
* @author YuCheng Hu
*/
@Data
@ConfigurationProperties(prefix = "wechat.official-account")
public class WeChatOfficialAccountProperties {
private String appId; //微信公众号 appId
private String secret; //微信公众号 secret
private String token; //微信公众号 token
private String aesKey; //微信公众号 aesKey
private WeChatHost hosts; //自定义 host 配置
private String appId; //微信公众号 appId
private String secret; //微信公众号 secret
private String token; //微信公众号 token
private String aesKey; //微信公众号 aesKey
private WeChatHost hosts; //自定义 host 配置
private final WeChatDataStorage weChatDataStorage = new WeChatDataStorage();
/**
* 存储策略
*/
private final ConfigStorage configStorage = new ConfigStorage();
@Data
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;
/**
* 存储类型.
*/
private StorageCategory type = MEM;
/**
* 指定key前缀.
*/
private String keyPrefix = "wx";
/**
* redis连接配置.
*/
@NestedConfigurationProperty
private final RedisProperties redis = new RedisProperties();
/**
* http客户端类型.
*/
private HttpClientCategory httpClientCategory = HttpClientCategory.OK_HTTP;
/**
* http代理主机.
*/
private String httpProxyHost;
/**
* http代理端口.
*/
private Integer httpProxyPort;
/**
* http代理用户名.
*/
private String httpProxyUsername;
/**
* http代理密码.
*/
private String httpProxyPassword;
}
}