- public class WeixinAction extends ActionSupport{
- private String signature;
- private String timestamp;
- private String nonce;
- private String echostr;
- private String token;
- ActionContext context = ActionContext.getContext();
- private HttpServletResponse response = ServletActionContext.getResponse();
- public void weiXinInfo() throws Exception{
- //1. 将token、timestamp、nonce三个参数进行字典序排序
- token = "weixin";
- String[] str = {token,timestamp,nonce};
- for(int i=0;i<str.length-1;i++){
- for (int j = i + 1; j < str.length; j++) {
- if(str[i].compareTo(str[j])>0){//字符串比较用compareTo方法
- String temp = str[i];
- str[i] = str[j];
- str[j] = temp;
- }
- }
- }
- StringBuilder sb = new StringBuilder();
- for(String str1 : str){
- sb.append(str1);
- }
- String s = sb.toString();
- response.setContentType("text/html");
- response.setCharacterEncoding("UTF-8");
- PrintWriter out= response.getWriter();
- if(SHA1Util.encodeBySHA(s).equals(signature)){
- //LogUtil.logger.info("echo"+echostr);
- out.write(echostr);
- }else{
- //LogUtil.logger.info("fail"+echostr);
- out.write("false");
- }
- out.flush();
- out.close();
- }
- public String getToken() {
- return token;
- }
- public void setToken(String token) {
- this.token = token;
- }
- public String getSignature() {
- return signature;
- }
- public void setSignature(String signature) {
- this.signature = signature;
- }
- public String getTimestamp() {
- return timestamp;
- }
- public void setTimestamp(String timestamp) {
- this.timestamp = timestamp;
- }
- public String getNonce() {
- return nonce;
- }
- public void setNonce(String nonce) {
- this.nonce = nonce;
- }
- public String getEchostr() {
- return echostr;
- }
- public void setEchostr(String echostr) {
- this.echostr = echostr;
- }
- }
- //进行SHA-1加密
- public class SHA1Util {
- /**
- * 转换字节数组为十六进制字符串
- * @param b
- * @return
- */
- private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
- public static String byteArrayToHexString(byte[] b){
- StringBuffer stringBuffer = new StringBuffer();
- for (int i = 0; i < b.length; i++){
- stringBuffer.append(byteToHexString(b[i]));
- }
- return stringBuffer.toString();
- }
- /**
- * 将一个字节转化成十六进制形式的字符串
- * @param b
- * @return
- */
- public static String byteToHexString(byte b){
- int n = b;
- if (n < 0) {
- n = 256 + n;
- }
- int d1 = n / 16;
- int d2 = n % 16;
- return hexDigits[d1] + hexDigits[d2];
- }
- /**
- * 字符串SHA-1加密
- * @param string
- * @return
- */
- public static String encodeBySHA(String string){
- if(string != null && !string.isEmpty()){
- try {
- //创建具有指定算法名称的信息摘要
- MessageDigest messageDigest = MessageDigest.getInstance("SHA-1");
- //使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
- byte[] bytes = messageDigest.digest(string.getBytes());
- //将得到的字节数组变成字符串返回
- string = byteArrayToHexString(bytes);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- return string;
- }
- }
-
- httpClient请求绕过验证
-
HttpClient httpclient = new DefaultHttpClient();
httpclient = HttpUtil.wrapClient(httpclient);/**
* 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”异常
* 不用导入SSL证书
*
* @author shipengzhi(shipengzhi@sogou-inc.com)
*
*/
public static org.apache.http.client.HttpClient wrapClient(org.apache.http.client.HttpClient base) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
@SuppressWarnings("unused")
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@SuppressWarnings("unused")
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
}
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException {
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("https", 443, ssf));
ThreadSafeClientConnManager mgr = new ThreadSafeClientConnManager(registry);
return new DefaultHttpClient(mgr, base.getParams());
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}