免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
怎樣創(chuàng)建安全的Rest WebService API 請(qǐng)求
API Key

當(dāng)用戶注冊(cè)之后,會(huì)給用戶一個(gè)API Key。這個(gè)API Key會(huì)附在每個(gè)請(qǐng)求的url后面,這個(gè)方式的缺點(diǎn)是如果某個(gè)人知道你的API Key之后,他就能偽裝成那個(gè)用戶了。 但是如果你的API請(qǐng)求是使用HTTPS(SSL)的方式,就可以避免你的API Key被別人獲取了。


API Key + Secret Key簽名

比上一種跟復(fù)雜的一種方式就是用一個(gè)secret key去簽名每一刻API URL請(qǐng)求,Amazon Web Services就是使用這種方式。當(dāng)用戶注冊(cè)完后, 你會(huì)給用戶2個(gè)keys:API Key(比如:username)和secret key(比如password),API key會(huì)附在每個(gè)請(qǐng)求url上面,但是secret key
不會(huì)。secret key是用來簽名每個(gè)請(qǐng)求的。通常會(huì)加上另外一個(gè)參數(shù)比如(Signature)。

Amazon會(huì)把所有的請(qǐng)求信息作為請(qǐng)求的參數(shù),然后按照參數(shù)名排序,再按照secret key進(jìn)行hash。這個(gè)hash的值會(huì)作為一個(gè)新的參數(shù)(Signature)附加到請(qǐng)求的url上。在server端做相同的事情,獲得所有的參數(shù)(除了Signature),排序,用sercet key hash,如果這個(gè)值跟傳過來的Signature參數(shù)的值相等的話,則認(rèn)為這個(gè)請(qǐng)求是合法的。



下面的是引用Amazon Web Services文檔中的內(nèi)容。

For example, the following is a Query string (linebreaks added for clarity).

引用
Action=DescribeImages
&AWSAccessKeyId=10QMXFEV71ZS32XQFTR2
&SignatureVersion=1
&Timestamp=2006-12-08T07%3A48%3A03Z
&Version=2007-01-03


For the preceding Query string, you would calculate the HMAC signature over the following string.
引用
(linebreaks added for clarity)
      ActionDescribeImages
      AWSAccessKeyId10QMXFEV71ZS32XQFTR2
      SignatureVersion1
      Timestamp2006-12-08T07:48:03Z
      Version2007-01-03


Using the preceding string and the secret key DMADSSfPfdaDjbK+RRUhS/aDrjsiZadgAUm8gRU2 the
base64 encoded signature is as follows:
GjH3941IBe6qsgQu+k7FpCJjpnc=

The following is a Java code sample to compute the signature from the string and the private key.

引用
import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
public class HmacExample
{
    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
/**
* Computes RFC 2104-compliant HMAC signature.
     *
     * @param data
     *     The data to be signed.
     * @param key
     *     The signing key.
     * @return
     *     The base64-encoded RFC 2104-compliant HMAC signature.
     * @throws
     *     java.security.SignatureException when signature generation fails
     */
    public static String calculateRFC2104HMAC(String data, String key)
          throws java.security.SignatureException
    {
       String result;
       
        try {
            // get an hmac_sha1 key from the raw key bytes
            SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(),
             HMAC_SHA1_ALGORITHM);
            // get an hmac_sha1 Mac instance
            // and initialize with the signing key
            Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            mac.init(signingKey);
            // compute the hmac on input data bytes
            byte[] rawHmac = mac.doFinal(data.getBytes());
            // base64-encode the hmac
            result = Base64.encodeBytes(rawHmac);
        }
        catch (Exception e) {
            throw new SignatureException("Failed to generate HMAC : "
                + e.getMessage());
        }
       
     return result;
     }
       
}
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
HMAC-SHA1 Signatures
騰訊云對(duì)象存儲(chǔ)(COS)服務(wù)的 API
[收藏]REST WebService與SOAP WebService的比較
java微信開發(fā)API第一步 服務(wù)器接入
OAuth協(xié)議中的HMAC_SHA1算法
基于Webrtc服務(wù)器搭建音視頻通話的詳細(xì)步驟(附源碼下載)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服