🎨 格式化部分代码

This commit is contained in:
Binary Wang 2021-06-02 19:55:53 +08:00
parent c9fad3121c
commit fdd3a94131
7 changed files with 857 additions and 149 deletions

View File

@ -24,13 +24,31 @@ public interface WxCpConfigStorage {
/**
* 读取企业微信 API Url.
* 支持私有化企业微信服务器.
*
* @param path the path
* @return the api url
*/
String getApiUrl(String path);
/**
* Gets access token.
*
* @return the access token
*/
String getAccessToken();
/**
* Gets access token lock.
*
* @return the access token lock
*/
Lock getAccessTokenLock();
/**
* Is access token expired boolean.
*
* @return the boolean
*/
boolean isAccessTokenExpired();
/**
@ -38,14 +56,40 @@ public interface WxCpConfigStorage {
*/
void expireAccessToken();
/**
* Update access token.
*
* @param accessToken the access token
*/
void updateAccessToken(WxAccessToken accessToken);
/**
* Update access token.
*
* @param accessToken the access token
* @param expiresIn the expires in
*/
void updateAccessToken(String accessToken, int expiresIn);
/**
* Gets jsapi ticket.
*
* @return the jsapi ticket
*/
String getJsapiTicket();
/**
* Gets jsapi ticket lock.
*
* @return the jsapi ticket lock
*/
Lock getJsapiTicketLock();
/**
* Is jsapi ticket expired boolean.
*
* @return the boolean
*/
boolean isJsapiTicketExpired();
/**
@ -55,13 +99,31 @@ public interface WxCpConfigStorage {
/**
* 应该是线程安全的.
*
* @param jsapiTicket the jsapi ticket
* @param expiresInSeconds the expires in seconds
*/
void updateJsapiTicket(String jsapiTicket, int expiresInSeconds);
/**
* Gets agent jsapi ticket.
*
* @return the agent jsapi ticket
*/
String getAgentJsapiTicket();
/**
* Gets agent jsapi ticket lock.
*
* @return the agent jsapi ticket lock
*/
Lock getAgentJsapiTicketLock();
/**
* Is agent jsapi ticket expired boolean.
*
* @return the boolean
*/
boolean isAgentJsapiTicketExpired();
/**
@ -71,51 +133,114 @@ public interface WxCpConfigStorage {
/**
* 应该是线程安全的.
*
* @param jsapiTicket the jsapi ticket
* @param expiresInSeconds the expires in seconds
*/
void updateAgentJsapiTicket(String jsapiTicket, int expiresInSeconds);
/**
* Gets corp id.
*
* @return the corp id
*/
String getCorpId();
/**
* Gets corp secret.
*
* @return the corp secret
*/
String getCorpSecret();
/**
* Gets agent id.
*
* @return the agent id
*/
Integer getAgentId();
/**
* Gets token.
*
* @return the token
*/
String getToken();
/**
* Gets aes key.
*
* @return the aes key
*/
String getAesKey();
/**
* Gets expires time.
*
* @return the expires time
*/
long getExpiresTime();
/**
* Gets oauth 2 redirect uri.
*
* @return the oauth 2 redirect uri
*/
String getOauth2redirectUri();
/**
* Gets http proxy host.
*
* @return the http proxy host
*/
String getHttpProxyHost();
/**
* Gets http proxy port.
*
* @return the http proxy port
*/
int getHttpProxyPort();
/**
* Gets http proxy username.
*
* @return the http proxy username
*/
String getHttpProxyUsername();
/**
* Gets http proxy password.
*
* @return the http proxy password
*/
String getHttpProxyPassword();
/**
* Gets tmp dir file.
*
* @return the tmp dir file
*/
File getTmpDirFile();
/**
* http client builder.
*
* @return ApacheHttpClientBuilder
* @return ApacheHttpClientBuilder apache http client builder
*/
ApacheHttpClientBuilder getApacheHttpClientBuilder();
/**
* 是否自动刷新token
*
* @return .
* @return . boolean
*/
boolean autoRefreshToken();
/**
* 获取群机器人webhook的key
*
* @return key
* @return key webhook key
*/
String getWebhookKey();
}

View File

@ -25,106 +25,361 @@ public interface WxCpTpConfigStorage {
/**
* 读取企业微信 API Url.
* 支持私有化企业微信服务器.
*
* @param path the path
* @return the api url
*/
String getApiUrl(String path);
/**
* 第三方应用的suite access token相关
*
* @return the suite access token
*/
String getSuiteAccessToken();
/**
* 获取suite_access_token和剩余过期时间
*
* @return suite access token and the remaining expiration time
*/
WxAccessToken getSuiteAccessTokenEntity();
/**
* Is suite access token expired boolean.
*
* @return the boolean
*/
boolean isSuiteAccessTokenExpired();
//强制将suite access token过期掉.
/**
* Expire suite access token.
*/
//强制将suite access token过期掉.
void expireSuiteAccessToken();
/**
* Update suite access token.
*
* @param suiteAccessToken the suite access token
*/
void updateSuiteAccessToken(WxAccessToken suiteAccessToken);
/**
* Update suite access token.
*
* @param suiteAccessToken the suite access token
* @param expiresInSeconds the expires in seconds
*/
void updateSuiteAccessToken(String suiteAccessToken, int expiresInSeconds);
/**
* 第三方应用的suite ticket相关
*
* @return the suite ticket
*/
String getSuiteTicket();
/**
* Is suite ticket expired boolean.
*
* @return the boolean
*/
boolean isSuiteTicketExpired();
//强制将suite ticket过期掉.
/**
* Expire suite ticket.
*/
//强制将suite ticket过期掉.
void expireSuiteTicket();
//应该是线程安全的
/**
* Update suite ticket.
*
* @param suiteTicket the suite ticket
* @param expiresInSeconds the expires in seconds
*/
//应该是线程安全的
void updateSuiteTicket(String suiteTicket, int expiresInSeconds);
/**
* 第三方应用的其他配置来自于企微配置
*
* @return the suite id
*/
String getSuiteId();
/**
* Gets suite secret.
*
* @return the suite secret
*/
String getSuiteSecret();
// 第三方应用的token用来检查应用的签名
/**
* Gets token.
*
* @return the token
*/
// 第三方应用的token用来检查应用的签名
String getToken();
//第三方应用的EncodingAESKey用来检查签名
/**
* Gets aes key.
*
* @return the aes key
*/
//第三方应用的EncodingAESKey用来检查签名
String getAesKey();
/**
* 企微服务商企业ID & 企业secret
*
* @return the corp id
*/
String getCorpId();
/**
* Gets corp secret.
*
* @return the corp secret
*/
String getCorpSecret();
/**
* 服务商secret
*
* @return the provider secret
*/
String getProviderSecret();
/**
* 授权企业的access token相关
*
* @param authCorpId the auth corp id
* @return the access token
*/
String getAccessToken(String authCorpId);
/**
* Gets access token entity.
*
* @param authCorpId the auth corp id
* @return the access token entity
*/
WxAccessToken getAccessTokenEntity(String authCorpId);
/**
* Is access token expired boolean.
*
* @param authCorpId the auth corp id
* @return the boolean
*/
boolean isAccessTokenExpired(String authCorpId);
/**
* Expire access token.
*
* @param authCorpId the auth corp id
*/
void expireAccessToken(String authCorpId);
/**
* Update access token.
*
* @param authCorpId the auth corp id
* @param accessToken the access token
* @param expiredInSeconds the expired in seconds
*/
void updateAccessToken(String authCorpId, String accessToken, int expiredInSeconds);
/**
* 授权企业的js api ticket相关
*
* @param authCorpId the auth corp id
* @return the auth corp js api ticket
*/
String getAuthCorpJsApiTicket(String authCorpId);
/**
* Is auth corp js api ticket expired boolean.
*
* @param authCorpId the auth corp id
* @return the boolean
*/
boolean isAuthCorpJsApiTicketExpired(String authCorpId);
/**
* Expire auth corp js api ticket.
*
* @param authCorpId the auth corp id
*/
void expireAuthCorpJsApiTicket(String authCorpId);
/**
* Update auth corp js api ticket.
*
* @param authCorpId the auth corp id
* @param jsApiTicket the js api ticket
* @param expiredInSeconds the expired in seconds
*/
void updateAuthCorpJsApiTicket(String authCorpId, String jsApiTicket, int expiredInSeconds);
/**
* 授权企业的第三方应用js api ticket相关
*
* @param authCorpId the auth corp id
* @return the auth suite js api ticket
*/
String getAuthSuiteJsApiTicket(String authCorpId);
boolean isAuthSuiteJsApiTicketExpired(String authCorpId);
void expireAuthSuiteJsApiTicket(String authCorpId);
void updateAuthSuiteJsApiTicket(String authCorpId, String jsApiTicket, int expiredInSeconds);;
/**
* Is auth suite js api ticket expired boolean.
*
* @param authCorpId the auth corp id
* @return the boolean
*/
boolean isAuthSuiteJsApiTicketExpired(String authCorpId);
/**
* Expire auth suite js api ticket.
*
* @param authCorpId the auth corp id
*/
void expireAuthSuiteJsApiTicket(String authCorpId);
/**
* Update auth suite js api ticket.
*
* @param authCorpId the auth corp id
* @param jsApiTicket the js api ticket
* @param expiredInSeconds the expired in seconds
*/
void updateAuthSuiteJsApiTicket(String authCorpId, String jsApiTicket, int expiredInSeconds);
;
/**
* Is provider token expired boolean.
*
* @return the boolean
*/
boolean isProviderTokenExpired();
/**
* Update provider token.
*
* @param providerToken the provider token
* @param expiredInSeconds the expired in seconds
*/
void updateProviderToken(String providerToken, int expiredInSeconds);
/**
* Gets provider token.
*
* @return the provider token
*/
String getProviderToken();
/**
* Gets provider token entity.
*
* @return the provider token entity
*/
WxCpProviderToken getProviderTokenEntity();
// 强制过期
/**
* Expire provider token.
*/
// 强制过期
void expireProviderToken();
/**
* 网络代理相关
*
* @return the http proxy host
*/
String getHttpProxyHost();
/**
* Gets http proxy port.
*
* @return the http proxy port
*/
int getHttpProxyPort();
/**
* Gets http proxy username.
*
* @return the http proxy username
*/
String getHttpProxyUsername();
/**
* Gets http proxy password.
*
* @return the http proxy password
*/
String getHttpProxyPassword();
/**
* Gets apache http client builder.
*
* @return the apache http client builder
*/
ApacheHttpClientBuilder getApacheHttpClientBuilder();
/**
* Auto refresh token boolean.
*
* @return the boolean
*/
boolean autoRefreshToken();
// 毫无相关性的代码
/**
* Gets tmp dir file.
*
* @return the tmp dir file
*/
// 毫无相关性的代码
@Deprecated
File getTmpDirFile();
/**
* Gets provider access token lock.
*
* @return the provider access token lock
*/
Lock getProviderAccessTokenLock();
/**
* Gets suite access token lock.
*
* @return the suite access token lock
*/
Lock getSuiteAccessTokenLock();
/**
* Gets access token lock.
*
* @param authCorpId the auth corp id
* @return the access token lock
*/
Lock getAccessTokenLock(String authCorpId);
/**
* Gets auth corp jsapi ticket lock.
*
* @param authCorpId the auth corp id
* @return the auth corp jsapi ticket lock
*/
Lock getAuthCorpJsapiTicketLock(String authCorpId);
/**
* Gets suite jsapi ticket lock.
*
* @param authCorpId the auth corp id
* @return the suite jsapi ticket lock
*/
Lock getSuiteJsapiTicketLock(String authCorpId);
}

View File

@ -18,30 +18,39 @@ import java.util.concurrent.locks.ReentrantLock;
*/
public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
private static final long serialVersionUID = 1154541446729462780L;
/**
* The Access token.
*/
protected volatile String accessToken;
/**
* The Access token lock.
*/
protected transient Lock accessTokenLock = new ReentrantLock();
/**
* The Agent id.
*/
protected volatile Integer agentId;
/**
* The Jsapi ticket lock.
*/
protected transient Lock jsapiTicketLock = new ReentrantLock();
/**
* The Agent jsapi ticket lock.
*/
protected transient Lock agentJsapiTicketLock = new ReentrantLock();
private volatile String corpId;
private volatile String corpSecret;
private volatile String token;
protected volatile String accessToken;
protected transient Lock accessTokenLock = new ReentrantLock();
private volatile String aesKey;
protected volatile Integer agentId;
private volatile long expiresTime;
private volatile String oauth2redirectUri;
private volatile String httpProxyHost;
private volatile int httpProxyPort;
private volatile String httpProxyUsername;
private volatile String httpProxyPassword;
private volatile String jsapiTicket;
protected transient Lock jsapiTicketLock = new ReentrantLock();
private volatile long jsapiTicketExpiresTime;
private volatile String agentJsapiTicket;
protected transient Lock agentJsapiTicketLock = new ReentrantLock();
private volatile long agentJsapiTicketExpiresTime;
private volatile File tmpDirFile;
@ -70,15 +79,20 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.accessToken;
}
/**
* Sets access token.
*
* @param accessToken the access token
*/
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override
public Lock getAccessTokenLock() {
return this.accessTokenLock;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
@Override
public boolean isAccessTokenExpired() {
return System.currentTimeMillis() > this.expiresTime;
@ -105,19 +119,34 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.jsapiTicket;
}
/**
* Sets jsapi ticket.
*
* @param jsapiTicket the jsapi ticket
*/
public void setJsapiTicket(String jsapiTicket) {
this.jsapiTicket = jsapiTicket;
}
@Override
public Lock getJsapiTicketLock() {
return this.jsapiTicketLock;
}
public void setJsapiTicket(String jsapiTicket) {
this.jsapiTicket = jsapiTicket;
}
/**
* Gets jsapi ticket expires time.
*
* @return the jsapi ticket expires time
*/
public long getJsapiTicketExpiresTime() {
return this.jsapiTicketExpiresTime;
}
/**
* Sets jsapi ticket expires time.
*
* @param jsapiTicketExpiresTime the jsapi ticket expires time
*/
public void setJsapiTicketExpiresTime(long jsapiTicketExpiresTime) {
this.jsapiTicketExpiresTime = jsapiTicketExpiresTime;
}
@ -171,6 +200,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.corpId;
}
/**
* Sets corp id.
*
* @param corpId the corp id
*/
public void setCorpId(String corpId) {
this.corpId = corpId;
}
@ -180,6 +214,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.corpSecret;
}
/**
* Sets corp secret.
*
* @param corpSecret the corp secret
*/
public void setCorpSecret(String corpSecret) {
this.corpSecret = corpSecret;
}
@ -189,6 +228,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.token;
}
/**
* Sets token.
*
* @param token the token
*/
public void setToken(String token) {
this.token = token;
}
@ -198,6 +242,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.expiresTime;
}
/**
* Sets expires time.
*
* @param expiresTime the expires time
*/
public void setExpiresTime(long expiresTime) {
this.expiresTime = expiresTime;
}
@ -207,6 +256,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.aesKey;
}
/**
* Sets aes key.
*
* @param aesKey the aes key
*/
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}
@ -216,6 +270,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.agentId;
}
/**
* Sets agent id.
*
* @param agentId the agent id
*/
public void setAgentId(Integer agentId) {
this.agentId = agentId;
}
@ -225,6 +284,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.oauth2redirectUri;
}
/**
* Sets oauth 2 redirect uri.
*
* @param oauth2redirectUri the oauth 2 redirect uri
*/
public void setOauth2redirectUri(String oauth2redirectUri) {
this.oauth2redirectUri = oauth2redirectUri;
}
@ -234,6 +298,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.httpProxyHost;
}
/**
* Sets http proxy host.
*
* @param httpProxyHost the http proxy host
*/
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
@ -243,6 +312,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.httpProxyPort;
}
/**
* Sets http proxy port.
*
* @param httpProxyPort the http proxy port
*/
public void setHttpProxyPort(int httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
@ -252,6 +326,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.httpProxyUsername;
}
/**
* Sets http proxy username.
*
* @param httpProxyUsername the http proxy username
*/
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
@ -261,6 +340,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.httpProxyPassword;
}
/**
* Sets http proxy password.
*
* @param httpProxyPassword the http proxy password
*/
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
@ -275,6 +359,11 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.tmpDirFile;
}
/**
* Sets tmp dir file.
*
* @param tmpDirFile the tmp dir file
*/
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
@ -284,6 +373,15 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.apacheHttpClientBuilder;
}
/**
* Sets apache http client builder.
*
* @param apacheHttpClientBuilder the apache http client builder
*/
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
@Override
public boolean autoRefreshToken() {
return true;
@ -294,10 +392,12 @@ public class WxCpDefaultConfigImpl implements WxCpConfigStorage, Serializable {
return this.webhookKey;
}
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
/**
* Sets webhook key.
*
* @param webhookKey the webhook key
* @return the webhook key
*/
public WxCpDefaultConfigImpl setWebhookKey(String webhookKey) {
this.webhookKey = webhookKey;
return this;

View File

@ -17,9 +17,9 @@ import java.util.concurrent.locks.ReentrantLock;
* 使用说明本实现仅供参考并不完整.
* 比如为减少项目依赖未加入redis分布式锁的实现如有需要请自行实现
* </pre>
* @deprecated 不建议使用如有需要请自行改造实现加入到自己的项目中并引用
*
* @author gaigeshen
* @deprecated 不建议使用 如有需要请自行改造实现加入到自己的项目中并引用
*/
@Deprecated
public class WxCpRedisConfigImpl implements WxCpConfigStorage {
@ -31,6 +31,10 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
private static final String AGENT_JSAPI_TICKET_EXPIRES_TIME_KEY = "WX_CP_AGENT_%s_JSAPI_TICKET_EXPIRES_TIME";
private final JedisPool jedisPool;
/**
* The Base api url.
*/
protected volatile String baseApiUrl;
private volatile String corpId;
private volatile String corpSecret;
private volatile String token;
@ -43,11 +47,65 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
private volatile String httpProxyPassword;
private volatile File tmpDirFile;
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
protected volatile String baseApiUrl;
private volatile String webhookKey;
/**
* Instantiates a new Wx cp redis config.
*
* @param jedisPool the jedis pool
*/
public WxCpRedisConfigImpl(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
/**
* Instantiates a new Wx cp redis config.
*
* @param host the host
* @param port the port
*/
public WxCpRedisConfigImpl(String host, int port) {
jedisPool = new JedisPool(host, port);
}
/**
* Instantiates a new Wx cp redis config.
*
* @param poolConfig the pool config
* @param host the host
* @param port the port
*/
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port) {
jedisPool = new JedisPool(poolConfig, host, port);
}
/**
* Instantiates a new Wx cp redis config.
*
* @param poolConfig the pool config
* @param host the host
* @param port the port
* @param timeout the timeout
* @param password the password
*/
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port, int timeout, String password) {
jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
}
/**
* Instantiates a new Wx cp redis config.
*
* @param poolConfig the pool config
* @param host the host
* @param port the port
* @param timeout the timeout
* @param password the password
* @param database the database
*/
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port, int timeout, String password, int database) {
jedisPool = new JedisPool(poolConfig, host, port, timeout, password, database);
}
@Override
public void setBaseApiUrl(String baseUrl) {
this.baseApiUrl = baseUrl;
@ -61,26 +119,6 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return baseApiUrl + path;
}
public WxCpRedisConfigImpl(JedisPool jedisPool) {
this.jedisPool = jedisPool;
}
public WxCpRedisConfigImpl(String host, int port) {
jedisPool = new JedisPool(host, port);
}
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port) {
jedisPool = new JedisPool(poolConfig, host, port);
}
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port, int timeout, String password) {
jedisPool = new JedisPool(poolConfig, host, port, timeout, password);
}
public WxCpRedisConfigImpl(JedisPoolConfig poolConfig, String host, int port, int timeout, String password, int database) {
jedisPool = new JedisPool(poolConfig, host, port, timeout, password, database);
}
/**
* This method will be destroy jedis pool
*/
@ -226,6 +264,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.corpId;
}
/**
* Sets corp id.
*
* @param corpId the corp id
*/
public void setCorpId(String corpId) {
this.corpId = corpId;
}
@ -235,6 +278,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.corpSecret;
}
/**
* Sets corp secret.
*
* @param corpSecret the corp secret
*/
public void setCorpSecret(String corpSecret) {
this.corpSecret = corpSecret;
}
@ -244,6 +292,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.agentId;
}
/**
* Sets agent id.
*
* @param agentId the agent id
*/
public void setAgentId(Integer agentId) {
this.agentId = agentId;
}
@ -253,6 +306,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.token;
}
/**
* Sets token.
*
* @param token the token
*/
public void setToken(String token) {
this.token = token;
}
@ -262,6 +320,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.aesKey;
}
/**
* Sets aes key.
*
* @param aesKey the aes key
*/
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}
@ -285,6 +348,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.oauth2redirectUri;
}
/**
* Sets oauth 2 redirect uri.
*
* @param oauth2redirectUri the oauth 2 redirect uri
*/
public void setOauth2redirectUri(String oauth2redirectUri) {
this.oauth2redirectUri = oauth2redirectUri;
}
@ -294,6 +362,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.httpProxyHost;
}
/**
* Sets http proxy host.
*
* @param httpProxyHost the http proxy host
*/
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
@ -303,6 +376,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.httpProxyPort;
}
/**
* Sets http proxy port.
*
* @param httpProxyPort the http proxy port
*/
public void setHttpProxyPort(int httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
@ -314,6 +392,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
// ============================ Setters below
/**
* Sets http proxy username.
*
* @param httpProxyUsername the http proxy username
*/
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
@ -323,6 +406,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.httpProxyPassword;
}
/**
* Sets http proxy password.
*
* @param httpProxyPassword the http proxy password
*/
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
@ -332,6 +420,11 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.tmpDirFile;
}
/**
* Sets tmp dir file.
*
* @param tmpDirFile the tmp dir file
*/
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
@ -341,6 +434,15 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.apacheHttpClientBuilder;
}
/**
* Sets apache http client builder.
*
* @param apacheHttpClientBuilder the apache http client builder
*/
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
@Override
public boolean autoRefreshToken() {
return true;
@ -351,8 +453,4 @@ public class WxCpRedisConfigImpl implements WxCpConfigStorage {
return this.getWebhookKey();
}
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
}

View File

@ -14,33 +14,72 @@ import java.util.concurrent.locks.Lock;
* 基于Redisson的实现
*
* @author yuanqixun
* @date 2020/5/13
* @date 2020 /5/13
*/
public class WxCpRedissonConfigImpl extends WxCpDefaultConfigImpl {
/**
* The constant LOCK_KEY.
*/
protected final static String LOCK_KEY = "wechat_cp_lock:";
/**
* The constant CP_ACCESS_TOKEN_KEY.
*/
protected final static String CP_ACCESS_TOKEN_KEY = "wechat_cp_access_token_key:";
/**
* The constant CP_JSAPI_TICKET_KEY.
*/
protected final static String CP_JSAPI_TICKET_KEY = "wechat_cp_jsapi_ticket_key:";
/**
* The constant CP_AGENT_JSAPI_TICKET_KEY.
*/
protected final static String CP_AGENT_JSAPI_TICKET_KEY = "wechat_cp_agent_jsapi_ticket_key:";
private final WxRedisOps redisOps;
/**
* redis 存储的 key 的前缀可为空
*/
protected String keyPrefix;
/**
* The Access token key.
*/
protected String accessTokenKey;
/**
* The Jsapi ticket key.
*/
protected String jsapiTicketKey;
/**
* The Agent jsapi ticket key.
*/
protected String agentJsapiTicketKey;
/**
* The Lock key.
*/
protected String lockKey;
private final WxRedisOps redisOps;
/**
* Instantiates a new Wx cp redisson config.
*
* @param redissonClient the redisson client
* @param keyPrefix the key prefix
*/
public WxCpRedissonConfigImpl(@NonNull RedissonClient redissonClient, String keyPrefix) {
this(new RedissonWxRedisOps(redissonClient), keyPrefix);
}
/**
* Instantiates a new Wx cp redisson config.
*
* @param redissonClient the redisson client
*/
public WxCpRedissonConfigImpl(@NonNull RedissonClient redissonClient) {
this(redissonClient, null);
}
/**
* Instantiates a new Wx cp redisson config.
*
* @param redisOps the redis ops
* @param keyPrefix the key prefix
*/
public WxCpRedissonConfigImpl(@NonNull WxRedisOps redisOps, String keyPrefix) {
this.redisOps = redisOps;
this.keyPrefix = keyPrefix;
@ -63,6 +102,12 @@ public class WxCpRedissonConfigImpl extends WxCpDefaultConfigImpl {
agentJsapiTicketKey = prefix + CP_AGENT_JSAPI_TICKET_KEY.concat(ukey);
}
/**
* Gets lock by key.
*
* @param key the key
* @return the lock by key
*/
protected Lock getLockByKey(String key) {
return redisOps.getLock(key);
}

View File

@ -22,7 +22,12 @@ import java.util.concurrent.locks.ReentrantLock;
*/
public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializable {
private static final long serialVersionUID = 6678780920621872824L;
// locker
private final transient Map<String, Lock> providerAccessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> suiteAccessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> accessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> authCorpJsapiTicketLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> authSuiteJsapiTicketLocker = new ConcurrentHashMap<>();
private volatile String corpId;
private volatile String corpSecret;
/**
@ -31,46 +36,29 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
private volatile String providerSecret;
private volatile String providerToken;
private volatile long providerTokenExpiresTime;
private volatile String suiteId;
private volatile String suiteSecret;
private volatile String token;
private volatile String suiteAccessToken;
private volatile long suiteAccessTokenExpiresTime;
private volatile String aesKey;
private volatile String suiteTicket;
private volatile long suiteTicketExpiresTime;
private volatile String oauth2redirectUri;
private volatile Map<String, String> authCorpAccessTokenMap = new HashMap<>();
private volatile Map<String, Long> authCorpAccessTokenExpireTimeMap = new HashMap<>();
private volatile Map<String, String> authCorpJsApiTicketMap = new HashMap<>();
private volatile Map<String, Long> authCorpJsApiTicketExpireTimeMap = new HashMap<>();
private volatile Map<String, String> authSuiteJsApiTicketMap = new HashMap<>();
private volatile Map<String, Long> authSuiteJsApiTicketExpireTimeMap = new HashMap<>();
private volatile String httpProxyHost;
private volatile int httpProxyPort;
private volatile String httpProxyUsername;
private volatile String httpProxyPassword;
private volatile File tmpDirFile;
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
private volatile String baseApiUrl;
// locker
private final transient Map<String, Lock> providerAccessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> suiteAccessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> accessTokenLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> authCorpJsapiTicketLocker = new ConcurrentHashMap<>();
private final transient Map<String, Lock> authSuiteJsapiTicketLocker = new ConcurrentHashMap<>();
@Override
public void setBaseApiUrl(String baseUrl) {
this.baseApiUrl = baseUrl;
@ -89,6 +77,15 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.suiteAccessToken;
}
/**
* Sets suite access token.
*
* @param suiteAccessToken the suite access token
*/
public void setSuiteAccessToken(String suiteAccessToken) {
this.suiteAccessToken = suiteAccessToken;
}
@Override
public WxAccessToken getSuiteAccessTokenEntity() {
WxAccessToken accessToken = new WxAccessToken();
@ -98,10 +95,6 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return accessToken;
}
public void setSuiteAccessToken(String suiteAccessToken) {
this.suiteAccessToken = suiteAccessToken;
}
@Override
public boolean isSuiteAccessTokenExpired() {
return System.currentTimeMillis() > this.suiteAccessTokenExpiresTime;
@ -123,6 +116,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
this.suiteAccessTokenExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
}
/**
* Sets suite access token expires time.
*
* @param suiteAccessTokenExpiresTime the suite access token expires time
*/
@Deprecated
public void setSuiteAccessTokenExpiresTime(long suiteAccessTokenExpiresTime) {
this.suiteAccessTokenExpiresTime = suiteAccessTokenExpiresTime;
@ -133,6 +131,16 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.suiteTicket;
}
/**
* Sets suite ticket.
*
* @param suiteTicket the suite ticket
*/
@Deprecated
public void setSuiteTicket(String suiteTicket) {
this.suiteTicket = suiteTicket;
}
@Override
public boolean isSuiteTicketExpired() {
return System.currentTimeMillis() > this.suiteTicketExpiresTime;
@ -150,17 +158,21 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
this.suiteTicketExpiresTime = System.currentTimeMillis() + (expiresInSeconds - 200) * 1000L;
}
@Deprecated
public void setSuiteTicket(String suiteTicket) {
this.suiteTicket = suiteTicket;
}
/**
* Gets suite ticket expires time.
*
* @return the suite ticket expires time
*/
@Deprecated
public long getSuiteTicketExpiresTime() {
return this.suiteTicketExpiresTime;
}
/**
* Sets suite ticket expires time.
*
* @param suiteTicketExpiresTime the suite ticket expires time
*/
@Deprecated
public void setSuiteTicketExpiresTime(long suiteTicketExpiresTime) {
this.suiteTicketExpiresTime = suiteTicketExpiresTime;
@ -171,6 +183,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.suiteId;
}
/**
* Sets suite id.
*
* @param corpId the corp id
*/
@Deprecated
public void setSuiteId(String corpId) {
this.suiteId = corpId;
@ -181,6 +198,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.suiteSecret;
}
/**
* Sets suite secret.
*
* @param corpSecret the corp secret
*/
@Deprecated
public void setSuiteSecret(String corpSecret) {
this.suiteSecret = corpSecret;
@ -191,6 +213,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.token;
}
/**
* Sets token.
*
* @param token the token
*/
@Deprecated
public void setToken(String token) {
this.token = token;
@ -201,6 +228,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.aesKey;
}
/**
* Sets aes key.
*
* @param aesKey the aes key
*/
@Deprecated
public void setAesKey(String aesKey) {
this.aesKey = aesKey;
@ -212,6 +244,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.corpId;
}
/**
* Sets corp id.
*
* @param corpId the corp id
*/
@Deprecated
public void setCorpId(String corpId) {
this.corpId = corpId;
@ -222,16 +259,20 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.corpSecret;
}
@Override
public String getProviderSecret() {
return providerSecret;
}
/**
* Sets corp secret.
*
* @param corpSecret the corp secret
*/
@Deprecated
public void setCorpSecret(String corpSecret) {
this.corpSecret = corpSecret;
}
@Override
public String getProviderSecret() {
return providerSecret;
}
@Override
public String getAccessToken(String authCorpId) {
@ -244,7 +285,7 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
Long expire = authCorpAccessTokenExpireTimeMap.getOrDefault(authCorpId, 0L);
WxAccessToken accessTokenEntity = new WxAccessToken();
accessTokenEntity.setAccessToken(accessToken);
accessTokenEntity.setExpiresIn((int)((expire - System.currentTimeMillis()) / 1000 + 200));
accessTokenEntity.setExpiresIn((int) ((expire - System.currentTimeMillis()) / 1000 + 200));
return accessTokenEntity;
}
@ -252,16 +293,16 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
public boolean isAccessTokenExpired(String authCorpId) {
//不存在或者过期
return authCorpAccessTokenExpireTimeMap.get(authCorpId) == null
|| System.currentTimeMillis() > authCorpAccessTokenExpireTimeMap.get(authCorpId);
|| System.currentTimeMillis() > authCorpAccessTokenExpireTimeMap.get(authCorpId);
}
@Override
public void expireAccessToken(String authCorpId) {
@Override
public void expireAccessToken(String authCorpId) {
authCorpAccessTokenMap.remove(authCorpId);
authCorpAccessTokenExpireTimeMap.remove(authCorpId);
}
}
@Override
@Override
public void updateAccessToken(String authCorpId, String accessToken, int expiredInSeconds) {
authCorpAccessTokenMap.put(authCorpId, accessToken);
// 预留200秒的时间
@ -353,6 +394,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
this.providerTokenExpiresTime = 0L;
}
/**
* Sets oauth 2 redirect uri.
*
* @param oauth2redirectUri the oauth 2 redirect uri
*/
public void setOauth2redirectUri(String oauth2redirectUri) {
this.oauth2redirectUri = oauth2redirectUri;
}
@ -362,6 +408,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.httpProxyHost;
}
/**
* Sets http proxy host.
*
* @param httpProxyHost the http proxy host
*/
public void setHttpProxyHost(String httpProxyHost) {
this.httpProxyHost = httpProxyHost;
}
@ -371,6 +422,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.httpProxyPort;
}
/**
* Sets http proxy port.
*
* @param httpProxyPort the http proxy port
*/
public void setHttpProxyPort(int httpProxyPort) {
this.httpProxyPort = httpProxyPort;
}
@ -380,6 +436,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.httpProxyUsername;
}
/**
* Sets http proxy username.
*
* @param httpProxyUsername the http proxy username
*/
public void setHttpProxyUsername(String httpProxyUsername) {
this.httpProxyUsername = httpProxyUsername;
}
@ -389,6 +450,11 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.httpProxyPassword;
}
/**
* Sets http proxy password.
*
* @param httpProxyPassword the http proxy password
*/
public void setHttpProxyPassword(String httpProxyPassword) {
this.httpProxyPassword = httpProxyPassword;
}
@ -403,6 +469,15 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
return this.tmpDirFile;
}
/**
* Sets tmp dir file.
*
* @param tmpDirFile the tmp dir file
*/
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
@Override
public Lock getProviderAccessTokenLock() {
return this.providerAccessTokenLocker
@ -432,21 +507,22 @@ public class WxCpTpDefaultConfigImpl implements WxCpTpConfigStorage, Serializabl
.computeIfAbsent(String.join(":", this.suiteId, authCorpId), key -> new ReentrantLock());
}
public void setTmpDirFile(File tmpDirFile) {
this.tmpDirFile = tmpDirFile;
}
@Override
public ApacheHttpClientBuilder getApacheHttpClientBuilder() {
return this.apacheHttpClientBuilder;
}
/**
* Sets apache http client builder.
*
* @param apacheHttpClientBuilder the apache http client builder
*/
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
@Override
public boolean autoRefreshToken() {
return true;
}
public void setApacheHttpClientBuilder(ApacheHttpClientBuilder apacheHttpClientBuilder) {
this.apacheHttpClientBuilder = apacheHttpClientBuilder;
}
}

