融云后台服务创建token

public class RongUtils {

	private static final String APPKEY = "RC-App-Key";
	private static final String NONCE = "RC-Nonce";
	private static final String TIMESTAMP = "RC-Timestamp";
	private static final String SIGNATURE = "RC-Signature";

	public static String getToken(String id) {

		String nonce = String.valueOf(Math.random() * 1000000);
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
		StringBuilder toSign = new StringBuilder("BIX27pwRm8Kd").append(nonce).append(timestamp);
		String sign = CodeUtil.hexSHA1(toSign.toString());

		Map<String, String> headers = new HashMap<String, String>();
		headers.put(APPKEY, "");
		headers.put(NONCE, nonce);
		headers.put(TIMESTAMP, timestamp);
		headers.put(SIGNATURE, sign);
		headers.put("Content-Type", "application/x-www-form-urlencoded");
		JSONObject json = JSON.parseObject(HttpUtils.post("http://api.cn.ronghub.com/user/getToken.json", "portraitUri=&name=&userId=" + id, headers).getContent());

		return json.getString("token");

	}
}

1:生成TOKEN

2:SHA1

public class CodeUtil {
	
	public static String hexSHA1(String value) {
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			md.update(value.getBytes("utf-8"));
			byte[] digest = md.digest();
			return byteToHexString(digest);
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}
	
	public static String byteToHexString(byte[] bytes) {
		return String.valueOf(Hex.encodeHex(bytes));
	}
}



猜你喜欢

转载自blog.csdn.net/wangbo54979/article/details/51699066