This commit is contained in:
tanghc
2019-02-27 12:37:14 +08:00
parent 85d61d2f1e
commit 46a546f1da
196 changed files with 13210 additions and 47 deletions

View File

@@ -0,0 +1,17 @@
package com.gitee.sop.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
// 开启网关功能
@EnableZuulProxy
@SpringBootApplication
public class SopGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(SopGatewayApplication.class, args);
}
}

View File

@@ -0,0 +1,35 @@
package com.gitee.sop.gateway.config;
import com.gitee.sop.gatewaycommon.bean.ApiContext;
import com.gitee.sop.gatewaycommon.configuration.AlipayZuulConfiguration;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;
/**
* 开通支付宝开放平台能力
* @author tanghc
*/
@Configuration
public class ZuulConfig extends AlipayZuulConfiguration {
{
Map<String, String> appSecretStore = new HashMap();
appSecretStore.put("alipay_test", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlyb9aUBaljQP/vjmBFe1mF8HsWSvyfC2NTlpT/V9E+sBxTr8TSkbzJCeeeOEm4LCaVXL0Qz63MZoT24v7AIXTuMdj4jyiM/WJ4tjrWAgnmohNOegfntTto16C3l234vXz4ryWZMR/7W+MXy5B92wPGQEJ0LKFwNEoLspDEWZ7RdE53VH7w6y6sIZUfK+YkXWSwehfKPKlx+lDw3zRJ3/yvMF+U+BAdW/MfECe1GuBnCFKnlMRh3UKczWyXWkL6ItOpYHHJi/jx85op5BWDje2pY9QowzfN94+0DB3T7UvZeweu3zlP6diwAJDzLaFQX8ULfWhY+wfKxIRgs9NoiSAQIDAQAB");
ApiContext.getApiConfig().addAppSecret(appSecretStore);
}
}
/**
* 开通淘宝开放平能力
*/
//@Configuration
//public class ZuulConfig extends TaobaoZuulConfiguration {
//
// {
// Map<String, String> appSecretStore = new HashMap();
// appSecretStore.put("taobao_test", "G9w0BAQEFAAOCAQ8AMIIBCgKCA");
// ApiContext.getApiConfig().addAppSecret(appSecretStore);
// }
//}

View File

@@ -0,0 +1,28 @@
server.port=8081
spring.application.name=api-gateway
# 注册中心
eureka.host=localhost
eureka.port=1111
eureka.client.serviceUrl.defaultZone=http://${eureka.host}:${eureka.port}/eureka/
# 入口地址,默认是/zuul
zuul.servlet-path=/api
# 路由配置
# story服务路由配置
# 内置有个默认的:
# zuul.routes.<name>.path=/<服务名>/**
# zuul.routes.<name>.serviceId=<服务名>
# 等同于:
# zuul.routes.<服务名>=/<服务名>/**
# Redis数据库索引默认为0
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码默认为空
spring.redis.password=

View File

@@ -0,0 +1,127 @@
package com.gitee.sop.gateway;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class PostTest extends TestBase {
String url = "http://localhost:8081/zuul";
String appKey = "test";
String secret = "123456";
@Test
public void testPost() throws Exception {
// 业务参数
Map<String, String> jsonMap = new HashMap<String, String>();
jsonMap.put("id", "1");
jsonMap.put("id", "葫芦娃");
String json = JSON.toJSONString(jsonMap);
json = URLEncoder.encode(json, "utf-8");
// 系统参数
Map<String, Object> param = new HashMap<String, Object>();
param.put("name", "story.get");
param.put("app_key", appKey);
param.put("data", json);
param.put("timestamp", getTime());
param.put("version", "2.0");
String sign = buildSign(param, secret);
param.put("sign", sign);
System.out.println("=====请求数据=====");
String postJson = JSON.toJSONString(param);
System.out.println(postJson);
post(url, param); // 发送请求
}
/**
* 构建签名
*
* @param paramsMap
* 参数
* @param secret
* 密钥
* @return
* @throws IOException
*/
public static String buildSign(Map<String, ?> paramsMap, String secret) throws IOException {
Set<String> keySet = paramsMap.keySet();
List<String> paramNames = new ArrayList<String>(keySet);
Collections.sort(paramNames);
StringBuilder paramNameValue = new StringBuilder();
for (String paramName : paramNames) {
paramNameValue.append(paramName).append(paramsMap.get(paramName));
}
String source = secret + paramNameValue.toString() + secret;
return md5(source);
}
/**
* 生成md5,全部大写
*
* @param message
* @return
*/
public static String md5(String message) {
try {
// 1 创建一个提供信息摘要算法的对象初始化为md5算法对象
MessageDigest md = MessageDigest.getInstance("MD5");
// 2 将消息变成byte数组
byte[] input = message.getBytes();
// 3 计算后获得字节数组,这就是那128位了
byte[] buff = md.digest(input);
// 4 把数组每一字节一个字节占八位换成16进制连成md5字符串
return byte2hex(buff);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 二进制转十六进制字符串
*
* @param bytes
* @return
*/
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public String getTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}

View File

@@ -0,0 +1,17 @@
package com.gitee.sop.gateway;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SopGatewayApplicationTests {
@Test
public void contextLoads() {
}
}

View File

@@ -0,0 +1,121 @@
package com.gitee.sop.gateway;
import com.alibaba.fastjson.JSON;
import org.junit.Test;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class SopPostTest extends TestBase {
String url = "http://localhost:8081/zuul";
String appKey = "test";
String secret = "123456";
@Test
public void testPost() throws Exception {
// 系统参数
Map<String, Object> param = new HashMap<String, Object>();
param.put("method", "story.get");
param.put("app_key", appKey);
param.put("timestamp", getTime());
param.put("version", "2.0");
// 业务参数
param.put("id", "1");
param.put("name", "葫芦娃");
String sign = buildSign(param, secret);
param.put("sign", sign);
System.out.println("=====请求数据=====");
String postJson = JSON.toJSONString(param);
System.out.println(postJson);
post(url, param); // 发送请求
}
/**
* 构建签名
*
* @param paramsMap 参数
* @param secret 密钥
* @return
* @throws IOException
*/
public static String buildSign(Map<String, ?> paramsMap, String secret) throws IOException {
Set<String> keySet = paramsMap.keySet();
List<String> paramNames = new ArrayList<String>(keySet);
Collections.sort(paramNames);
StringBuilder paramNameValue = new StringBuilder();
for (String paramName : paramNames) {
paramNameValue.append(paramName).append(paramsMap.get(paramName));
}
String source = secret + paramNameValue.toString() + secret;
return md5(source);
}
/**
* 生成md5,全部大写
*
* @param message
* @return
*/
public static String md5(String message) {
try {
// 1 创建一个提供信息摘要算法的对象初始化为md5算法对象
MessageDigest md = MessageDigest.getInstance("MD5");
// 2 将消息变成byte数组
byte[] input = message.getBytes(StandardCharsets.UTF_8);
// 3 计算后获得字节数组,这就是那128位了
byte[] buff = md.digest(input);
// 4 把数组每一字节一个字节占八位换成16进制连成md5字符串
return byte2hex(buff);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 二进制转十六进制字符串
*
* @param bytes
* @return
*/
private static String byte2hex(byte[] bytes) {
StringBuilder sign = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(bytes[i] & 0xFF);
if (hex.length() == 1) {
sign.append("0");
}
sign.append(hex.toUpperCase());
}
return sign.toString();
}
public String getTime() {
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
}
}

View File

@@ -0,0 +1,102 @@
package com.gitee.sop.gateway;
import junit.framework.TestCase;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
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.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @author tanghc
*/
public class TestBase extends TestCase {
public void post(String url, String postJson) throws IOException {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost post = new HttpPost(url);
// 构造消息头
post.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
// 构建消息实体
StringEntity entity = new StringEntity(postJson, Charset.forName("UTF-8"));
entity.setContentEncoding("UTF-8");
// 发送Json格式的数据请求
entity.setContentType("application/json");
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
HttpEntity responseEntity = response.getEntity();
String content = IOUtils.toString(responseEntity.getContent(), "UTF-8");
System.out.println(content);
}
/**
* 发送POST请求
* @param url
* @return JSON或者字符串
* @throws Exception
*/
public static Object post(String url, Map<String, Object> params) throws Exception{
CloseableHttpClient client = null;
CloseableHttpResponse response = null;
try{
/**
* 创建一个httpclient对象
*/
client = HttpClients.createDefault();
/**
* 创建一个post对象
*/
HttpPost post = new HttpPost(url);
List<NameValuePair> nameValuePairs = params.entrySet().stream().map(entry -> {
return new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue()));
}).collect(Collectors.toList());
/**
* 包装成一个Entity对象
*/
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nameValuePairs, "UTF-8");
/**
* 设置请求的内容
*/
post.setEntity(entity);
/**
* 设置请求的报文头部的编码
*/
post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8"));
/**
* 执行post请求
*/
response = client.execute(post);
/**
* 通过EntityUitls获取返回内容
*/
String result = EntityUtils.toString(response.getEntity(),"UTF-8");
System.out.println(result);
return result;
}catch (Exception e){
e.printStackTrace();
}finally {
IOUtils.closeQuietly(client);
IOUtils.closeQuietly(response);
}
return null;
}
}