View File

@ -3,6 +3,7 @@ package me.chanjar.weixin.cp.config.impl;
import lombok.Builder;
import lombok.NonNull;
import lombok.Setter;
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.redis.WxRedisOps;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
@ -21,26 +22,46 @@ import java.util.concurrent.locks.Lock;
*/
@Builder
public class WxCpTpRedissonConfigImpl implements WxCpTpConfigStorage, Serializable {
private static final long serialVersionUID = -5385639031981770319L;
/**
* The constant LOCK_KEY.
*/
// lock key
protected static final String LOCK_KEY = "wechat_tp_lock:";
/**
* The constant LOCKER_PROVIDER_ACCESS_TOKEN.
*/
protected static final String LOCKER_PROVIDER_ACCESS_TOKEN = "providerAccessTokenLock";
/**
* The constant LOCKER_SUITE_ACCESS_TOKEN.
*/
protected static final String LOCKER_SUITE_ACCESS_TOKEN = "suiteAccessTokenLock";
/**
* The constant LOCKER_ACCESS_TOKEN.
*/
protected static final String LOCKER_ACCESS_TOKEN = "accessTokenLock";
/**
* The constant LOCKER_CORP_JSAPI_TICKET.
*/
protected static final String LOCKER_CORP_JSAPI_TICKET = "corpJsapiTicketLock";
/**
* The constant LOCKER_SUITE_JSAPI_TICKET.
*/
protected static final String LOCKER_SUITE_JSAPI_TICKET = "suiteJsapiTicketLock";
@NonNull
private final WxRedisOps wxRedisOps;
//redis里面key的统一前缀
//private final String keyPrefix = "";//4.0.9.B 有final为不可设置,去掉final改为可设置
private String keyPrefix = "";
private final String suiteAccessTokenKey = ":suiteAccessTokenKey:";
private final String suiteTicketKey = ":suiteTicketKey:";
private final String accessTokenKey = ":accessTokenKey:";
private final String authCorpJsApiTicketKey = ":authCorpJsApiTicketKey:";
private final String authSuiteJsApiTicketKey = ":authSuiteJsApiTicketKey:";
private final String providerTokenKey = ":providerTokenKey:";
/**
* redis里面key的统一前缀
*/
@Setter
private String keyPrefix = "";
private volatile String baseApiUrl;
private volatile String httpProxyHost;
private volatile int httpProxyPort;
@ -48,36 +69,29 @@ public class WxCpTpRedissonConfigImpl implements WxCpTpConfigStorage, Serializab
private volatile String httpProxyPassword;
private volatile ApacheHttpClientBuilder apacheHttpClientBuilder;
private volatile File tmpDirFile;
/**
* 第三方应用的其他配置来自于企微配置
*/
private volatile String suiteId;
private volatile String suiteSecret;
// 第三方应用的token用来检查应用的签名
/**
* 第三方应用的token用来检查应用的签名
*/
private volatile String token;
//第三方应用的EncodingAESKey用来检查签名
/**
* 第三方应用的EncodingAESKey用来检查签名
*/
private volatile String aesKey;
/**
* 企微服务商企业ID & 企业secret来自于企微配置
*/
private volatile String corpId;
private volatile String corpSecret;
/**
* 服务商secret
*/
private volatile String providerSecret;
// lock key
protected static final String LOCK_KEY = "wechat_tp_lock:";
protected static final String LOCKER_PROVIDER_ACCESS_TOKEN = "providerAccessTokenLock";
protected static final String LOCKER_SUITE_ACCESS_TOKEN = "suiteAccessTokenLock";
protected static final String LOCKER_ACCESS_TOKEN = "accessTokenLock";
protected static final String LOCKER_CORP_JSAPI_TICKET = "corpJsapiTicketLock";
protected static final String LOCKER_SUITE_JSAPI_TICKET = "suiteJsapiTicketLock";
@Override
public void setBaseApiUrl(String baseUrl) {
this.baseApiUrl = baseUrl;
@ -221,7 +235,7 @@ public class WxCpTpRedissonConfigImpl implements WxCpTpConfigStorage, Serializab
WxAccessToken accessTokenEntity = new WxAccessToken();
accessTokenEntity.setAccessToken(accessToken);
accessTokenEntity.setExpiresIn((int)((expire - System.currentTimeMillis()) / 1000 + 200));
accessTokenEntity.setExpiresIn((int) ((expire - System.currentTimeMillis()) / 1000 + 200));
return accessTokenEntity;
}
@ -393,8 +407,6 @@ public class WxCpTpRedissonConfigImpl implements WxCpTpConfigStorage, Serializab
/**
* 单独处理provider,且不应和suite 有关系
* @param key
* @return
*/
private Lock getProviderLockByKey(String key) {
return this.wxRedisOps.getLock(String.join(":", providerKeyWithPrefix(LOCK_KEY), key));
@ -412,26 +424,23 @@ public class WxCpTpRedissonConfigImpl implements WxCpTpConfigStorage, Serializab
@Override
public String toString() {
//TODO:
return WxCpGsonBuilder.create().toJson(this);
}
/**
* 一个provider 会有多个suite,需要唯一标识作为前缀
* @param key
* @return
*
*/
private String keyWithPrefix(String key) {
return keyPrefix +":"+suiteId+":" + key;
return keyPrefix + ":" + suiteId + ":" + key;
}
/**
* provider 应该独享一个key,且不和任何suite关联
* 一个provider 会有多个suite,不同的suite 都应该指向同一个provider 的数据
* @param key
* @return
*
*/
private String providerKeyWithPrefix(String key) {
return keyPrefix +":"+corpId+":" + key;
return keyPrefix + ":" + corpId + ":" + key;
}
}