mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-10-28 04:29:07 +08:00
🎨 修复微信开发平台部分问题,并对企业微信新增多种 redis 存储实现支持
This commit is contained in:
@ -20,7 +20,7 @@
|
||||
wx.cp.aes-key = @aes-key
|
||||
wx.cp.agent-id = @agent-id
|
||||
# ConfigStorage 配置(选填)
|
||||
wx.cp.config-storage.type=memory # memory 默认,目前只支持 memory 类型,可以自行扩展 redis 等类型
|
||||
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
|
||||
# http 客户端配置(选填)
|
||||
wx.cp.config-storage.http-proxy-host=
|
||||
wx.cp.config-storage.http-proxy-port=
|
||||
|
||||
@ -18,6 +18,18 @@
|
||||
<artifactId>weixin-java-cp</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@ -1,6 +1,9 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.config;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInJedisConfigStorageConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInMemoryConfigStorageConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInRedisTemplateConfigStorageConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.storage.WxCpInRedissonConfigStorageConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
@ -12,7 +15,10 @@ import org.springframework.context.annotation.Import;
|
||||
*/
|
||||
@Configuration
|
||||
@Import({
|
||||
WxCpInMemoryConfigStorageConfiguration.class
|
||||
WxCpInMemoryConfigStorageConfiguration.class,
|
||||
WxCpInJedisConfigStorageConfiguration.class,
|
||||
WxCpInRedissonConfigStorageConfiguration.class,
|
||||
WxCpInRedisTemplateConfigStorageConfiguration.class
|
||||
})
|
||||
public class WxCpStorageAutoConfiguration {
|
||||
}
|
||||
|
||||
@ -3,6 +3,7 @@ package com.binarywang.spring.starter.wxjava.cp.properties;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@ -61,6 +62,17 @@ public class WxCpProperties {
|
||||
*/
|
||||
private StorageType type = StorageType.memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀
|
||||
*/
|
||||
private String keyPrefix = "wx:cp";
|
||||
|
||||
/**
|
||||
* redis连接配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private WxCpRedisProperties redis = new WxCpRedisProperties();
|
||||
|
||||
/**
|
||||
* http代理主机
|
||||
*/
|
||||
@ -104,6 +116,18 @@ public class WxCpProperties {
|
||||
/**
|
||||
* 内存
|
||||
*/
|
||||
memory
|
||||
memory,
|
||||
/**
|
||||
* jedis
|
||||
*/
|
||||
jedis,
|
||||
/**
|
||||
* redisson
|
||||
*/
|
||||
redisson,
|
||||
/**
|
||||
* redistemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,46 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Redis配置.
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/04/23
|
||||
*/
|
||||
@Data
|
||||
public class WxCpRedisProperties implements Serializable {
|
||||
private static final long serialVersionUID = -5924815351660074401L;
|
||||
|
||||
/**
|
||||
* 主机地址.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
*/
|
||||
private int port = 6379;
|
||||
|
||||
/**
|
||||
* 密码.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 超时.
|
||||
*/
|
||||
private int timeout = 2000;
|
||||
|
||||
/**
|
||||
* 数据库.
|
||||
*/
|
||||
private int database = 0;
|
||||
|
||||
private Integer maxActive;
|
||||
private Integer maxIdle;
|
||||
private Integer maxWaitMillis;
|
||||
private Integer minIdle;
|
||||
}
|
||||
@ -15,8 +15,8 @@ public abstract class AbstractWxCpConfigStorageConfiguration {
|
||||
protected WxCpDefaultConfigImpl config(WxCpDefaultConfigImpl config, WxCpProperties properties) {
|
||||
String corpId = properties.getCorpId();
|
||||
String corpSecret = properties.getCorpSecret();
|
||||
String token = properties.getToken();
|
||||
Integer agentId = properties.getAgentId();
|
||||
String token = properties.getToken();
|
||||
String aesKey = properties.getAesKey();
|
||||
// 企业微信,私钥,会话存档路径
|
||||
String msgAuditPriKey = properties.getMsgAuditPriKey();
|
||||
@ -24,12 +24,10 @@ public abstract class AbstractWxCpConfigStorageConfiguration {
|
||||
|
||||
config.setCorpId(corpId);
|
||||
config.setCorpSecret(corpSecret);
|
||||
config.setAgentId(agentId);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
config.setToken(token);
|
||||
}
|
||||
if (agentId != null) {
|
||||
config.setAgentId(agentId);
|
||||
}
|
||||
if (StringUtils.isNotBlank(aesKey)) {
|
||||
config.setAesKey(aesKey);
|
||||
}
|
||||
|
||||
@ -0,0 +1,74 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.storage;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* 自动装配基于 jedis 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/04/23
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "jedis"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpInJedisConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
|
||||
private final WxCpProperties wxCpProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxCpConfigStorage.class)
|
||||
public WxCpConfigStorage wxOpenConfigStorage() {
|
||||
WxCpDefaultConfigImpl config = getConfigStorage();
|
||||
return this.config(config, wxCpProperties);
|
||||
}
|
||||
|
||||
private WxCpJedisConfigImpl getConfigStorage() {
|
||||
WxCpRedisProperties wxCpRedisProperties = wxCpProperties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (wxCpRedisProperties != null && StringUtils.isNotEmpty(wxCpRedisProperties.getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
return new WxCpJedisConfigImpl(jedisPool, wxCpProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool() {
|
||||
WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
|
||||
WxCpRedisProperties redis = storage.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);
|
||||
|
||||
return new JedisPool(config, redis.getHost(), redis.getPort(),
|
||||
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.storage;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisTemplate 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/04/23
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpInRedisTemplateConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
|
||||
private final WxCpProperties wxCpProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxCpConfigStorage.class)
|
||||
public WxCpConfigStorage wxOpenConfigStorage() {
|
||||
WxCpDefaultConfigImpl config = getConfigStorage();
|
||||
return this.config(config, wxCpProperties);
|
||||
}
|
||||
|
||||
private WxCpRedisTemplateConfigImpl getConfigStorage() {
|
||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
return new WxCpRedisTemplateConfigImpl(redisTemplate, wxCpProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.storage;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpRedissonConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisson 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/04/23
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpInRedissonConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
|
||||
private final WxCpProperties wxCpProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxCpConfigStorage.class)
|
||||
public WxCpConfigStorage wxOpenConfigStorage() {
|
||||
WxCpDefaultConfigImpl config = getConfigStorage();
|
||||
return this.config(config, wxCpProperties);
|
||||
}
|
||||
|
||||
private WxCpRedissonConfigImpl getConfigStorage() {
|
||||
WxCpRedisProperties redisProperties = wxCpProperties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient();
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxCpRedissonConfigImpl(redissonClient, wxCpProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient() {
|
||||
WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
|
||||
WxCpRedisProperties redis = storage.getRedis();
|
||||
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
|
||||
.setDatabase(redis.getDatabase())
|
||||
.setPassword(redis.getPassword());
|
||||
config.setTransportMode(TransportMode.NIO);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user