template.html 20.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
<!DOCTYPE html>
<html>
<head>
<title>${标题}</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
/* RESET
=============================================================================*/

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed, figure, figcaption, footer, header, hgroup, menu, nav, output, ruby, section, summary, time, mark, audio, video {
  margin: 0;
  padding: 0;
  border: 0;
}

/* BODY
=============================================================================*/

body {
  font-family: Helvetica, arial, freesans, clean, sans-serif;
  font-size: 14px;
  line-height: 1.6;
  color: #333;
  background-color: #fff;
  padding: 20px;
  max-width: 960px;
  margin: 0 auto;
}

body>*:first-child {
  margin-top: 0 !important;
}

body>*:last-child {
  margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
  margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
  margin: 20px 0 10px;
  padding: 0;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
  font-size: inherit;
}

h1 {
  font-size: 28px;
  color: #000;
}

h2 {
  font-size: 24px;
  border-bottom: 1px solid #ccc;
  color: #000;
}

h3 {
  font-size: 18px;
}

h4 {
  font-size: 16px;
}

h5 {
  font-size: 14px;
}

h6 {
  color: #777;
  font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
  margin-top: 0;
  padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
  margin-top: 0;
  padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
  margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
  color: #4183C4;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
  padding-left: 30px;
}

ul li > :first-child, 
ol li > :first-child, 
ul li ul:first-of-type, 
ol li ol:first-of-type, 
ul li ol:first-of-type, 
ol li ul:first-of-type {
  margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
  margin-bottom: 0;
}

dl {
  padding: 0;
}

dl dt {
  font-size: 14px;
  font-weight: bold;
  font-style: italic;
  padding: 0;
  margin: 15px 0 5px;
}

dl dt:first-child {
  padding: 0;
}

dl dt>:first-child {
  margin-top: 0px;
}

dl dt>:last-child {
  margin-bottom: 0px;
}

dl dd {
  margin: 0 0 15px;
  padding: 0 15px;
}

dl dd>:first-child {
  margin-top: 0px;
}

dl dd>:last-child {
  margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
  font-size: 12px;
  font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
  margin: 0 0px;
  padding: 0px 0px;
  white-space: nowrap;
  border: 1px solid #eaeaea;
  background-color: #f8f8f8;
  border-radius: 3px;
}

pre>code {
  margin: 0;
  padding: 0;
  white-space: pre;
  border: none;
  background: transparent;
}

pre {
  background-color: #f8f8f8;
  border: 1px solid #ccc;
  font-size: 13px;
  line-height: 19px;
  overflow: auto;
  padding: 6px 10px;
  border-radius: 3px;
}

pre code, pre tt {
  background-color: transparent;
  border: none;
}

