簡體   English   中英

如何使用Java和Eclipse簽署針對Amazon API的REST請求?

[英]How do I sign a REST request for Amazon API using Java and Eclipse?

這是我的困境。 我熟悉閱讀Java代碼,但是不擅長編寫Java代碼。 我有Amazon文檔中的幾個示例,但是我做錯了。 我正在使用Eclipse。 我有AWS Java SDK,Apache Commons編解碼器和日志記錄以及Base64。 我在項目內部的正確類路徑中擁有所有代碼。

我了解如何構成請求(元素的順序,時間戳等),但是我不知道如何將此信息發送到Java代碼以創建簽名的請求。 因此,我將從文檔中使用的代碼開始。

簽名代碼:

import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

    /**
    * This class defines common routines for generating
    * authentication signatures for AWS requests.
    */
    public class Signature {
        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 = Encoding.EncodeBase64(rawHmac);

            } catch (Exception e) {
                throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
            }
            return result;
        }
}

編碼代碼:

/**
* This class defines common routines for encoding * data in AWS requests.
*/
public class Encoding {
    /**
    * Performs base64-encoding of input bytes.
    *
    * @param rawData * Array of bytes to be encoded.
    * @return * The base64 encoded string representation of rawData.
    */
    public static String EncodeBase64(byte[] rawData) {
        return Base64.encodeBytes(rawData);
    }
}

我想出的代碼來簽署請求:

import java.security.SignatureException;

public class SignatureTest {
    /**
     * @param args
     * @throws SignatureException
     */
    public static void main( String[] args ) throws SignatureException {
        // data is the URL parameters and time stamp to be encoded
        String data = "<data-to-encode>";
        // key is the secret key
        String key = "<my-secret-key>";
        Signature.calculateRFC2104HMAC(data, key);
    }
}

起初,我遇到了與類路徑有關的錯誤,因此我將這些錯誤添加到了我的項目中。 現在,當我運行代碼時,沒有錯誤也沒有響應,我只是返回到命令提示符。 我知道這可能是一個簡單的解決方案。 我花了一個星期的時間試圖弄清楚這一點,但沒有成功找到任何答案。 有人可以指出我正確的方向嗎?

注意:由於大小原因,我沒有在此處包括Base64代碼,但是我確實在項目中包含了該代碼。

我的代碼行:

Signature.calculateRFC2104HMAC(data, key);

缺少顯示結果的打印語句。 更改為

System.out.println(Signature.calculateRFC2104HMAC(data, key));

給我我期望的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM