打印微信请求日志时地址中附带access_token #200

This commit is contained in:
Binary Wang
2017-04-28 10:35:16 +08:00
parent 48d6f10cb2
commit 19b3b991f9
4 changed files with 22 additions and 20 deletions

View File

@@ -1,16 +1,9 @@
package me.chanjar.weixin.mp.api.impl;
import java.io.IOException;
import java.util.concurrent.locks.Lock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import me.chanjar.weixin.common.bean.WxJsapiSignature;
import me.chanjar.weixin.common.bean.result.WxError;
import me.chanjar.weixin.common.exception.WxErrorException;
@@ -22,8 +15,13 @@ import me.chanjar.weixin.common.util.http.*;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.bean.*;
import me.chanjar.weixin.mp.bean.result.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractWxMpService<H,P> implements WxMpService,RequestHttp<H,P> {
import java.io.IOException;
import java.util.concurrent.locks.Lock;
public abstract class AbstractWxMpServiceImpl<H, P> implements WxMpService, RequestHttp<H, P> {
private static final JsonParser JSON_PARSER = new JsonParser();
@@ -46,7 +44,6 @@ public abstract class AbstractWxMpService<H,P> implements WxMpService,RequestHtt
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@Override
public boolean checkSignature(String timestamp, String nonce, String signature) {
try {
@@ -296,7 +293,6 @@ public abstract class AbstractWxMpService<H,P> implements WxMpService,RequestHtt
do {
try {
T result = executeInternal(executor, uri, data);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, result);
return result;
} catch (WxErrorException e) {
if (retryTimes + 1 > this.maxRetryTimes) {
@@ -326,16 +322,22 @@ public abstract class AbstractWxMpService<H,P> implements WxMpService,RequestHtt
}
public synchronized <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
if (uri.indexOf("access_token=") != -1) {
if (uri.contains("access_token=")) {
throw new IllegalArgumentException("uri参数中不允许有access_token: " + uri);
}
String accessToken = getAccessToken(false);
String uriWithAccessToken = uri;
uriWithAccessToken += uri.indexOf('?') == -1 ? "?access_token=" + accessToken : "&access_token=" + accessToken;
String uriWithAccessToken;
if (uri.contains("?")) {
uriWithAccessToken = uri + "&access_token=" + accessToken;
} else {
uriWithAccessToken = uri + "?access_token=" + accessToken;
}
try {
return executor.execute(this, uriWithAccessToken, data);
T result = executor.execute(this, uriWithAccessToken, data);
this.log.debug("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, result);
return result;
} catch (WxErrorException e) {
WxError error = e.getError();
/*
@@ -352,12 +354,12 @@ public abstract class AbstractWxMpService<H,P> implements WxMpService,RequestHtt
}
if (error.getErrorCode() != 0) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uri, data, error);
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[RESPONSE]: {}", uriWithAccessToken, data, error);
throw new WxErrorException(error);
}
return null;
} catch (IOException e) {
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uri, data, e.getMessage());
this.log.error("\n[URL]: {}\n[PARAMS]: {}\n[EXCEPTION]: {}", uriWithAccessToken, data, e.getMessage());
throw new RuntimeException(e);
}
}

View File

@@ -16,12 +16,12 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.impl.AbstractWxMpService;
import me.chanjar.weixin.mp.api.impl.AbstractWxMpServiceImpl;
/**
* apache-http方式实现
*/
public class WxMpServiceImpl extends AbstractWxMpService<CloseableHttpClient,HttpHost> {
public class WxMpServiceImpl extends AbstractWxMpServiceImpl<CloseableHttpClient,HttpHost> {
private CloseableHttpClient httpClient;
private HttpHost httpProxy;

View File

@@ -13,7 +13,7 @@ import java.util.concurrent.locks.Lock;
/**
* jodd-http方式实现
*/
public class WxMpServiceImpl extends AbstractWxMpService<HttpConnectionProvider,ProxyInfo> {
public class WxMpServiceImpl extends AbstractWxMpServiceImpl<HttpConnectionProvider,ProxyInfo> {
private HttpConnectionProvider httpClient;
private ProxyInfo httpProxy;

View File

@@ -11,7 +11,7 @@ import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.api.impl.*;
import okhttp3.*;
public class WxMpServiceImpl extends AbstractWxMpService<ConnectionPool, OkhttpProxyInfo> {
public class WxMpServiceImpl extends AbstractWxMpServiceImpl<ConnectionPool, OkhttpProxyInfo> {
private ConnectionPool httpClient;