融云获取Token

package com.zw.admin.server.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/talk")
public class TalkController {
    private static final String App_Key = "*******";
    private static final String App_Secret = "*******";
    private static final String TIMESTAMP = "RC-Timestamp";
    private static final String updateUrl = "http://api-cn.ronghub.com/user/refresh.json";
    private static final String url = "http://api-cn.ronghub.com/user/getToken.json";
    @RequestMapping(value = "/getToken")
    @ResponseBody
    public  static  String getToken(@Param("userId") String userId, @Param("userHead") String userHead, @Param("userName") String userName) {
        StringBuffer res = new StringBuffer();
        String Timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳,从 1970 年 1 月 1 日 0 点 0 分 0 秒开始到现在的秒数。
        String Nonce = String.valueOf(Math.floor(Math.random() * 1000000));//随机数,无长度限制。
        String Signature = getSha1(App_Secret + Nonce + Timestamp);//数据签名。
        //Logger.i(Signature);
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("App-Key", App_Key);
        httpPost.setHeader("Timestamp", Timestamp);
        httpPost.setHeader("Nonce", Nonce);
        httpPost.setHeader("Signature", Signature);
        httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
        List nameValuePair = new ArrayList(1);
        nameValuePair.add(new BasicNameValuePair("name", userName));//名称(例如使用这个功能的‘张三’)
        nameValuePair.add(new BasicNameValuePair("userId", userId));// 用户id(根据自己的项目,自己生成一个串就行,UUID就行)
        nameValuePair.add(new BasicNameValuePair("portraitUri", userHead));//头像(存储头像的路径)
        HttpResponse httpResponse = null;
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair, "utf-8"));
            httpResponse = httpClient.execute(httpPost);
            BufferedReader br = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
            String line = null;
            while ((line = br.readLine()) != null) {
                res.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // System.out.println("res=" + res.toString());
        JSONObject jsonObject = JSON.parseObject(res.toString());
        //Logger.i(userRespone.getCode()+"");
        //  System.out.println(jsonObject.getString("token"));
        HashMap map = new HashMap<>();
        map.put("token",jsonObject.getString("token"));
        return jsonObject.toJSONString(map);
    }
    public static String getSha1(String str){
        if(str==null||str.length()==0){
            return null;
        }
        char hexDigits[] = {'0','1','2','3','4','5','6','7','8','9',
                'a','b','c','d','e','f'};
        try {
            MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
            mdTemp.update(str.getBytes("UTF-8"));

            byte[] md = mdTemp.digest();
            int j = md.length;
            char buf[] = new char[j*2];
            int k = 0;
            for (int i = 0; i < j; i++) {
                byte byte0 = md[i];
                buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
                buf[k++] = hexDigits[byte0 & 0xf];
            }
            return new String(buf);
        } catch (Exception e) {
            // TODO: handle exception
            return null;
        }
    }
}

网上很多人问sha1怎么弄出来的,实际是一种加密方法。

你可能感兴趣的:(融云获取Token)