Demo 程序使用包的微信 Access Token 存储方式
This commit is contained in:
parent
a4d7226b7b
commit
e28b6f25bb
|
@ -4,7 +4,7 @@ import com.ossez.wechat.demo.common.enums.HttpClientType;
|
|||
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.WxMpConfigStorage;
|
||||
import com.ossez.wechat.oa.config.ConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
@ -19,7 +19,7 @@ public class WxMpServiceAutoConfiguration {
|
|||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public WeChatOfficialAccountService weChatOfficialAccountService(WxMpConfigStorage configStorage, WeChatOfficialAccountProperties wxMpProperties) {
|
||||
public WeChatOfficialAccountService weChatOfficialAccountService(ConfigStorage configStorage, WeChatOfficialAccountProperties wxMpProperties) {
|
||||
HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
|
||||
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
|
||||
|
||||
|
|
|
@ -4,15 +4,16 @@ import com.ossez.wechat.demo.common.enums.StorageType;
|
|||
import com.ossez.wechat.demo.properties.RedisProperties;
|
||||
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.JedisWxRedisOps;
|
||||
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.WxMpConfigStorage;
|
||||
import com.ossez.wechat.oa.config.impl.WxMpDefaultConfigImpl;
|
||||
import com.ossez.wechat.oa.config.impl.WxMpRedisConfigImpl;
|
||||
import com.ossez.wechat.oa.config.ConfigStorage;
|
||||
import com.ossez.wechat.oa.config.DefaultConfigStorage;
|
||||
import com.ossez.wechat.oa.config.impl.RedisConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
@ -35,125 +36,108 @@ import java.util.Set;
|
|||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpStorageAutoConfiguration {
|
||||
private final ApplicationContext applicationContext;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
private final WeChatOfficialAccountProperties wxMpProperties;
|
||||
private final WeChatOfficialAccountProperties wxMpProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMpConfigStorage() {
|
||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||
WxMpConfigStorage config;
|
||||
switch (type) {
|
||||
case Jedis:
|
||||
config = jedisConfigStorage();
|
||||
break;
|
||||
case RedisTemplate:
|
||||
config = redisTemplateConfigStorage();
|
||||
break;
|
||||
default:
|
||||
config = defaultConfigStorage();
|
||||
break;
|
||||
}
|
||||
// wx host config
|
||||
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
||||
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
||||
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
||||
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
||||
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
||||
config.setHostConfig(hostConfig);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxMpConfigStorage defaultConfigStorage() {
|
||||
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
||||
setWxMpInfo(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxMpConfigStorage jedisConfigStorage() {
|
||||
JedisPoolAbstract jedisPool;
|
||||
if (wxMpProperties.getConfigStorage() != null && wxMpProperties.getConfigStorage().getRedis() != null
|
||||
&& StringUtils.isNotEmpty(wxMpProperties.getConfigStorage().getRedis().getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||
setWxMpInfo(wxMpRedisConfig);
|
||||
return wxMpRedisConfig;
|
||||
}
|
||||
|
||||
private WxMpConfigStorage redisTemplateConfigStorage() {
|
||||
StringRedisTemplate redisTemplate = null;
|
||||
try {
|
||||
redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("stringRedisTemplate");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ConfigStorage.class)
|
||||
public ConfigStorage wxMpConfigStorage() {
|
||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||
ConfigStorage config;
|
||||
switch (type) {
|
||||
case RedisTemplate:
|
||||
config = redisTemplateConfigStorage();
|
||||
break;
|
||||
default:
|
||||
config = defaultConfigStorage();
|
||||
break;
|
||||
}
|
||||
// wx host config
|
||||
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
||||
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
||||
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
||||
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
||||
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
||||
config.setHostConfig(hostConfig);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
||||
private ConfigStorage defaultConfigStorage() {
|
||||
DefaultConfigStorage config = new DefaultConfigStorage();
|
||||
setWxMpInfo(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||
|
||||
setWxMpInfo(wxMpRedisConfig);
|
||||
return wxMpRedisConfig;
|
||||
}
|
||||
private ConfigStorage redisTemplateConfigStorage() {
|
||||
StringRedisTemplate redisTemplate = null;
|
||||
try {
|
||||
redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("stringRedisTemplate");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
private void setWxMpInfo(WxMpDefaultConfigImpl 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());
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
||||
}
|
||||
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
}
|
||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||
RedisConfigStorage wxMpRedisConfig = new RedisConfigStorage(redisOps,
|
||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||
|
||||
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);
|
||||
setWxMpInfo(wxMpRedisConfig);
|
||||
return wxMpRedisConfig;
|
||||
}
|
||||
|
||||
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
|
||||
redis.getDatabase());
|
||||
}
|
||||
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());
|
||||
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,11 +41,12 @@ public class WeChatController {
|
|||
@ResponseBody
|
||||
public String getAccessToken() throws WxErrorException {
|
||||
|
||||
Student student = new Student(
|
||||
"Eng2015001", "John Doe", Student.Gender.MALE, 1);
|
||||
studentRepository.save(student);
|
||||
|
||||
return weChatOfficialAccountService.getAccessToken(true);
|
||||
weChatOfficialAccountService.getAccessToken(true);
|
||||
|
||||
|
||||
|
||||
return "ss" ;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -48,7 +48,7 @@ spring.redis.port=6379
|
|||
|
||||
# KEY
|
||||
wechat.official-account.app-id = wx637b82a7f94123ef
|
||||
wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925
|
||||
wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925+
|
||||
wechat.official-account.token = 0b01a700891d4a2f93a4323771c32455
|
||||
wechat.official-account.aes-key = aesKey
|
||||
|
||||
|
|
Loading…
Reference in New Issue