Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

Commit

Permalink
Merge branch 'release/1.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
chanjarster committed Oct 19, 2014
2 parents 3e28f84 + 6b02fac commit 97970b5
Show file tree
Hide file tree
Showing 20 changed files with 990 additions and 140 deletions.
24 changes: 23 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-tools</artifactId>
<version>1.0.0</version>
<version>1.0.1</version>
<name>WeiXin Java Tools</name>
<description>用于开发微信公众号的Java工具</description>
<url>https://github.com/chanjarster/weixin-java-tools</url>
Expand Down Expand Up @@ -96,6 +96,28 @@
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.3.0.M0</version>
<scope>test</scope>
</dependency>
</dependencies>

<distributionManagement>
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/me/chanjar/weixin/api/WxConfigStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public interface WxConfigStorage {
public String getSecret();

public String getToken();


public String getAesKey();

public int getExpiresIn();

}
10 changes: 10 additions & 0 deletions src/main/java/me/chanjar/weixin/api/WxInMemoryConfigStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class WxInMemoryConfigStorage implements WxConfigStorage {
protected String secret;
protected String token;
protected String accessToken;
protected String aesKey;
protected int expiresIn;

public void updateAccessToken(WxAccessToken accessToken) {
Expand Down Expand Up @@ -56,6 +57,14 @@ public void setToken(String token) {
this.token = token;
}

public String getAesKey() {
return aesKey;
}

public void setAesKey(String aesKey) {
this.aesKey = aesKey;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
Expand All @@ -64,4 +73,5 @@ public void setExpiresIn(int expiresIn) {
this.expiresIn = expiresIn;
}


}
24 changes: 2 additions & 22 deletions src/main/java/me/chanjar/weixin/api/WxServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import me.chanjar.weixin.bean.WxCustomMessage;
import me.chanjar.weixin.util.crypto.SHA1;
import me.chanjar.weixin.util.json.GsonHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.ClientProtocolException;
Expand Down Expand Up @@ -67,33 +68,12 @@ public class WxServiceImpl implements WxService {

public boolean checkSignature(String timestamp, String nonce, String signature) {
try {
String token = wxConfigStorage.getToken();
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
String[] arr = new String[] { token, timestamp, nonce };
Arrays.sort(arr);
StringBuilder sb = new StringBuilder();
for(String a : arr) {
sb.append(a);
}
sha1.update(sb.toString().getBytes());
byte[] output = sha1.digest();
return bytesToHex(output).equals(signature);
return SHA1.gen(wxConfigStorage.getToken(), timestamp, nonce).equals(signature);
} catch (Exception e) {
return false;
}
}

protected String bytesToHex(byte[] b) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
StringBuffer buf = new StringBuffer();
for (int j = 0; j < b.length; j++) {
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
buf.append(hexDigit[b[j] & 0x0f]);
}
return buf.toString();
}

public void accessTokenRefresh() throws WxErrorException {
if (!GLOBAL_ACCESS_TOKEN_REFRESH_FLAG.getAndSet(true)) {
try {
Expand Down
Loading

0 comments on commit 97970b5

Please sign in to comment.