简体   繁体   English

如何使用Java和Eclipse签署针对Amazon API的REST请求?

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

Here is my dilemma. 这是我的困境。 I'm familiar with reading Java code, but no good at writing it. 我熟悉阅读Java代码,但是不擅长编写Java代码。 I have several examples from Amazon documentation, but I'm doing something wrong. 我有Amazon文档中的几个示例,但是我做错了。 I'm using Eclipse. 我正在使用Eclipse。 I have the AWS Java SDK, Apache Commons Codec & Logging, and Base64. 我有AWS Java SDK,Apache Commons编解码器和日志记录以及Base64。 I have all the code in the proper classpaths inside my project. 我在项目内部的正确类路径中拥有所有代码。

I understand how to form the request (the order of elements, timestamp, etc.), but I don't know how to send this info to the java code to create the signed request. 我了解如何构成请求(元素的顺序,时间戳等),但是我不知道如何将此信息发送到Java代码以创建签名的请求。 So I'll start with the code I am using from the documentation. 因此,我将从文档中使用的代码开始。

Code for Signature: 签名代码:

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;
        }
}

Code for Encoding: 编码代码:

/**
* 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);
    }
}

Code I came up with to sign request: 我想出的代码来签署请求:

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);
    }
}

At first, I was getting errors related to classpath, so I added those to my project. 起初,我遇到了与类路径有关的错误,因此我将这些错误添加到了我的项目中。 Now when I run the code, I get no errors and no response, I just return to a command prompt. 现在,当我运行代码时,没有错误也没有响应,我只是返回到命令提示符。 I know this is probably a simple solution. 我知道这可能是一个简单的解决方案。 I've spent a week trying to figure this out and have had no success finding any answers. 我花了一个星期的时间试图弄清楚这一点,但没有成功找到任何答案。 Can someone point me in the right direction? 有人可以指出我正确的方向吗?

Note: I didn't include the Base64 code here because of the size, but I do have that in my project. 注意:由于大小原因,我没有在此处包括Base64代码,但是我确实在项目中包含了该代码。

The line in my code: 我的代码行:

Signature.calculateRFC2104HMAC(data, key);

is missing a print statement to display the results. 缺少显示结果的打印语句。 Changing it to 更改为

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

gives me the result I was expecting. 给我我期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Amazon中托管REST API? - How do I host a REST API in Amazon? 如何使用REST请求进行gnip查询(Java) - How do I make a gnip query using a REST request (Java) 使用Rally Java Rest API,如何获取DirectChildrenCount? - Using the Rally Java Rest API, how do I get DirectChildrenCount? 如何使用Java / Android与REST API通信? - How do I communicate with a REST API using Java/Android? 如何使用Java从使用Glassfish和REST协议设置的Amazon AWS服务器中检索文件? - How do I retrieve a file using Java from an Amazon AWS server that I set up using Glassfish and the REST protocol? 如何限制 GET 请求检索到的数组元素的数量(REST API JAVA) - How do i limit the number of array elements retrieved by a GET request (REST API JAVA) HBase 读取:为了提高性能,如何使用 hbase java REST api 对 get 请求进行批处理 - HBase read : To improve on performance , how to do batch processing of get request using hbase java REST api 如何在 Z03D476861AFD3841110F2CB80CCFA8 中请求 Rest API eclipse 服务? - How to request Rest API eclipse service in postman? 如何通过Java测试亚马逊MWS API? - How do I test Amazon MWS API via Java? 在 java 中使用 printf 时如何放置美元符号? - How do I put a dollar sign when using printf in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM