当前位置: 首页 > news >正文

重庆市建设网站做网站公司

重庆市建设网站,做网站公司,代运营是什么意思,合肥医疗网站建设目录 前言 一、黄金段位接口交互 二、钻石段位接口交互设计 1.接口文档定义 2.工具类以及demo提供 a.调用方部分代码 b.被调用方 三.星耀段位接口访问设计 1.在钻石段位的基础上,进行sdk的封装 a.maven引入 b.sdk包含工具类 四.王者段位接口访问设计 1.开发详情 2.…

目录

前言

一、黄金段位接口交互

二、钻石段位接口交互设计

1.接口文档定义

2.工具类以及demo提供

a.调用方部分代码

b.被调用方

三.星耀段位接口访问设计

1.在钻石段位的基础上,进行sdk的封装

  a.maven引入

   b.sdk包含工具类

四.王者段位接口访问设计 

1.开发详情

2.项目结构

3.系统侧使用

 4.源码下载

总结


前言

在系统开发过程中,系统与系统之间往往不是完全独立的,需要进行互相调用

黄金段位:直接http访问api接口,获取相关数据

钻石段位:定义接口规范,发放授权,api接口需要认证授权才能访问

星耀段位:提供sdk客户端,封装调用api接口所需要的加解密以及http访问的工具类

王者段位:sdk进一步封装,封装成近似本地调用的api接口,调用方只需配置appId,appSecret,接口域名即可,采用springboot自动装配,开发自己的starter


一、黄金段位接口交互

 public String doPost(String host, String uri, String body, String method) {try {String address = host + uri;URL restServiceURL = new URL(address);HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();httpConnection.setRequestMethod(method);httpConnection.setDoOutput(true);httpConnection.setDoInput(true);httpConnection.setRequestProperty("Content-Type", "application/json");OutputStream outputStream = httpConnection.getOutputStream();outputStream.write(body.getBytes(StandardCharsets.UTF_8));outputStream.flush();if (httpConnection.getResponseCode() != 200) {throw new RuntimeException("Failed : HTTP outor code : " + httpConnection.getResponseCode());}BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));StringBuilder output = new StringBuilder();do {output.append(responseBuffer.readLine());} while (responseBuffer.read() != -1);httpConnection.disconnect();return output.toString();} catch (Exception e) {LOGGER.error("请求post接口报错:{}", e);}return null;}

二、钻石段位接口交互设计

1.接口文档定义

账号:appId; 授权密钥:appSecret;http每次请求header中需要添加appId、randomcode、timestamp、encodekey、sign五个参数,接口将使用这五个参数进行鉴权判断请求方式是否可以使用当前API

序号

参数名

参数类型

描述

值(样例)

1

appId

String必填

授权帐号

liangxi.zeng

2

randomcode

String必填

随机生成的字符串(每次可不相同)

2sd4TeSk

3

timestamp

String必填

当前时间戳

20150917160500

4

encodekey

String必填

前三个参数拼在一起后加上SIM授权的密钥appSecret作为salt使用SHA-256算法进行加密

8729e01cb547sdc3ea645aaa9f8493ab251e5ef32c3d6628cf85f985319145e3

5

sign

String必填

签名信息,采用MD5加密,计算公式接口:sign=MD5(uri&body&appSecret),有些接口可能没有body,参数是在URL中,则将body置为空串进行签名

6

appSecretString必填

加解密双方约定自定义的字符串,用作加密,不能直接放入header

o10ympt70x8gqas8hpoctopk3lwrdfd

7Content-TypeString必填参数提交方式application/Json

 

2.工具类以及demo提供

a.调用方部分代码

加解密工具类DigestUtilspublicvoid setHeader(HttpURLConnection httpConnection,String uri,String body,String method) throws ProtocolException {httpConnection.setRequestMethod(method);httpConnection.setDoOutput(true);httpConnection.setDoInput(true);httpConnection.setRequestProperty("Content-Type", "application/json");httpConnection.setRequestProperty("appId", appId);String randomCode = RandomStringUtils.random(16, true, true);httpConnection.setRequestProperty("randomcode", randomCode);String dateNow = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss");httpConnection.setRequestProperty("timestamp", dateNow);//加密String encodeKey = DigestUtils.sha256Hex(StringUtils.join(appUser, randomCode, dateNow, "{", privateKey, "}"));httpConnection.setRequestProperty("encodekey", encodeKey);//签名String sign = DigestUtils.md5Hex(StringUtils.join(uri, "&", body, "&", privateKey));httpConnection.setRequestProperty("sign", sign.toUpperCase(Locale.ROOT));}public String doPost(String host, String uri, String body, String method) {try {String address = host + uri;URL restServiceURL = new URL(address);HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();//设置加解密参数认证参数setHeader(httpConnection,url,body,method);OutputStream outputStream = httpConnection.getOutputStream();outputStream.write(body.getBytes(StandardCharsets.UTF_8));outputStream.flush();if (httpConnection.getResponseCode() != 200) {throw new RuntimeException("Failed : HTTP outor code : " + httpConnection.getResponseCode());}BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));StringBuilder output = new StringBuilder();do {output.append(responseBuffer.readLine());} while (responseBuffer.read() != -1);httpConnection.disconnect();return output.toString();} catch (Exception e) {LOGGER.error("请求post接口报错:{}", e);}return null;}

b.被调用方

对请求头里传入的参数进行一一校验即可,设计成过滤器拦截,提供给外部的接口都需要认证鉴权

三.星耀段位接口访问设计

1.在钻石段位的基础上,进行sdk的封装

     a.maven引入

<dependency><groupId>com.tcl.api.auth</groupId><artifactId>auth-util</artifactId><version>2.2.0-RELEASE<</version>
</dependency>

   b.sdk包含工具类


/*** api 访问工具类* @author liangxi.zeng*/
@Slf4j
public class ApiHttpUtils {/*** 设置请求同* @param httpConnection* @param uri* @param appId* @param body* @param method* @throws ProtocolException*/private static void setRequestHeader(HttpURLConnection httpConnection, String uri,String appId,String appSecret, String body, String method) throws ProtocolException {httpConnection.setRequestMethod(method);httpConnection.setDoOutput(true);httpConnection.setDoInput(true);httpConnection.setRequestProperty("Content-Type", "application/json");httpConnection.setRequestProperty("appuser", appId);String randomCode = RandomStringUtils.random(16, true, true);httpConnection.setRequestProperty("randomcode", randomCode);log.debug("randomcode:{}", randomCode);String dateNow = DateUtil.format(DateUtil.date(), "yyyyMMddHHmmss'Z'");log.debug("dateNow:{}", dateNow);httpConnection.setRequestProperty("timestamp", dateNow);//加密String encodeKey = DigestUtils.sha256Hex(StringUtils.join(appId, randomCode, dateNow, "{", appSecret, "}"));log.debug("encodeKey:{}", encodeKey);httpConnection.setRequestProperty("encodekey", encodeKey);//签名String sign = DigestUtils.md5Hex(StringUtils.join(uri, "&", body, "&", appSecret));log.debug("sign:{}", sign);httpConnection.setRequestProperty("sign", sign.toUpperCase(Locale.ROOT));}/*** 发送post请求* @param host* @param uri* @param body* @return*/public static String doPost(String host, String uri, String body,String appId,String appSecret) {try {String address = host + uri;log.debug("appuser:{},privateket:{},address:{}", appId, appSecret,address);URL restServiceURL = new URL(address);HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();setRequestHeader(httpConnection,uri,body,appId,appSecret,"POST");return getResponse(httpConnection,body);} catch (Exception e) {log.error("请求idm post接口报错:{}", e);}return null;}/*** 发送post请求* @param host* @param uri* @param body* @return*/public static String doGet(String host, String uri, String body,String appId,String appSecret) {try {String address = host + uri;log.debug("appuser:{},privateket:{},address:{}", appId, appSecret,address);URL restServiceURL = new URL(address);HttpURLConnection httpConnection = (HttpURLConnection) restServiceURL.openConnection();setRequestHeader(httpConnection,uri,body,appId,appSecret,"GET");return getResponse(httpConnection,body);} catch (Exception e) {log.error("请求idm post接口报错:{}", e);}return null;}/*** 获取响应内容* @param httpConnection* @param body* @return* @throws IOException*/private static String getResponse(HttpURLConnection httpConnection,String body) throws IOException {OutputStream outputStream = httpConnection.getOutputStream();outputStream.write(body.getBytes(StandardCharsets.UTF_8));outputStream.flush();if (httpConnection.getResponseCode() != 200) {throw new RuntimeException("Failed : HTTP outor code : " + httpConnection.getResponseCode());}BufferedReader responseBuffer = new BufferedReader(new InputStreamReader(httpConnection.getInputStream(), "UTF-8"));StringBuilder output = new StringBuilder();do {output.append(responseBuffer.readLine());} while (responseBuffer.read() != -1);httpConnection.disconnect();return output.toString();}}

四.王者段位接口访问设计 

1.开发详情

a.基于springboot的spring.factories开发自己的starter

b.采用openFeign实现http远程接口访问

c.用FeignRequestInterceptor完成请求头的权限认证参数放入

2.项目结构

3.系统侧使用

api:auth:appId: 12323appSecret: lakdsjlajdsljajskdjfdomain: https://sp.tcl.com/portal/
<dependency><groupId>com.tcl.ea.zenglx</groupId><artifactId>api-auth-spring-boot-starter</artifactId><version>1.0-SNAPSHOT</version>
</dependency>
@Service
public class RemoteDeal {@Autowiredprivate ApiClient apiClient;//获取用户信息public User getUserInfo() {return apiClient.getUserInfo();}}

 4.源码下载

api认证源码


总结

 1.发起http请求的开源框架有, Forest,httpClient,feign,OKHttp等

 2.开发组件需要了解spring的生命周期,各种特性,springboot的各种特性

http://www.hkea.cn/news/917252/

相关文章:

  • 怎么自己做网站吓别人金融网站推广圳seo公司
  • 彩票网站的客服有做吗海淀seo搜索优化多少钱
  • 河源哪有做网站网页模板设计
  • 手机网站可以做英文版本吗近三天时政热点
  • 怎么做网站游戏网络优化排名培训
  • ic外贸网站建设黑帽seo技巧
  • 实业有限公司网站怎么做百度一下了你就知道官网
  • 企业电子商务网站推广平台有哪些渠道
  • 本地用织梦做网站百度的网站网址
  • 基础展示营销型型网站新闻发稿平台有哪些
  • 做游戏赚钱的网站最新新闻热点事件2022
  • 商务网站建设哪家好推广代理公司
  • 自己做网站是否要买云主机西安百度提升优化
  • 成都注册公司哪个区好分析网站推广和优化的原因
  • 模板建站杭州seo泽成
  • 济南网站建设公司川芎网络怎么注册自己的网址
  • linux下安装wordpress关键词优化排名查询
  • wordpress手机网站怎么做中央电视台一套广告价目表
  • 百家号如何给网站做推广推广方案是什么
  • 西安三网合一网站建设产品线上推广方案
  • 2023年免费b站入口百度网站优化
  • 响应式网站建设有利于seo网站发布与推广方案
  • 网页制作教程课件seo推广排名重要吗
  • 小规模纳税人企业所得税怎么征收广州seo招聘
  • 济南企业自助建站网络营销策划公司
  • iis 新建网站 要登录温州seo推广外包
  • 个人想做企业网站备案惠州seo代理商
  • 做公务员题的网站口红的推广软文
  • 福州网站建设 联系yanktcn 04上海百网优seo优化公司
  • 网站备案号如何获得网站建设营销推广