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.demo.properties.WeChatOfficialAccountProperties;
|
||||||
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
|
import com.ossez.wechat.oa.api.WeChatOfficialAccountService;
|
||||||
import com.ossez.wechat.oa.api.impl.okhttp.WeChatOfficialAccountServiceOkHttp;
|
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.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@ -19,7 +19,7 @@ public class WxMpServiceAutoConfiguration {
|
|||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
public WeChatOfficialAccountService weChatOfficialAccountService(WxMpConfigStorage configStorage, WeChatOfficialAccountProperties wxMpProperties) {
|
public WeChatOfficialAccountService weChatOfficialAccountService(ConfigStorage configStorage, WeChatOfficialAccountProperties wxMpProperties) {
|
||||||
HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
|
HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
|
||||||
WeChatOfficialAccountService weChatOfficialAccountService = new WeChatOfficialAccountServiceOkHttp();
|
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.RedisProperties;
|
||||||
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
|
import com.ossez.wechat.demo.properties.WeChatOfficialAccountProperties;
|
||||||
import com.google.common.collect.Sets;
|
import com.google.common.collect.Sets;
|
||||||
|
import com.ossez.wechat.oa.config.ConfigStorage;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import com.ossez.wechat.common.redis.JedisWxRedisOps;
|
import com.ossez.wechat.common.redis.JedisWxRedisOps;
|
||||||
import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps;
|
import com.ossez.wechat.common.redis.RedisTemplateWxRedisOps;
|
||||||
import com.ossez.wechat.common.redis.WxRedisOps;
|
import com.ossez.wechat.common.redis.WxRedisOps;
|
||||||
import com.ossez.wechat.oa.config.WxMpHostConfig;
|
import com.ossez.wechat.oa.config.WxMpHostConfig;
|
||||||
import com.ossez.wechat.oa.config.WxMpConfigStorage;
|
import com.ossez.wechat.oa.config.ConfigStorage;
|
||||||
import com.ossez.wechat.oa.config.impl.WxMpDefaultConfigImpl;
|
import com.ossez.wechat.oa.config.DefaultConfigStorage;
|
||||||
import com.ossez.wechat.oa.config.impl.WxMpRedisConfigImpl;
|
import com.ossez.wechat.oa.config.impl.RedisConfigStorage;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
@ -35,125 +36,108 @@ import java.util.Set;
|
|||||||
@Configuration
|
@Configuration
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class WxMpStorageAutoConfiguration {
|
public class WxMpStorageAutoConfiguration {
|
||||||
private final ApplicationContext applicationContext;
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
private final WeChatOfficialAccountProperties wxMpProperties;
|
private final WeChatOfficialAccountProperties wxMpProperties;
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(WxMpConfigStorage.class)
|
@ConditionalOnMissingBean(ConfigStorage.class)
|
||||||
public WxMpConfigStorage wxMpConfigStorage() {
|
public ConfigStorage wxMpConfigStorage() {
|
||||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||||
WxMpConfigStorage config;
|
ConfigStorage config;
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case Jedis:
|
case RedisTemplate:
|
||||||
config = jedisConfigStorage();
|
config = redisTemplateConfigStorage();
|
||||||
break;
|
break;
|
||||||
case RedisTemplate:
|
default:
|
||||||
config = redisTemplateConfigStorage();
|
config = defaultConfigStorage();
|
||||||
break;
|
break;
|
||||||
default:
|
}
|
||||||
config = defaultConfigStorage();
|
// wx host config
|
||||||
break;
|
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
||||||
}
|
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
||||||
// wx host config
|
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
||||||
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
||||||
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
||||||
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
config.setHostConfig(hostConfig);
|
||||||
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
}
|
||||||
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
return config;
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (null == redisTemplate) {
|
private ConfigStorage defaultConfigStorage() {
|
||||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
DefaultConfigStorage config = new DefaultConfigStorage();
|
||||||
|
setWxMpInfo(config);
|
||||||
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
|
||||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
|
||||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
|
||||||
|
|
||||||
setWxMpInfo(wxMpRedisConfig);
|
private ConfigStorage redisTemplateConfigStorage() {
|
||||||
return wxMpRedisConfig;
|
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) {
|
if (null == redisTemplate) {
|
||||||
WeChatOfficialAccountProperties properties = wxMpProperties;
|
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
||||||
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());
|
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
RedisConfigStorage wxMpRedisConfig = new RedisConfigStorage(redisOps,
|
||||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
|
||||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private JedisPoolAbstract getJedisPool() {
|
setWxMpInfo(wxMpRedisConfig);
|
||||||
RedisProperties redis = wxMpProperties.getConfigStorage().getRedis();
|
return wxMpRedisConfig;
|
||||||
|
|
||||||
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(),
|
private void setWxMpInfo(DefaultConfigStorage config) {
|
||||||
redis.getDatabase());
|
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
|
@ResponseBody
|
||||||
public String getAccessToken() throws WxErrorException {
|
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
|
# KEY
|
||||||
wechat.official-account.app-id = wx637b82a7f94123ef
|
wechat.official-account.app-id = wx637b82a7f94123ef
|
||||||
wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925
|
wechat.official-account.secret = 343cecbc44d69e45367a65cc9b4d3925+
|
||||||
wechat.official-account.token = 0b01a700891d4a2f93a4323771c32455
|
wechat.official-account.token = 0b01a700891d4a2f93a4323771c32455
|
||||||
wechat.official-account.aes-key = aesKey
|
wechat.official-account.aes-key = aesKey
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user