Authored by fangyeqing

INIT:first commit

  1 +.idea
  2 +*.iml
  3 +target/
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project xmlns="http://maven.apache.org/POM/4.0.0"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5 + <modelVersion>4.0.0</modelVersion>
  6 +
  7 + <groupId>com.xkl</groupId>
  8 + <artifactId>xkl-web-example</artifactId>
  9 + <version>1.0-SNAPSHOT</version>
  10 +
  11 + <dependencies>
  12 + <!--lombok-->
  13 + <dependency>
  14 + <groupId>org.projectlombok</groupId>
  15 + <artifactId>lombok</artifactId>
  16 + <version>1.16.8</version>
  17 + </dependency>
  18 + <!-- https://mvnrepository.com/artifact/junit/junit -->
  19 + <dependency>
  20 + <groupId>junit</groupId>
  21 + <artifactId>junit</artifactId>
  22 + <version>4.12</version>
  23 + </dependency>
  24 +
  25 + <dependency>
  26 + <groupId>org.json</groupId>
  27 + <artifactId>json</artifactId>
  28 + <version>20160810</version>
  29 + </dependency>
  30 +
  31 + </dependencies>
  32 +
  33 +</project>
  1 +package com.xkl;
  2 +
  3 +/**
  4 + * Created by win7 on 2016/12/25.
  5 + */
  6 +public interface Constants {
  7 +
  8 + String URL_PREFIX = "http://127.0.0.1:8090";
  9 + //String URL_PREFIX = "https://139.129.166.85:8090";
  10 +
  11 + String KEY = "weixin";
  12 +}
  1 +package com.xkl;
  2 +
  3 +import java.security.MessageDigest;
  4 +
  5 +/**
  6 + * Created by win7 on 2016/12/25.
  7 + */
  8 +public class EncodeTools {
  9 + private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
  10 + 'e', 'f' };
  11 +
  12 + public static String encode(String algorithm, String str) {
  13 + if (str == null) {
  14 + return null;
  15 + }
  16 + try {
  17 + MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
  18 + messageDigest.update(str.getBytes());
  19 + return getFormattedText(messageDigest.digest());
  20 + } catch (Exception e) {
  21 + throw new RuntimeException(e);
  22 + }
  23 +
  24 + }
  25 +
  26 + /**
  27 + * Takes the raw bytes from the digest and formats them correct.
  28 + *
  29 + * @param bytes
  30 + * the raw bytes from the digest.
  31 + * @return the formatted bytes.
  32 + */
  33 + private static String getFormattedText(byte[] bytes) {
  34 + int len = bytes.length;
  35 + StringBuilder buf = new StringBuilder(len * 2);
  36 + // 把密文转换成十六进制的字符串形式
  37 + for (int j = 0; j < len; j++) {
  38 + buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
  39 + buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
  40 + }
  41 + return buf.toString();
  42 + }
  43 +
  44 +
  45 +}
  1 +package com.xkl;
  2 +
  3 +import java.io.BufferedReader;
  4 +import java.io.DataOutputStream;
  5 +import java.io.IOException;
  6 +import java.io.InputStreamReader;
  7 +import java.net.HttpURLConnection;
  8 +import java.net.URL;
  9 +import java.net.URLEncoder;
  10 +import java.util.*;
  11 +
  12 +/**
  13 + * Created by win7 on 2016/12/25.
  14 + */
  15 +public class HttpTools {
  16 +
  17 + /**
  18 + * 获取当前时间,10位
  19 + * @return
  20 + */
  21 + public static String getNow(){
  22 + Calendar c = Calendar.getInstance();//可以对每个时间域单独修改
  23 + return String.valueOf(c.getTimeInMillis()/1000);
  24 + }
  25 +
  26 + /**
  27 + * 获取签名
  28 + *
  29 + * 加密算法: sign=md5(str + t + key),32位小写md5
  30 + * t: 北京时间距离1970年1月1日的秒数,10位
  31 + * key:平台对应的key,此值喜开路管理员授予
  32 + * str:业务参数数组,按参数名递增排序排列拼接,例如用户登陆接口,coid+password+username
  33 + *
  34 + * 需要验证:
  35 + * sign:是否正确
  36 + * t:是否过期,客户端与服务器时间必须为±300s以内,否则时间验证过期。
  37 + * @param params
  38 + * @return
  39 + */
  40 + public static String getSign(Map<String, String> params){
  41 + String sign = "";
  42 + String paramStr = "";
  43 + List<String> keys = new ArrayList<String>();
  44 + for(Map.Entry<String, String> entry : params.entrySet()) {
  45 + keys.add(entry.getKey());
  46 + }
  47 + Collections.sort(keys);
  48 + for(String para:keys){
  49 + if(!para.equals("sign")&&!para.equals("t")&&!para.equals("type"))
  50 + paramStr += params.get(para);
  51 + }
  52 + sign = EncodeTools.encode("MD5",paramStr + params.get("t") + Constants.KEY);
  53 +
  54 + return sign;
  55 + }
  56 +
  57 + /**
  58 + * 发送http请求
  59 + * @param url 请求url
  60 + * @param type 请求类型:GET,POST,DELETE
  61 + * @param params 请求参数:map类型
  62 + * @return
  63 + */
  64 + public static String requestByMapWithToken(String url, String type, Map<String, String> params, String authorization){
  65 + String reqbody = getRequestData(params,"UTF-8").toString();
  66 + String result = requestByString(url + "?" + reqbody,type,authorization,null);
  67 + return result;
  68 + }
  69 +
  70 + public static String requestByMap(String url, String type,Map<String, String> params){
  71 + return requestByMapWithToken(url,type,params,"");
  72 + }
  73 +
  74 + /**
  75 + * 发送http请求
  76 + * @param url 请求url
  77 + * @param type 请求类型:GET,POST,DELETE
  78 + * @param reqbody 请求参数:String类型
  79 + * @return
  80 + */
  81 + public static String requestByString(String url, String type, String authorization,String reqbody){
  82 + HttpURLConnection con = null;
  83 + String result = null;
  84 + try {
  85 + con = getHttpConnection( url , type, authorization);
  86 + //you can add any request body here if you want to post
  87 + if( reqbody != null){
  88 + DataOutputStream out = new DataOutputStream(con.getOutputStream());
  89 + out.writeBytes(reqbody);
  90 + out.flush();
  91 + out.close();
  92 + }
  93 + con.connect();
  94 + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
  95 + String temp = null;
  96 + StringBuilder sb = new StringBuilder();
  97 + while((temp = in.readLine()) != null){
  98 + sb.append(temp).append(" ");
  99 + }
  100 + result = sb.toString();
  101 + in.close();
  102 + } catch (IOException e) {
  103 + System.out.println(e);
  104 + }
  105 + return result;
  106 +
  107 + }
  108 +
  109 + private static HttpURLConnection getHttpConnection(String url, String type, String authorization){
  110 + URL uri = null;
  111 + HttpURLConnection con = null;
  112 + try{
  113 + uri = new URL(url);
  114 + con = (HttpURLConnection) uri.openConnection();
  115 + con.setRequestMethod(type); //type: POST, PUT, DELETE, GET
  116 + if(type.equals("POST"))
  117 + con.setDoOutput(true);
  118 + con.setConnectTimeout(60000); //60 secs
  119 + con.setReadTimeout(60000); //60 secs
  120 + con.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
  121 + con.setRequestProperty("authorization",authorization);
  122 + }catch(Exception e){
  123 + System.out.println( "connection i/o failed" );
  124 + }
  125 +
  126 +
  127 + return con;
  128 + }
  129 +
  130 + private static StringBuffer getRequestData(Map<String, String> params, String encode) {
  131 + StringBuffer stringBuffer = new StringBuffer(); //存储封装好的请求体信息
  132 + try {
  133 + for(Map.Entry<String, String> entry : params.entrySet()) {
  134 + stringBuffer.append(entry.getKey())
  135 + .append("=")
  136 + .append(URLEncoder.encode(entry.getValue(), encode))
  137 + .append("&");
  138 + }
  139 + stringBuffer.deleteCharAt(stringBuffer.length() - 1); //删除最后的一个"&"
  140 + } catch (Exception e) {
  141 + e.printStackTrace();
  142 + }
  143 + return stringBuffer;
  144 + }
  145 +
  146 +}
  1 +package com.xkl;
  2 +
  3 +import org.junit.Test;
  4 +
  5 +import java.util.HashMap;
  6 +import java.util.Map;
  7 +
  8 +/**
  9 + * Created by win7 on 2017/1/2.
  10 + */
  11 +public class OpenIdTest {
  12 + private static final String URL_OPEN_ID = Constants.URL_PREFIX + "/openId";
  13 + private static final String URL_LOGIN = Constants.URL_PREFIX + "/openId/login";
  14 +
  15 + @Test
  16 + public void testBind(){
  17 + Map<String,String> params = new HashMap<String, String>();
  18 + params.put("username","user1");
  19 + params.put("password",EncodeTools.encode("MD5","pass1"));
  20 + params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
  21 + params.put("openIdType","0");
  22 +
  23 + params.put("t",HttpTools.getNow());
  24 + params.put("type","1");
  25 + params.put("sign",HttpTools.getSign(params));
  26 + String response = HttpTools.requestByMap(URL_OPEN_ID,"POST",params);
  27 + System.out.println(response);
  28 + }
  29 +
  30 + @Test
  31 + public void testUnBind(){
  32 + Map<String,String> params = new HashMap<String, String>();
  33 + params.put("username","user1");
  34 + params.put("password",EncodeTools.encode("MD5","pass1"));
  35 + params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
  36 + params.put("openIdType","0");
  37 +
  38 + params.put("t",HttpTools.getNow());
  39 + params.put("type","1");
  40 + params.put("sign",HttpTools.getSign(params));
  41 + String response = HttpTools.requestByMap(URL_OPEN_ID,"DELETE",params);
  42 + System.out.println(response);
  43 + }
  44 +
  45 + @Test
  46 + public void testLogin(){
  47 + Map<String,String> params = new HashMap<String, String>();
  48 + params.put("openId","abcdefghijklmnopqrstuvwxyz123456");
  49 + params.put("openIdType","0");
  50 +
  51 + params.put("t",HttpTools.getNow());
  52 + params.put("type","1");
  53 + params.put("sign",HttpTools.getSign(params));
  54 + String response = HttpTools.requestByMap(URL_LOGIN,"POST",params);
  55 + System.out.println(response);
  56 + }
  57 +}
  1 +package com.xkl;
  2 +
  3 +import org.junit.BeforeClass;
  4 +import org.junit.Test;
  5 +
  6 +import java.util.HashMap;
  7 +import java.util.Map;
  8 +
  9 +import static com.xkl.TokenTest.loginAndGetToken;
  10 +
  11 +/**
  12 + * Created by win7 on 2016/12/26.
  13 + * 报告相关
  14 + */
  15 +public class ReportTest {
  16 + private static final String URL_REPORT_LIST = Constants.URL_PREFIX + "/report/list";
  17 + private static final String URL_REPORT_DETAIL = Constants.URL_PREFIX + "/report/detail";
  18 + private static final String URL_ITEM_INFO = Constants.URL_PREFIX + "/report/itemInfo";
  19 + private static final String URL_ITEM_GRAPH = Constants.URL_PREFIX + "/report/itemGraph";
  20 + private static String token;
  21 +
  22 + @BeforeClass
  23 + public static void init(){
  24 + token = loginAndGetToken("user1","pass1");
  25 + }
  26 +
  27 + @Test
  28 + public void testList(){
  29 + Map<String,String> params = new HashMap<String, String>();
  30 + params.put("t",HttpTools.getNow());
  31 + params.put("type","1");
  32 + params.put("sign",HttpTools.getSign(params));
  33 + String response = HttpTools.requestByMapWithToken(URL_REPORT_LIST,"GET",params,token);
  34 + System.out.println(response);
  35 + }
  36 +
  37 + @Test
  38 + public void testDetail(){
  39 + Map<String,String> params = new HashMap<String, String>();
  40 + params.put("report_id","1");
  41 + params.put("t",HttpTools.getNow());
  42 + params.put("type","1");
  43 + params.put("sign",HttpTools.getSign(params));
  44 + String response = HttpTools.requestByMapWithToken(URL_REPORT_DETAIL,"GET",params,token);
  45 + System.out.println(response);
  46 + }
  47 +
  48 + @Test
  49 + public void testItemInfo(){
  50 + Map<String,String> params = new HashMap<String, String>();
  51 + params.put("itemId","10");
  52 + params.put("status","0");
  53 + params.put("t",HttpTools.getNow());
  54 + params.put("type","1");
  55 + params.put("sign",HttpTools.getSign(params));
  56 + String response = HttpTools.requestByMapWithToken(URL_ITEM_INFO,"GET",params,token);
  57 + System.out.println(response);
  58 + }
  59 +
  60 + @Test
  61 + public void testItemGraph(){
  62 + Map<String,String> params = new HashMap<String, String>();
  63 + params.put("itemId","10");
  64 + params.put("stime","2016-01-01 10:32:00");
  65 + params.put("etime","2016-12-22 10:32:00");
  66 + params.put("t",HttpTools.getNow());
  67 + params.put("type","1");
  68 + params.put("sign",HttpTools.getSign(params));
  69 + String response = HttpTools.requestByMapWithToken(URL_ITEM_GRAPH,"GET",params,token);
  70 + System.out.println(response);
  71 + }
  72 +}
  1 +package com.xkl;
  2 +
  3 +import org.json.JSONObject;
  4 +import org.junit.Test;
  5 +
  6 +import java.util.HashMap;
  7 +import java.util.Map;
  8 +
  9 +/**
  10 + * Created by win7 on 2017/1/2.
  11 + * 登录、退出登录测试
  12 + */
  13 +public class TokenTest {
  14 + private static final String URL_LOGIN = Constants.URL_PREFIX + "/token";
  15 +
  16 + public static String loginAndGetToken(String user,String pass){
  17 + Map<String,String> params = new HashMap<String, String>();
  18 + params.put("username",user);
  19 + params.put("password",EncodeTools.encode("MD5",pass));
  20 + params.put("t",HttpTools.getNow());
  21 + params.put("type","1");
  22 + params.put("sign",HttpTools.getSign(params));
  23 + String response = HttpTools.requestByMap(URL_LOGIN,"POST",params);
  24 + System.out.println("response:"+ response);
  25 + JSONObject jsonObject = new JSONObject(response);
  26 + JSONObject content = jsonObject.getJSONObject("content");
  27 + return content.getString("userId") + "_" + content.getString("token");
  28 + }
  29 +
  30 + @Test
  31 + public void testLogin(){
  32 + System.out.println(loginAndGetToken("user1","pass1"));
  33 + }
  34 +
  35 + @Test
  36 + public void logout(){
  37 + String token = loginAndGetToken("user1","pass1");
  38 + Map<String,String> params = new HashMap<String, String>();
  39 + params.put("t",HttpTools.getNow());
  40 + params.put("type","1");
  41 + params.put("sign",HttpTools.getSign(params));
  42 + String response = HttpTools.requestByMapWithToken(URL_LOGIN,"DELETE",params,token);
  43 + System.out.println(response);
  44 + }
  45 +}
  1 +package com.xkl;
  2 +
  3 +import org.junit.Test;
  4 +
  5 +import java.util.HashMap;
  6 +import java.util.Map;
  7 +
  8 +import static com.xkl.TokenTest.loginAndGetToken;
  9 +
  10 +/**
  11 + * Created by win7 on 2016/12/25.
  12 + * 用户注册、修改密码、获取个人信息
  13 + */
  14 +public class UserInfoTest {
  15 + private static final String URL_USER = Constants.URL_PREFIX + "/userInfo";
  16 +
  17 + @Test
  18 + public void testRegister(){
  19 + register("15211112222","pass152");
  20 + }
  21 +
  22 + @Test
  23 + public void testModPass(){
  24 + register("15211112222","pass152");
  25 +
  26 + Map<String,String> params = new HashMap<String, String>();
  27 + params.put("password",EncodeTools.encode("MD5","pass152_new"));
  28 + params.put("t",HttpTools.getNow());
  29 + params.put("type","1");
  30 + params.put("sign",HttpTools.getSign(params));
  31 + //获取token
  32 + String token = loginAndGetToken("15211112222","pass152");
  33 + System.out.println(token);
  34 + String response = HttpTools.requestByMapWithToken(URL_USER,"PUT",params,token);
  35 + System.out.println(response);
  36 + }
  37 +
  38 + @Test
  39 + public void testGetUserInfo(){
  40 + Map<String,String> params = new HashMap<String, String>();
  41 + params.put("t",HttpTools.getNow());
  42 + params.put("type","1");
  43 + params.put("sign",HttpTools.getSign(params));
  44 + //获取token
  45 + String token = loginAndGetToken("user1","pass1");
  46 + System.out.println(token);
  47 + String response = HttpTools.requestByMapWithToken(URL_USER,"GET",params,token);
  48 + System.out.println(response);
  49 + }
  50 +
  51 + public void register(String user,String pass){
  52 + Map<String,String> params = new HashMap<String, String>();
  53 + params.put("username",user);
  54 + params.put("password",EncodeTools.encode("MD5",pass));
  55 + params.put("t",HttpTools.getNow());
  56 + params.put("type","1");
  57 + params.put("sign",HttpTools.getSign(params));
  58 + String response = HttpTools.requestByMap(URL_USER,"POST",params);
  59 + System.out.println(response);
  60 + }
  61 +
  62 + @Test
  63 + public void testRegisterNoSign(){
  64 + Map<String,String> params = new HashMap<String, String>();
  65 + params.put("username","15211112222");
  66 + params.put("password",EncodeTools.encode("MD5","pass152"));
  67 + params.put("t",HttpTools.getNow());
  68 + params.put("type","1");
  69 + params.put("sign","nosign");
  70 + String response = HttpTools.requestByMap(URL_USER,"POST",params);
  71 + System.out.println(response);
  72 + }
  73 +
  74 + @Test
  75 + public void testRegisterWrongTime(){
  76 + Map<String,String> params = new HashMap<String, String>();
  77 + params.put("username","15211112222");
  78 + params.put("password",EncodeTools.encode("MD5","pass152"));
  79 + params.put("t",HttpTools.getNow()+100000);
  80 + params.put("type","1");
  81 + params.put("sign",HttpTools.getSign(params));
  82 + String response = HttpTools.requestByMap(URL_USER,"POST",params);
  83 + System.out.println(response);
  84 + }
  85 +}