繁体   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