kbd {
    -moz-border-bottom-colors: none;
    -moz-border-left-colors: none;
    -moz-border-right-colors: none;
    -moz-border-top-colors: none;
    background-color: #DDDDDD;
    background-image: linear-gradient(#F1F1F1, #DDDDDD);
    background-repeat: repeat-x;
    border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
    border-image: none;
    border-radius: 2px 2px 2px 2px;
    border-style: solid;
    border-width: 1px;
    font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    line-height: 10px;
    padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
  border-left: 4px solid #DDD;
  padding: 0 15px;
  color: #777;
}

blockquote>:first-child {
  margin-top: 0px;
}

blockquote>:last-child {
  margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
  clear: both;
  margin: 15px 0;
  height: 0px;
  overflow: hidden;
  border: none;
  background: transparent;
  border-bottom: 4px solid #ddd;
  padding: 0;
}

/* TABLES
=============================================================================*/

table{
  border-collapse: collapse;
}


table th {
  font-weight: bold;
}

table th, table td {
  border: 1px solid #ccc;
  padding: 6px 13px;
}

table tr {
  border-top: 1px solid #ccc;
  background-color: #fff;
}

table tr:nth-child(2n) {
  background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
  max-width: 100%
}
</style>
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
//标题序号计数器
var hCount=[0,0,0,0,0,0];
//设置计数器
function setHCount(number)
{
    //当前计数器加一
    hCount[number-1]++;
    for(var i=number,length=hCount.length;i<length;i++)
    {
        //子目录计数器全部置零
        hCount[i]=0;
    }
}
//重命名目录名称
function setHTagValue(item,number)
{
    //获取标题名
    var text=$(item).get(0).innerHTML;
    //初始化空字符串
    var before="";
    //生成序号
    for(var i=0,length=hCount.length;i<number;i++)
    {
        if(i<number-1)
            before+=hCount[i]+".";
        else
            before+=hCount[i]+" ";
    }
//  $(item).get(0).innerHTML=(h1Count)+"."+(h2Count)+"."+(h3Count)+"."+(h4Count)+"."+(h5Count)+"."+(h6Count)+" "+text;
    //在标题前面加上序号
    $(item).get(0).innerHTML=before+text;
}
function renameHTag(item)
{
    var tag = $(item).get(0).localName;
    if(tag==="h1")
    {
        setHCount(1);
        console.log("捕获到标签:%s",tag);
    }
    if(tag==="h2")
    {
        setHCount(2);
        console.log("捕获到标签:%s",tag);
    }
    if(tag==="h3")
    {
        setHCount(3);
        console.log("捕获到标签:%s",tag);
    }
    if(tag==="h4")
    {
        setHCount(4);
        console.log("捕获到标签:%s",tag);
    }
    if(tag==="h5")
    {
        setHCount(5);
        console.log("捕获到标签:%s",tag);
    }
    if(tag==="h6")
    {
        setHCount(6);
        console.log("捕获到标签:%s",tag);
    }
}
$(document).ready(function()
{
    $("h1,h2,h3,h4,h5,h6").each(function(i,item)
    {
        //给<H>类标签编号
        renameHTag(item);
        //获取标签的名字,h1,还是h2
        var tag = $(item).get(0).localName;

        //为该标签设置id属性
        $(item).attr("id","wow"+i);
        //添加一个页内超链接,并设置class选择器
        $("#category").append('<a class="new'+tag+'" href="#wow'+i+'">'+$(item).text()+'</a></br>');
        //为每一个标题超链接的class属性设置左边距
        $(".newh1").css("margin-left",0);
        $(".newh2").css("margin-left",20);
        $(".newh3").css("margin-left",40);
        $(".newh4").css("margin-left",60);
        $(".newh5").css("margin-left",80);
        $(".newh6").css("margin-left",100);
    });
    //设置class选择器为.book-bodyhtml内容
    $(".book-body").html($(".book-body").nextAll())
});

</script>

<style type="text/css">

@media (max-width: 1600px) {
    .book-body {
       padding-left:200px;
    }
}

@media (max-width: 1400px) {
    .book-body {
       padding-left:200px;
    }
}

@media (max-width: 1200px) {
    .book-body {
       padding-left:300px;
    }
}
@media (max-width: 700px) {
    .book-body {
       padding-left:0px;
    }
}
@media (min-width: 600px) {
    #category{
        position: fixed;
        left: 20px;
        top:0;
        height: 100%;
    }
}

@media (-webkit-max-device-pixel-ratio: 1) {
    ::-webkit-scrollbar-track-piece {
        background-color:#FFF
    }

    ::-webkit-scrollbar {
        width: 6px;
        height: 6px
    }

    ::-webkit-scrollbar-thumb {
        background-color: #c2c2c2;
        background-clip: padding-box;
        min-height: 28px
    }

    ::-webkit-scrollbar-thumb:hover {
        background-color: #A0A0A0
    }
}

</style>
</head>
<body>

<!--返回顶部-->
<a href="javascript:scroll(0,0)" style="position:fixed;float:right;right:32px;top:70%">返回顶部</a> 
<!--目录栏,设置占用宽度为20%可以根据实际情况设置-->
<div style="width:20%;" id="category" class="book-summary"></div>

<h2 id="${事件名称}">${事件名称}</h2>
<h3 id="1-">1.概述</h3>
${事件说明}
<h3 id="2-">2.请求地址</h3>
<table>
<thead>
<tr>
<th>请求方法</th>
<th>协议</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<tr>
<td>post</td>
<td>HTTP/REST</td>
<td><a>http://${ESB服务地址}/api/esb/execute</a></td>
</tr>
<tr>
<td>execute</td>
<td>SOAP/WebService</td>
<td><a>http://${ESB服务地址}/services/ESBServiceV2?wsdl</a></td>
</tr>
</tbody>
</table>
<h3 id="3-">3.请求参数</h3>
<table>
<thead>
<tr>
<th style="text-align:left">名称</th>
<th style="text-align:center">类型</th>
<th style="text-align:center">必须</th>
<th style="text-align:left">描述</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">appkey</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">ESB 分配给产品的 AppKey ,进入 ESB 中心&quot;产品管理-安全设置&quot;可查看该产品的公共参数信息。</td>
</tr>
<tr>
<td style="text-align:left">timestamp</td>
<td style="text-align:center">long</td>
<td style="text-align:center"></td>
<td style="text-align:left">时间戳,精确到毫秒,时区需与 ESB 中心所在服务器的一致,ESB API 允许客户端最大时间误差为 15 分钟</td>
</tr>
<tr>
<td style="text-align:left">username</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">认证用户名,ESB 中心“产品管理-安全设置”中认证开启时必填<br />指定加密方式后,值为加密(username+timestamp)</td>
</tr>
<tr>
<td style="text-align:left">password</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">认证密码,ESB 中心“产品管理-安全设置”中认证开启时必填<br />指定加密方式后,值为加密(password+timestamp)</td>
</tr>
<tr>
<td style="text-align:left">sign</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">API 输入参数签名结果,签名算法参照下面的介绍</td>
</tr>
<tr>
<td style="text-align:left">format</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">响应格式(${数据格式})</td>
</tr>
<tr>
<td style="text-align:left">eventkey</td>
<td style="text-align:center">string</td>
<td style="text-align:center"></td>
<td style="text-align:left">事件标识(${事件标识})</td>
</tr>
<tr>
<td style="text-align:left">params</td>
<td style="text-align:center">json</td>
<td style="text-align:center"></td>
<td style="text-align:left">请求数据</td>
</tr>
${请求参数}
</tbody>
</table>
<h3 id="4-">4.响应参数</h3>
<table>
<thead>
<tr>
<th>名称</th>
<th>类型</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>code</td>
<td>string</td>
<td>请求返回的状态码</td>
</tr>
<tr>
<td>msg</td>
<td>string</td>
<td>请求返回的状态说明</td>
</tr>
<tr>
<td>partialFailure</td>
<td>boolean</td>
<td>是否存在接口执行失败跳过的情况发生(true:存在)</td>
</tr>
<tr>
<td>data</td>
<td>${数据格式}</td>
<td>响应数据</td>
</tr>
${响应参数}
</tbody>
</table>
<h3 id="5-">5.请求示例</h3>
<h4 id="5-1-sdk-">5.1 SDK 请求示例</h4>
<p>外部系统可以通过引入SDK的方式,调用SDK中提供的方法,触发ESB中的事件。<br>注:<br>1.SDK在&quot;ESB中心-业务系统集成ESB导航-业务系统集成&quot;中下载。<br>2.SDK中已对timestamp、sign、format等参数进行了处理,外部系统调用时,只需按照以下示例,传递参数即可。<br>3.requestConfig中设置的参数,可参考第3节中的请求参数说明。<br>4.事件返回结果,可参考第6节中的响应示例。</p>
<pre><code class="lang-java">RequestConfig requestConfig = new RequestConfig();
requestConfig.setAppKey(appKey);             //应用标识
requestConfig.setServerUrl(serverUrl);//ESB 服务所在地址
requestConfig.setSign(Boolean.True);    //启用签名
requestConfig.setSercetKey(sercetKey);//签名密钥
requestConfig.setUserName(userName);    //认证用户名,通过 SDK 调用无需加密,SDK 将根据加密方式自动加密
requestConfig.setPassword(password);    //认证密码,通过 SDK 调用无需加密,SDK 将根据加密方式自动加密
requestConfig.setEncryption(encryption); //加密方式 MD5、SHA1

String eventkey = &quot;${事件标识}&quot;;    //事件标识
String params = &quot;${请求数据}&quot;; //事件请求数据

