Token优化
This commit is contained in:
parent
8902d8aef7
commit
fa6b60af82
@ -0,0 +1,36 @@
|
|||||||
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
|
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @className: AbstractTokenCreator
|
||||||
|
* @author jinyu
|
||||||
|
* @date 2016年4月21日
|
||||||
|
* @since JDK 1.6
|
||||||
|
* @see
|
||||||
|
*/
|
||||||
|
public abstract class AbstractTokenCreator implements TokenCreator {
|
||||||
|
|
||||||
|
protected final WeixinRequestExecutor weixinExecutor;
|
||||||
|
|
||||||
|
public AbstractTokenCreator() {
|
||||||
|
this.weixinExecutor = new WeixinRequestExecutor();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 缓存key:附加weixin4j前缀
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getCacheKey() {
|
||||||
|
return String.format("weixin4j_%s", getCacheKey0());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回缓存KEY的名称:建议接口类型命名 如 mp_token_{appid}
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public abstract String getCacheKey0();
|
||||||
|
}
|
||||||
@ -29,17 +29,14 @@ public class FileTokenStorager implements TokenStorager {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token lookup(String cacheKey) throws WeixinException {
|
public Token lookup(String cacheKey) throws WeixinException {
|
||||||
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
File token_file = new File(String.format("%s/%s.xml", cachePath, cacheKey));
|
||||||
cacheKey));
|
|
||||||
try {
|
try {
|
||||||
if (token_file.exists()) {
|
if (token_file.exists()) {
|
||||||
Token token = XmlStream.fromXML(
|
Token token = XmlStream.fromXML(new FileInputStream(token_file), Token.class);
|
||||||
new FileInputStream(token_file), Token.class);
|
|
||||||
if (token.getCreateTime() < 0) {
|
if (token.getCreateTime() < 0) {
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System
|
if ((token.getCreateTime() + (token.getExpiresIn() * 1000l) - CUTMS) > System.currentTimeMillis()) {
|
||||||
.currentTimeMillis()) {
|
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -52,10 +49,7 @@ public class FileTokenStorager implements TokenStorager {
|
|||||||
@Override
|
@Override
|
||||||
public void caching(String cacheKey, Token token) throws WeixinException {
|
public void caching(String cacheKey, Token token) throws WeixinException {
|
||||||
try {
|
try {
|
||||||
XmlStream.toXML(
|
XmlStream.toXML(token, new FileOutputStream(new File(String.format("%s/%s.xml", cachePath, cacheKey))));
|
||||||
token,
|
|
||||||
new FileOutputStream(new File(String.format("%s/%s.xml",
|
|
||||||
cachePath, cacheKey))));
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new WeixinException(e);
|
throw new WeixinException(e);
|
||||||
}
|
}
|
||||||
@ -64,12 +58,10 @@ public class FileTokenStorager implements TokenStorager {
|
|||||||
@Override
|
@Override
|
||||||
public Token evict(String cacheKey) throws WeixinException {
|
public Token evict(String cacheKey) throws WeixinException {
|
||||||
Token token = null;
|
Token token = null;
|
||||||
File token_file = new File(String.format("%s/%s.xml", cachePath,
|
File token_file = new File(String.format("%s/%s.xml", cachePath, cacheKey));
|
||||||
cacheKey));
|
|
||||||
try {
|
try {
|
||||||
if (token_file.exists()) {
|
if (token_file.exists()) {
|
||||||
token = XmlStream.fromXML(new FileInputStream(token_file),
|
token = XmlStream.fromXML(new FileInputStream(token_file), Token.class);
|
||||||
Token.class);
|
|
||||||
token_file.delete();
|
token_file.delete();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -83,9 +75,8 @@ public class FileTokenStorager implements TokenStorager {
|
|||||||
File[] files = new File(cachePath).listFiles(new FileFilter() {
|
File[] files = new File(cachePath).listFiles(new FileFilter() {
|
||||||
@Override
|
@Override
|
||||||
public boolean accept(File file) {
|
public boolean accept(File file) {
|
||||||
return file.isFile()
|
return file.isFile() && file.getName().startsWith(PREFIX)
|
||||||
&& "xml".equals(FileUtil.getFileExtension(file
|
&& "xml".equals(FileUtil.getFileExtension(file.getName()));
|
||||||
.getName()));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
for (File token : files) {
|
for (File token : files) {
|
||||||
|
|||||||
@ -45,8 +45,7 @@ public class RedisTokenStorager implements TokenStorager {
|
|||||||
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
this.jedisPool = new JedisPool(jedisPoolConfig, host, port);
|
||||||
}
|
}
|
||||||
|
|
||||||
public RedisTokenStorager(String host, int port,
|
public RedisTokenStorager(String host, int port, JedisPoolConfig jedisPoolConfig) {
|
||||||
JedisPoolConfig jedisPoolConfig) {
|
|
||||||
this(new JedisPool(jedisPoolConfig, host, port));
|
this(new JedisPool(jedisPoolConfig, host, port));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,8 +77,7 @@ public class RedisTokenStorager implements TokenStorager {
|
|||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
jedis.hmset(cacheKey, token2map(token));
|
jedis.hmset(cacheKey, token2map(token));
|
||||||
if (token.getExpiresIn() > 0) {
|
if (token.getExpiresIn() > 0) {
|
||||||
jedis.expire(cacheKey, token.getExpiresIn()
|
jedis.expire(cacheKey, token.getExpiresIn() - (int) (CUTMS / 1000l));
|
||||||
- (int) (CUTMS / 1000l));
|
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
if (jedis != null) {
|
if (jedis != null) {
|
||||||
@ -130,7 +128,7 @@ public class RedisTokenStorager implements TokenStorager {
|
|||||||
Jedis jedis = null;
|
Jedis jedis = null;
|
||||||
try {
|
try {
|
||||||
jedis = jedisPool.getResource();
|
jedis = jedisPool.getResource();
|
||||||
Set<String> cacheKeys = jedis.keys("weixin4j_*");
|
Set<String> cacheKeys = jedis.keys(String.format("%s*", PREFIX));
|
||||||
if (!cacheKeys.isEmpty()) {
|
if (!cacheKeys.isEmpty()) {
|
||||||
Pipeline pipeline = jedis.pipelined();
|
Pipeline pipeline = jedis.pipelined();
|
||||||
for (String cacheKey : cacheKeys) {
|
for (String cacheKey : cacheKeys) {
|
||||||
|
|||||||
@ -1,30 +1,30 @@
|
|||||||
package com.foxinmy.weixin4j.token;
|
package com.foxinmy.weixin4j.token;
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TOKEN创建者
|
* TOKEN创建者
|
||||||
*
|
*
|
||||||
* @className TokenCreator
|
* @className TokenCreator
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年1月10日
|
* @date 2015年1月10日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see
|
* @see
|
||||||
*/
|
*/
|
||||||
public interface TokenCreator {
|
public interface TokenCreator {
|
||||||
/**
|
/**
|
||||||
* 返回缓存KEY的名称
|
* 返回缓存KEY的名称
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public String getCacheKey();
|
public String getCacheKey();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建token
|
* 创建token
|
||||||
*
|
*
|
||||||
* @return
|
* @return
|
||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public Token createToken() throws WeixinException;
|
public Token createToken() throws WeixinException;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,4 +19,8 @@ public interface TokenStorager extends CacheStorager<Token> {
|
|||||||
* 考虑到程序的临界值,实际有效时间应该减去下面这个数
|
* 考虑到程序的临界值,实际有效时间应该减去下面这个数
|
||||||
*/
|
*/
|
||||||
final long CUTMS = 1 * 60 * 1000l;
|
final long CUTMS = 1 * 60 * 1000l;
|
||||||
|
/**
|
||||||
|
* 缓存key的前缀
|
||||||
|
*/
|
||||||
|
final String PREFIX = "weixin4j_";
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,6 @@
|
|||||||
package com.foximy.weixin4j.example.server;
|
package com.foximy.weixin4j.example.server;
|
||||||
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
|
||||||
|
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
|
|
||||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -42,11 +38,11 @@ public class Weixin4jServerStartupWithoutThread {
|
|||||||
* @throws WeixinException
|
* @throws WeixinException
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) throws WeixinException {
|
public static void main(String[] args) throws WeixinException {
|
||||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
//ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||||
new String[] { "classpath:/spring-bean.xml" });
|
// new String[] { "classpath:/spring-bean.xml" });
|
||||||
new WeixinServerBootstrap(aesToken)
|
new WeixinServerBootstrap(aesToken)
|
||||||
.handlerPackagesToScan(handlerPackage).openAlwaysResponse()
|
.handlerPackagesToScan(handlerPackage).openAlwaysResponse()
|
||||||
.resolveBeanFactory(new SpringBeanFactory(applicationContext))
|
//.resolveBeanFactory(new SpringBeanFactory(applicationContext))
|
||||||
.startup(port);
|
.startup(port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,63 +1,60 @@
|
|||||||
package com.foxinmy.weixin4j.mp.token;
|
package com.foxinmy.weixin4j.mp.token;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.type.TicketType;
|
||||||
import com.foxinmy.weixin4j.type.TicketType;
|
|
||||||
|
/**
|
||||||
/**
|
* 微信公众平台TICKET创建(包括jsticket、其它JSSDK所需的ticket的创建
|
||||||
* 微信公众平台TICKET创建(包括jsticket、其它JSSDK所需的ticket的创建
|
*
|
||||||
*
|
* @className WeixinJSTicketCreator
|
||||||
* @className WeixinJSTicketCreator
|
* @author jy
|
||||||
* @author jy
|
* @date 2015年1月10日
|
||||||
* @date 2015年1月10日
|
* @since JDK 1.6
|
||||||
* @since JDK 1.6
|
* @see <a href=
|
||||||
* @see <a href=
|
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">
|
||||||
* "https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN">
|
* JS TICKET</a>
|
||||||
* JS TICKET</a>
|
*/
|
||||||
*/
|
public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||||
public class WeixinTicketCreator implements TokenCreator {
|
|
||||||
|
private final String appid;
|
||||||
private final String appid;
|
private final TicketType ticketType;
|
||||||
private final TicketType ticketType;
|
private final TokenHolder weixinTokenHolder;
|
||||||
private final TokenHolder weixinTokenHolder;
|
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
/**
|
||||||
|
* jssdk
|
||||||
/**
|
*
|
||||||
* jssdk
|
* @param appid
|
||||||
*
|
* 公众号的appid
|
||||||
* @param appid
|
* @param ticketType
|
||||||
* 公众号的appid
|
* 票据类型
|
||||||
* @param ticketType
|
* @param weixinTokenHolder
|
||||||
* 票据类型
|
* <font color="red">公众平台的access_token</font>
|
||||||
* @param weixinTokenHolder
|
*/
|
||||||
* <font color="red">公众平台的access_token</font>
|
public WeixinTicketCreator(String appid, TicketType ticketType, TokenHolder weixinTokenHolder) {
|
||||||
*/
|
this.appid = appid;
|
||||||
public WeixinTicketCreator(String appid, TicketType ticketType, TokenHolder weixinTokenHolder) {
|
this.ticketType = ticketType;
|
||||||
this.appid = appid;
|
this.weixinTokenHolder = weixinTokenHolder;
|
||||||
this.ticketType = ticketType;
|
}
|
||||||
this.weixinTokenHolder = weixinTokenHolder;
|
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
@Override
|
||||||
}
|
public String getCacheKey0() {
|
||||||
|
return String.format("mp_ticket_%s_%s", ticketType.name(), appid);
|
||||||
@Override
|
}
|
||||||
public String getCacheKey() {
|
|
||||||
return String.format("weixin4j_mp_ticket_%s_%s", ticketType.name(), appid);
|
@Override
|
||||||
}
|
public Token createToken() throws WeixinException {
|
||||||
|
WeixinResponse response = weixinExecutor.get(
|
||||||
@Override
|
String.format(URLConsts.TICKET_URL, weixinTokenHolder.getToken().getAccessToken(), ticketType.name()));
|
||||||
public Token createToken() throws WeixinException {
|
JSONObject result = response.getAsJson();
|
||||||
WeixinResponse response = weixinExecutor.get(
|
Token token = new Token(result.getString("ticket"));
|
||||||
String.format(URLConsts.TICKET_URL, weixinTokenHolder.getToken().getAccessToken(), ticketType.name()));
|
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||||
JSONObject result = response.getAsJson();
|
token.setOriginalResult(response.getAsString());
|
||||||
Token token = new Token(result.getString("ticket"));
|
return token;
|
||||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
}
|
||||||
token.setOriginalResult(response.getAsString());
|
}
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,56 +1,56 @@
|
|||||||
package com.foxinmy.weixin4j.mp.token;
|
package com.foxinmy.weixin4j.mp.token;
|
||||||
|
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
import com.foxinmy.weixin4j.mp.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信公众平台TOKEN创建者
|
* 微信公众平台TOKEN创建者
|
||||||
*
|
*
|
||||||
* @className WeixinTokenCreator
|
* @className WeixinTokenCreator
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年1月10日
|
* @date 2015年1月10日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see <a
|
* @see <a
|
||||||
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
* href="https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140183&token=&lang=zh_CN">微信公众平台获取token说明</a>
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
*/
|
*/
|
||||||
public class WeixinTokenCreator implements TokenCreator {
|
public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||||
|
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
private final WeixinRequestExecutor weixinExecutor;
|
||||||
private final String appid;
|
private final String appid;
|
||||||
private final String secret;
|
private final String secret;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param appid
|
* @param appid
|
||||||
* 公众号ID
|
* 公众号ID
|
||||||
* @param secret
|
* @param secret
|
||||||
* 公众号secret
|
* 公众号secret
|
||||||
*/
|
*/
|
||||||
public WeixinTokenCreator(String appid, String secret) {
|
public WeixinTokenCreator(String appid, String secret) {
|
||||||
this.appid = appid;
|
this.appid = appid;
|
||||||
this.secret = secret;
|
this.secret = secret;
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
this.weixinExecutor = new WeixinRequestExecutor();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey0() {
|
||||||
return String.format("weixin4j_mp_token_%s", appid);
|
return String.format("mp_token_%s", appid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Token createToken() throws WeixinException {
|
public Token createToken() throws WeixinException {
|
||||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
|
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, appid,
|
||||||
secret);
|
secret);
|
||||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||||
});
|
});
|
||||||
token.setOriginalResult(response.getAsString());
|
token.setOriginalResult(response.getAsString());
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,60 +1,56 @@
|
|||||||
package com.foxinmy.weixin4j.qy.suite;
|
package com.foxinmy.weixin4j.qy.suite;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
|
||||||
|
/**
|
||||||
/**
|
* 微信企业号应用套件预授权码创建
|
||||||
* 微信企业号应用套件预授权码创建
|
*
|
||||||
*
|
* @className WeixinSuitePreCodeCreator
|
||||||
* @className WeixinSuitePreCodeCreator
|
* @author jy
|
||||||
* @author jy
|
* @date 2015年6月17日
|
||||||
* @date 2015年6月17日
|
* @since JDK 1.6
|
||||||
* @since JDK 1.6
|
* @see <a href=
|
||||||
* @see <a
|
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E9.A2.84.E6.8E.88.E6.9D.83.E7.A0.81">
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E9.A2.84.E6.8E.88.E6.9D.83.E7.A0.81">获取应用套件预授权码</a>
|
* 获取应用套件预授权码</a>
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
*/
|
*/
|
||||||
public class WeixinSuitePreCodeCreator implements TokenCreator {
|
public class WeixinSuitePreCodeCreator extends AbstractTokenCreator {
|
||||||
|
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
private final TokenHolder suiteTokenHolder;
|
||||||
private final TokenHolder suiteTokenHolder;
|
private final String suiteId;
|
||||||
private final String suiteId;
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param suiteTokenHolder
|
||||||
* @param suiteTokenHolder
|
* 应用套件的token
|
||||||
* 应用套件的token
|
* @param suiteId
|
||||||
* @param suiteId
|
* 应用套件ID
|
||||||
* 应用套件ID
|
*/
|
||||||
*/
|
public WeixinSuitePreCodeCreator(TokenHolder suiteTokenHolder, String suiteId) {
|
||||||
public WeixinSuitePreCodeCreator(TokenHolder suiteTokenHolder,
|
this.suiteTokenHolder = suiteTokenHolder;
|
||||||
String suiteId) {
|
this.suiteId = suiteId;
|
||||||
this.suiteTokenHolder = suiteTokenHolder;
|
}
|
||||||
this.suiteId = suiteId;
|
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
@Override
|
||||||
}
|
public String getCacheKey0() {
|
||||||
|
return String.format("qy_suite_precode_%s", suiteId);
|
||||||
@Override
|
}
|
||||||
public String getCacheKey() {
|
|
||||||
return String.format("weixin4j_qy_suite_precode_%s", suiteId);
|
@Override
|
||||||
}
|
public Token createToken() throws WeixinException {
|
||||||
|
WeixinResponse response = weixinExecutor.post(
|
||||||
@Override
|
String.format(URLConsts.SUITE_PRE_CODE_URL, suiteTokenHolder.getAccessToken()),
|
||||||
public Token createToken() throws WeixinException {
|
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
||||||
WeixinResponse response = weixinExecutor.post(
|
JSONObject result = response.getAsJson();
|
||||||
String.format(URLConsts.SUITE_PRE_CODE_URL,
|
Token token = new Token(result.getString("pre_auth_code"));
|
||||||
suiteTokenHolder.getAccessToken()),
|
token.setExpiresIn(result.getIntValue("expires_in"));
|
||||||
String.format("{\"suite_id\":\"%s\"}", suiteId));
|
token.setOriginalResult(response.getAsString());
|
||||||
JSONObject result = response.getAsJson();
|
return token;
|
||||||
Token token = new Token(result.getString("pre_auth_code"));
|
}
|
||||||
token.setExpiresIn(result.getIntValue("expires_in"));
|
}
|
||||||
token.setOriginalResult(response.getAsString());
|
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -1,56 +1,53 @@
|
|||||||
package com.foxinmy.weixin4j.qy.suite;
|
package com.foxinmy.weixin4j.qy.suite;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
|
||||||
|
/**
|
||||||
/**
|
* 微信企业号应用套件凭证创建
|
||||||
* 微信企业号应用套件凭证创建
|
*
|
||||||
*
|
* @className WeixinSuiteTokenCreator
|
||||||
* @className WeixinSuiteTokenCreator
|
* @author jy
|
||||||
* @author jy
|
* @date 2015年6月17日
|
||||||
* @date 2015年6月17日
|
* @since JDK 1.6
|
||||||
* @since JDK 1.6
|
* @see <a
|
||||||
* @see <a
|
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E5.BA.94.E7.94.A8.E5.A5.97.E4.BB.B6.E4.BB.A4.E7.89.8C">获取应用套件凭证</a>
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
*/
|
||||||
*/
|
public class WeixinSuiteTokenCreator extends AbstractTokenCreator {
|
||||||
public class WeixinSuiteTokenCreator implements TokenCreator {
|
|
||||||
|
private final SuiteTicketHolder ticketHolder;
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
|
||||||
private final SuiteTicketHolder ticketHolder;
|
/**
|
||||||
|
*
|
||||||
/**
|
* @param stringStorager
|
||||||
*
|
* 套件ticket存取器
|
||||||
* @param stringStorager
|
*/
|
||||||
* 套件ticket存取器
|
public WeixinSuiteTokenCreator(SuiteTicketHolder ticketHolder) {
|
||||||
*/
|
this.ticketHolder = ticketHolder;
|
||||||
public WeixinSuiteTokenCreator(SuiteTicketHolder ticketHolder) {
|
}
|
||||||
this.ticketHolder = ticketHolder;
|
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
@Override
|
||||||
}
|
public String getCacheKey0() {
|
||||||
|
return String.format("qy_suite_token_%s", ticketHolder.getSuiteId());
|
||||||
@Override
|
}
|
||||||
public String getCacheKey() {
|
|
||||||
return String.format("weixin4j_qy_suite_token_%s", ticketHolder.getSuiteId());
|
@Override
|
||||||
}
|
public Token createToken() throws WeixinException {
|
||||||
|
JSONObject obj = new JSONObject();
|
||||||
@Override
|
obj.put("suite_id", ticketHolder.getSuiteId());
|
||||||
public Token createToken() throws WeixinException {
|
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
||||||
JSONObject obj = new JSONObject();
|
obj.put("suite_ticket", ticketHolder.getTicket());
|
||||||
obj.put("suite_id", ticketHolder.getSuiteId());
|
WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL,
|
||||||
obj.put("suite_secret", ticketHolder.getSuiteSecret());
|
obj.toJSONString());
|
||||||
obj.put("suite_ticket", ticketHolder.getTicket());
|
obj = response.getAsJson();
|
||||||
WeixinResponse response = weixinExecutor.post(URLConsts.SUITE_TOKEN_URL,
|
Token token = new Token(obj.getString("suite_access_token"));
|
||||||
obj.toJSONString());
|
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||||
obj = response.getAsJson();
|
token.setOriginalResult(response.getAsString());
|
||||||
Token token = new Token(obj.getString("suite_access_token"));
|
return token;
|
||||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
}
|
||||||
token.setOriginalResult(response.getAsString());
|
}
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -2,11 +2,10 @@ package com.foxinmy.weixin4j.qy.suite;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -16,13 +15,13 @@ import com.foxinmy.weixin4j.token.TokenHolder;
|
|||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年6月17日
|
* @date 2015年6月17日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7access_token">获取企业号access_token</a>
|
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E7%AC%AC%E4%B8%89%E6%96%B9%E5%BA%94%E7%94%A8%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E#.E8.8E.B7.E5.8F.96.E4.BC.81.E4.B8.9A.E5.8F.B7access_token">
|
||||||
|
* 获取企业号access_token</a>
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
*/
|
*/
|
||||||
public class WeixinTokenSuiteCreator implements TokenCreator {
|
public class WeixinTokenSuiteCreator extends AbstractTokenCreator {
|
||||||
|
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
|
||||||
private final SuitePerCodeHolder perCodeHolder;
|
private final SuitePerCodeHolder perCodeHolder;
|
||||||
private final TokenHolder suiteTokenHolder;
|
private final TokenHolder suiteTokenHolder;
|
||||||
|
|
||||||
@ -33,19 +32,14 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
|||||||
* @param suiteTokenHolder
|
* @param suiteTokenHolder
|
||||||
* 第三方套件凭证token
|
* 第三方套件凭证token
|
||||||
*/
|
*/
|
||||||
public WeixinTokenSuiteCreator(SuitePerCodeHolder perCodeHolder,
|
public WeixinTokenSuiteCreator(SuitePerCodeHolder perCodeHolder, TokenHolder suiteTokenHolder) {
|
||||||
TokenHolder suiteTokenHolder) {
|
|
||||||
this.perCodeHolder = perCodeHolder;
|
this.perCodeHolder = perCodeHolder;
|
||||||
this.suiteTokenHolder = suiteTokenHolder;
|
this.suiteTokenHolder = suiteTokenHolder;
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey0() {
|
||||||
return String.format("weixin4j_qy_token_suite_%s_%s",
|
return String.format("qy_token_suite_%s_%s", perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId());
|
||||||
perCodeHolder.getSuiteId(), perCodeHolder.getAuthCorpId()
|
|
||||||
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -54,9 +48,8 @@ public class WeixinTokenSuiteCreator implements TokenCreator {
|
|||||||
obj.put("suite_id", perCodeHolder.getSuiteId());
|
obj.put("suite_id", perCodeHolder.getSuiteId());
|
||||||
obj.put("auth_corpid", perCodeHolder.getAuthCorpId());
|
obj.put("auth_corpid", perCodeHolder.getAuthCorpId());
|
||||||
obj.put("permanent_code", perCodeHolder.getPermanentCode());
|
obj.put("permanent_code", perCodeHolder.getPermanentCode());
|
||||||
WeixinResponse response = weixinExecutor.post(
|
WeixinResponse response = weixinExecutor
|
||||||
String.format(URLConsts.TOKEN_SUITE_URL,
|
.post(String.format(URLConsts.TOKEN_SUITE_URL, suiteTokenHolder.getAccessToken()), obj.toJSONString());
|
||||||
suiteTokenHolder.getAccessToken()), obj.toJSONString());
|
|
||||||
obj = response.getAsJson();
|
obj = response.getAsJson();
|
||||||
Token token = new Token(obj.getString("access_token"));
|
Token token = new Token(obj.getString("access_token"));
|
||||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||||
|
|||||||
@ -1,59 +1,56 @@
|
|||||||
package com.foxinmy.weixin4j.qy.token;
|
package com.foxinmy.weixin4j.qy.token;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
|
||||||
|
/**
|
||||||
/**
|
* 微信企业号应用提供商凭证创建
|
||||||
* 微信企业号应用提供商凭证创建
|
*
|
||||||
*
|
* @className WeixinTokenCreator
|
||||||
* @className WeixinTokenCreator
|
* @author jy
|
||||||
* @author jy
|
* @date 2015年1月10日
|
||||||
* @date 2015年1月10日
|
* @since JDK 1.6
|
||||||
* @since JDK 1.6
|
* @see <a href=
|
||||||
* @see <a
|
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E6%8F%90%E4%BE%9B%E5%95%86%E5%87%AD%E8%AF%81">
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E5%BA%94%E7%94%A8%E6%8F%90%E4%BE%9B%E5%95%86%E5%87%AD%E8%AF%81">获取应用提供商凭证</a>
|
* 获取应用提供商凭证</a>
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
*/
|
*/
|
||||||
public class WeixinProviderTokenCreator implements TokenCreator {
|
public class WeixinProviderTokenCreator extends AbstractTokenCreator {
|
||||||
|
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
private final String corpid;
|
||||||
private final String corpid;
|
private final String providersecret;
|
||||||
private final String providersecret;
|
|
||||||
|
/**
|
||||||
/**
|
*
|
||||||
*
|
* @param corpid
|
||||||
* @param corpid
|
* 企业号ID
|
||||||
* 企业号ID
|
* @param providersecret
|
||||||
* @param providersecret
|
* 企业号提供商的secret
|
||||||
* 企业号提供商的secret
|
*/
|
||||||
*/
|
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
||||||
public WeixinProviderTokenCreator(String corpid, String providersecret) {
|
this.corpid = corpid;
|
||||||
this.corpid = corpid;
|
this.providersecret = providersecret;
|
||||||
this.providersecret = providersecret;
|
}
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
|
||||||
}
|
@Override
|
||||||
|
public String getCacheKey0() {
|
||||||
@Override
|
return String.format("qy_provider_token_%s", corpid);
|
||||||
public String getCacheKey() {
|
}
|
||||||
return String.format("weixin4j_qy_provider_token_%s", corpid);
|
|
||||||
}
|
@Override
|
||||||
|
public Token createToken() throws WeixinException {
|
||||||
@Override
|
JSONObject obj = new JSONObject();
|
||||||
public Token createToken() throws WeixinException {
|
obj.put("corpid", corpid);
|
||||||
JSONObject obj = new JSONObject();
|
obj.put("provider_secret", providersecret);
|
||||||
obj.put("corpid", corpid);
|
WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL, obj.toJSONString());
|
||||||
obj.put("provider_secret", providersecret);
|
obj = response.getAsJson();
|
||||||
WeixinResponse response = weixinExecutor.post(URLConsts.PROVIDER_TOKEN_URL,
|
Token token = new Token(obj.getString("provider_access_token"));
|
||||||
obj.toJSONString());
|
token.setExpiresIn(obj.getIntValue("expires_in"));
|
||||||
obj = response.getAsJson();
|
token.setOriginalResult(response.getAsString());
|
||||||
Token token = new Token(obj.getString("provider_access_token"));
|
return token;
|
||||||
token.setExpiresIn(obj.getIntValue("expires_in"));
|
}
|
||||||
token.setOriginalResult(response.getAsString());
|
}
|
||||||
return token;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@ -2,11 +2,10 @@ package com.foxinmy.weixin4j.qy.token;
|
|||||||
|
|
||||||
import com.alibaba.fastjson.JSONObject;
|
import com.alibaba.fastjson.JSONObject;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||||
import com.foxinmy.weixin4j.type.TicketType;
|
import com.foxinmy.weixin4j.type.TicketType;
|
||||||
|
|
||||||
@ -20,12 +19,11 @@ import com.foxinmy.weixin4j.type.TicketType;
|
|||||||
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95"
|
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E9.99.84.E5.BD.951-JS-SDK.E4.BD.BF.E7.94.A8.E6.9D.83.E9.99.90.E7.AD.BE.E5.90.8D.E7.AE.97.E6.B3.95"
|
||||||
* >JSTICKET</a>
|
* >JSTICKET</a>
|
||||||
*/
|
*/
|
||||||
public class WeixinTicketCreator implements TokenCreator {
|
public class WeixinTicketCreator extends AbstractTokenCreator {
|
||||||
|
|
||||||
private final String corpid;
|
private final String corpid;
|
||||||
private final TicketType ticketType;
|
private final TicketType ticketType;
|
||||||
private final TokenHolder weixinTokenHolder;
|
private final TokenHolder weixinTokenHolder;
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param corpid
|
* @param corpid
|
||||||
@ -39,12 +37,11 @@ public class WeixinTicketCreator implements TokenCreator {
|
|||||||
this.corpid = corpid;
|
this.corpid = corpid;
|
||||||
this.ticketType = ticketType;
|
this.ticketType = ticketType;
|
||||||
this.weixinTokenHolder = weixinTokenHolder;
|
this.weixinTokenHolder = weixinTokenHolder;
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCacheKey() {
|
public String getCacheKey0() {
|
||||||
return String.format("weixin4j_qy_ticket_%s_%s", ticketType.name(), corpid);
|
return String.format("qy_ticket_%s_%s", ticketType.name(), corpid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@ -1,56 +1,56 @@
|
|||||||
package com.foxinmy.weixin4j.qy.token;
|
package com.foxinmy.weixin4j.qy.token;
|
||||||
|
|
||||||
import com.alibaba.fastjson.TypeReference;
|
import com.alibaba.fastjson.TypeReference;
|
||||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
import com.foxinmy.weixin4j.http.weixin.WeixinRequestExecutor;
|
||||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||||
import com.foxinmy.weixin4j.model.Token;
|
import com.foxinmy.weixin4j.model.Token;
|
||||||
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
import com.foxinmy.weixin4j.qy.type.URLConsts;
|
||||||
import com.foxinmy.weixin4j.token.TokenCreator;
|
import com.foxinmy.weixin4j.token.AbstractTokenCreator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信企业号TOKEN创建
|
* 微信企业号TOKEN创建
|
||||||
*
|
*
|
||||||
* @className WeixinTokenCreator
|
* @className WeixinTokenCreator
|
||||||
* @author jy
|
* @author jy
|
||||||
* @date 2015年1月10日
|
* @date 2015年1月10日
|
||||||
* @since JDK 1.6
|
* @since JDK 1.6
|
||||||
* @see <a
|
* @see <a href=
|
||||||
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">微信企业号获取token说明</a>
|
* "http://qydev.weixin.qq.com/wiki/index.php?title=%E4%B8%BB%E5%8A%A8%E8%B0%83%E7%94%A8">
|
||||||
* @see com.foxinmy.weixin4j.model.Token
|
* 微信企业号获取token说明</a>
|
||||||
*/
|
* @see com.foxinmy.weixin4j.model.Token
|
||||||
public class WeixinTokenCreator implements TokenCreator {
|
*/
|
||||||
|
public class WeixinTokenCreator extends AbstractTokenCreator {
|
||||||
private final WeixinRequestExecutor weixinExecutor;
|
|
||||||
private final String corpid;
|
private final WeixinRequestExecutor weixinExecutor;
|
||||||
private final String corpsecret;
|
private final String corpid;
|
||||||
|
private final String corpsecret;
|
||||||
/**
|
|
||||||
*
|
/**
|
||||||
* @param corpid
|
*
|
||||||
* 企业号ID
|
* @param corpid
|
||||||
* @param corpsecret
|
* 企业号ID
|
||||||
* 企业号secret
|
* @param corpsecret
|
||||||
*/
|
* 企业号secret
|
||||||
public WeixinTokenCreator(String corpid, String corpsecret) {
|
*/
|
||||||
this.corpid = corpid;
|
public WeixinTokenCreator(String corpid, String corpsecret) {
|
||||||
this.corpsecret = corpsecret;
|
this.corpid = corpid;
|
||||||
this.weixinExecutor = new WeixinRequestExecutor();
|
this.corpsecret = corpsecret;
|
||||||
}
|
this.weixinExecutor = new WeixinRequestExecutor();
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public String getCacheKey() {
|
@Override
|
||||||
return String.format("weixin4j_qy_token_%s", corpid);
|
public String getCacheKey0() {
|
||||||
}
|
return String.format("qy_token_%s", corpid);
|
||||||
|
}
|
||||||
@Override
|
|
||||||
public Token createToken() throws WeixinException {
|
@Override
|
||||||
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid,
|
public Token createToken() throws WeixinException {
|
||||||
corpsecret);
|
String tokenUrl = String.format(URLConsts.ASSESS_TOKEN_URL, corpid, corpsecret);
|
||||||
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
WeixinResponse response = weixinExecutor.get(tokenUrl);
|
||||||
Token token = response.getAsObject(new TypeReference<Token>() {
|
Token token = response.getAsObject(new TypeReference<Token>() {
|
||||||
});
|
});
|
||||||
token.setOriginalResult(response.getAsString());
|
token.setOriginalResult(response.getAsString());
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user