发表于: 2017-11-16 21:12:05

2 830


今天做的事:


今天弄微信,脑袋都要扣爆了,真的烦。


先说今天把昨天和微信对接的接口写完,然后测试成功,如下:

/**
* 和微信接入
* */
@RequestMapping(value = "/wx",method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response){


// 微信加密签名,signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。
   String signature = request.getParameter("signature");
   //时间戳
   String timestamp = request.getParameter("timestamp");
   //随机数
   String nonce = request.getParameter("nonce");
   //随机字符串
   String echostr = request.getParameter("echostr");

   PrintWriter out = null;
   
   try{
out = response.getWriter();
       // 通过检验signature对请求进行校验,若校验成功则原样返回echostr,否则接入失败
       if(SignUtil.checkSignature(signature,timestamp,nonce)){
out.print(echostr);
       }
}catch (IOException e){
e.printStackTrace();
   }finally {
out.close();
       out = null;
   }

}

这里用到一个工具类,使用了SHA-1加密,工具类如下:

package util;


import constant.WeChatConstant;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
* Created by yubotao on 2017/11/15.
*/
public class SignUtil {

/**
    *验证签名
    * @param signature
    * @param timestamp
    * @param nonce
    */
   public static boolean checkSignature(String signature,String timestamp,String nonce){
//第一个参数是token
       String[] arr = new String[]{WeChatConstant.token,timestamp,nonce};
       //将token、timestamp、nonce三个参数进行字典排序
       Arrays.sort(arr);
       
       StringBuilder content = new StringBuilder();
       
       for(int i =0; i < arr.length; i++){
content.append(arr[i]);
       }

MessageDigest md = null;
       String tmpStr = null;
       
       try{
md = MessageDigest.getInstance("SHA-1");
           // 将三个参数字符串拼接成一个字符串进行sha1加密
           byte[] digest = md.digest(content.toString().getBytes());
           tmpStr = byteToStr(digest);
       }catch (NoSuchAlgorithmException e){
e.printStackTrace();
       }

content = null;
       // 将sha1加密后的字符串可与signature对比
       return tmpStr != null ? tmpStr.equals(signature.toUpperCase()):false;
       
   }


/**
    * 将字节数组转换为十六进制字符串
    * @param byteArray
    * @return
    */
   private static String byteToStr(byte[] byteArray){
String strDigest = "";
       for(int i = 0; i < byteArray.length; i++){
strDigest += byteToHexStr(byteArray[i]);
       }

return strDigest;
   }


/**
    * 将字节转换为十六进制字符串
    * @param mByte
    * @return
    */
   private static String byteToHexStr(byte mByte){
char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
       char[] tempArr = new char[2];
       tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
       tempArr[1] = Digit[mByte & 0X0F];
       
       String s = new String(tempArr);
       return s;
   }

}


然后在服务器上运行即可,好久没上自己的服务器了,然后Nginx又出了点问题,搞了有一会。


之后写了一个获取AccessToken的接口,并成功运行,如下:

@Autowired
GetAccessToken token;

@RequestMapping(value = "/a/wx/accessToken",method = RequestMethod.GET)
public String getAccessToken(HttpServletRequest request, HttpServletResponse response, Model model){

List<String> tokenList = token.getAccessToken();
   
   log.info("tokenList : " + tokenList);
   
   String accessToken = tokenList.get(0);
   String expiresIn = tokenList.get(1);

   model.addAttribute("accessToken",accessToken);
   model.addAttribute("expiresIn",expiresIn);
   
   return "tokenTest";
}

使用的Service如下:

package service;

import POJO.AccessToken;
import com.google.gson.Gson;
import constant.WeChatConstant;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
* Created by yubotao on 2017/11/16.
*/
@Service
public class GetAccessToken {

public List<String> getAccessToken(){
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + WeChatConstant.AppID + "&secret=" + WeChatConstant.AppSecret;
       
       String accessToken = null;
       String expriesIn = null;
       List<String> another = new ArrayList<String>();
       
       try{
URL urlGet = new URL(url);
           HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
           //请求方法
           http.setRequestMethod("GET");
           http.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
           http.setDoOutput(true);
           http.setDoInput(true);
           http.connect();
   
           InputStream is = http.getInputStream();
           int size = is.available();
           byte[] jsonBytes = new byte[size];
           is.read(jsonBytes);
           String message = new String(jsonBytes,"UTF-8");
           
           Gson gson = new Gson();
           AccessToken token = gson.fromJson(message,AccessToken.class);
           
           accessToken = token.getAccess_token();
           expriesIn = token.getExpires_in();
   
           System.out.println("accessToken : " + accessToken);
           System.out.println("expiresIn : " + expriesIn);
           
           another.add(accessToken);
           another.add(expriesIn);
           
       }catch (Exception e){
e.printStackTrace();
       }
return another;
   }

}


这里面其实难点,还是对于http的理解,如何使用java web,这个我还是有欠缺,一会要再看看http这块。


效果图:


然后下午一直在看如何获取用户信息,获取openedID,然后获取用户的基本信息。


然后遇到重重险阻,总之没写成,然后看的我头都大了。。。


明天计划:争取把用户基本信息都获取到,然后需要完善一下获取AccessToken,因为这个地方要做个定时,是有时效性的。


问题:头疼,自己太菜了,感觉啥都不会。


收获:面前用java代码和微信平台对接,并且成功获取到AccessToken。



返回列表 返回列表
评论

    分享到