EsbService service = EsbManager.getService(&quot;http&quot;, requestConfig); //获取 ESB 服务
String response = service.execute(eventkey, params);    //触发 ESB 事件
</code></pre>
<h4 id="5-2-oa-">5.2 OA 请求示例</h4>
<p>OA系统已经集成了ESB相关的SDK,可参考以下方式触发ESB事件。<br>注:<br>1.请求参数,可参考第3节中的请求参数说明。<br>2.事件返回结果,可参考第6节中的响应示例。</p>
<pre><code class="lang-java">String eventKey = &quot;${事件标识}&quot;; //事件标识
String params = &quot;${请求数据}&quot;; //事件请求参数

//EsbService其他方法及说明见ESB API接口说明文档
EsbService service = EsbClient.getService(); //获取 ESB 服务
String result = service.execute(eventkey, params);
</code></pre>
<h4 id="5-3-http-">5.3 HTTP 请求示例</h4>
<p>HTTP请求包含2种方式,使用加密传输传输时,请求参数中需要对用户名、密码进行加密。</p>
<pre><code class="lang-http">#用户名密码非加密传输
POST /api/esb/execute?appkey=应用标识&amp;username=用户名&amp;password=密码&amp;timestamp=时间戳format=${数据格式}&amp;eventkey=${事件标识}&amp;params=${请求数据} HTTP/1.1
Cache-Control: no-cache
Host: ${ESB服务地址}

#用户名密码加密传输
POST /api/esb/execute?appkey=应用标识&amp;username=加密方法(用户名+时间戳)&amp;password=加密方法(密码+时间戳)&amp;timestamp=时间戳format=${数据格式}&amp;eventkey=${事件标识}&amp;params=${请求数据} HTTP/1.1
Cache-Control: no-cache
Host: ${ESB服务地址}
</code></pre>
<h3 id="6-">6. 响应示例</h3>
<pre><code class="lang-${数据格式}">
${响应数据}
</code></pre><h3 id="7-">7. 错误码解释</h3>
<table>
<thead>
<tr>
<th>错误码</th>
<th>错误消息</th>
<th>解决方案</th>
</tr>
</thead>
<tbody>
<tr>
<td>100</td>
<td>执行成功</td>
</tr>
<tr>
<td>101</td>
<td>无有效应用</td>
<td>查看对应事件是否已发布应用</td>
</tr>
<tr>
<td>102</td>
<td>无同步应用</td>
<td>查看对应事件已发布的应用是否有同步类别的应用</td>
</tr>
<tr>
<td>201</td>
<td>无效的Appkey</td>
<td>检查 appkey与 ESB 中心-产品管理-安全设置中的 appkey 是否一致</td>
</tr>
<tr>
<td>202</td>
<td>请求超时</td>
<td>检查传递的时间戳是否精确到毫秒<br />检查调用方的服务器时间与 ESB 中心的服务时间是否一致</td>
</tr>
<tr>
<td>203</td>
<td>签名错误</td>
<td>根据文档中的签名算法进行签名计算<br />关闭 ESB 中心-产品管理-安全设置中的签名校验开关</td>
</tr>
<tr>
<td>204</td>
<td>IP 地址不在可访问名单</td>
<td>检查调用方的 IP 是否在ESB 中心-产品管理-安全设置-IP 白名单中<br />检查调用方的 IP 是否在ESB 中心-产品管理-安全设置-IP 黑名单中<br />关闭ESB 中心-产品管理-安全设置中的IP 限制开关</td>
</tr>
<tr>
<td>205</td>
<td>用户名、密码不正确</td>
<td>检查调用方传递的用户名、密码与 ESB 中心-产品管理-安全设置中的用户名、密码是否一致<br />关闭 ESB 中心-产品管理-安全设置中的认证开关</td>
</tr>
<tr>
<td>301</td>
<td>系统错误</td>
<td>检查系统日志</td>
</tr>
<tr>
<td>302</td>
<td>无有效事件</td>
<td>检查调用方传递的事件标识是否存在,且状态是否正确</td>
</tr>
<tr>
<td>303</td>
<td>执行失败</td>
<td>检查系统日志,查看应用对应接口是否可访问<br />查看对应接口配置的成功标识是否正确</td>
</tr>
<tr>
<td>304</td>
<td>模块标识格式错误</td>
<td>标识只能由字符、数字、下划线组成</td>
</tr>
<tr>
<td>305</td>
<td>事件标识格式错误</td>
<td>标识只能由字符、数字、下划线组成</td>
</tr>
<tr>
<td>306</td>
<td>模块标识已存在</td>
<td>更换模块标识(新建编辑事件时)</td>
</tr>
<tr>
<td>307</td>
<td>事件标识已存在</td>
<td>更换事件标识(新建编辑事件时)</td>
</tr>
<tr>
<td>308</td>
<td>模块所属产品AppKey不一致</td>
<td>更换模板标识</td>
</tr>
<tr>
<td>309</td>
<td>参数格式不正确</td>
<td>检查事件发送的数据是否正确<br />检查事件发送的数据,与 ESB 中心事件定义的数据格式是否一致</td>
</tr>
<tr>
<td>404</td>
<td>未找到对应数据</td>
<td>检查请求数据</td>
</tr>
<tr>
</tr>
</tbody>
</table>
<h3 id="8-">8. 签名算法</h3>
<p>为了防止API调用过程中被黑客恶意篡改,可在 ESB 中心&quot;产品管理-安全设置&quot;中开启签名,开启签名后调用任何 API需要携带签名,ESB 中心根据请求参数,对签名进行验证,签名不合法的请求将会被拒绝。</p>
<p>签名大体过程如下:</p>
<ol>
<li><p>对所有API(除去sign参数),根据参数名称的ASCII码表的顺序排序。如:</p>
<p><code>foo:1, bar:2, foo_bar:3, foobar:4</code></p>
<p>排序后的顺序是:</p>
<p><code>bar:2, foo:1, foo_bar:3, foobar:4</code></p>
</li><li><p>将排序好的参数名和参数值拼装在一起,根据上面的示例得到的结果为:</p>
<p><code>bar2foo1foo_bar3foobar4</code></p>
</li><li><p>用app的secret初始化HMAC_MD5算法后,再进行摘要,如:</p>
<pre><code class="lang-java">SecretKey secretKey = new SecretKeySpec(secret.getBytes(), &quot;HmacMD5&quot;);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
bytes = mac.doFinal(“bar2foo1foo_bar3foobar4”.getBytes());
</code></pre>
</li><li><p>将摘要得到的字节流结果使用十六进制表示,如:</p>
<p><code>hex(“helloworld”.getBytes(“utf-8”)) = “68656C6C6F776F726C64”</code></p>
</li></ol>
<h4 id="8-1-java-">8.1 JAVA 签名算法示例</h4>
<pre><code class="lang-java">public static String sign( Map&lt;String,String&gt; params, String secret){

    // 第一步:检查参数是否已经排序
    String[] keys = params.keySet().toArray(new String[0]);
    Arrays.sort(keys);

    // 第二步:把所有参数名和参数值串在一起
    StringBuilder query = new StringBuilder();

    for (String key : keys) {
        if(key != null &amp;&amp; !key.isEmpty()) {
            String value = params.get(key);
            if (value != null &amp;&amp; !value.isEmpty()) {
                query.append(key).append(value);
            }
        }
    }
    // 第三步:使用HMAC加密
    byte[] bytes = encryptHMAC(query.toString(), secret);

    // 第四步:把二进制转化为大写的十六进制(正确签名应该为32大写字符串,此方法需要时使用)
    return byte2hex(bytes);
}

/**
 * 使用HMAC加密
 * @param data
 * @param secret
 * @return
 */
private static byte[] encryptHMAC(String data, String secret) {
    byte[] bytes = null;
    try {
        SecretKey secretKey = new SecretKeySpec(secret.getBytes(), &quot;HmacMD5&quot;);
        Mac mac = Mac.getInstance(secretKey.getAlgorithm());
        mac.init(secretKey);
        bytes = mac.doFinal(data.getBytes());
    } catch (Exception e) {
    }
    return bytes;
}

/**
 * 二进制转化为大写的十六进制
 * @param bytes
 * @return
 */
private static String byte2hex(byte[] bytes) {
    StringBuilder sign = new StringBuilder();
    for (int i = 0; i &lt; bytes.length; i++) {
        String hex = Integer.toHexString(bytes[i] &amp; 0xFF);
        if (hex.length() == 1) {
            sign.append(&quot;0&quot;);
        }
        sign.append(hex.toUpperCase());
    }
    return sign.toString();
}
</code></pre>

</body>
</html>
<!-- This document was created with MarkdownPad, the Markdown editor for Windows (http://markdownpad.com) -->