AWSV4签名认证(Java实现)

更新时间:2023-05-25 15:31:32 阅读: 评论:0

URLEncoder;
import java.curity.MessageDigest;
import java.curity.NoSuchAlgorithmException;
DateFormat;
SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
import java.util.TreeMap;
pto.Mac;英语 口语
pto.spec.SecretKeySpec;
/**
* AWS V4 签名处理⼯具
*
* 参考链接:docs./zh_cn/general/latest/gr/sigv4_signing.html  */
public class AWSV4Auth {
private AWSV4Auth() {
}
public static class Builder {
private String accessKeyID;
private String cretAccessKey;
private String regionName;
private String rviceName;
private String httpMethodName;
private String canonicalURI;
private TreeMap<String, String> queryParametes;
private TreeMap<String, String> awsHeaders;
private String payload;
private boolean debug = fal;drift
public Builder(String accessKeyID, String cretAccessKey) {
this.accessKeyID = accessKeyID;
}
public Builder regionName(String regionName) {
return this;
}
public Builder rviceName(String rviceName) {
this.rviceName = rviceName;
return this;
}
public Builder httpMethodName(String httpMethodName) {
this.httpMethodName = httpMethodName;
return this;
}
public Builder canonicalURI(String canonicalURI) {
this.canonicalURI = canonicalURI;
return this;
}
public Builder queryParametes(TreeMap<String, String> queryParametes) {
this.queryParametes = queryParametes;
return this;
}
public Builder awsHeaders(TreeMap<String, String> awsHeaders) {剑桥国际少儿英语
this.awsHeaders = awsHeaders;
return this;
}
public Builder payload(String payload) {
this.payload = payload;
return this;
return this;
}
public Builder debug() {
this.debug = true;
return this;
大学生日本留学
}
public AWSV4Auth build() {
return new AWSV4Auth(this);
}
}
private String accessKeyID;
private String cretAccessKey;bill kaulitz
private String regionName;
private String rviceName;
private String httpMethodName;
private String canonicalURI;
private TreeMap<String, String> queryParametes;
private TreeMap<String, String> awsHeaders;
private String payload;
private boolean debug = fal;
/* Other variables */
private final String HMACAlgorithm = "AWS4-HMAC-SHA256";
private final String aws4Request = "aws4_request";
private String strSignedHeader;
private String xAmzDate;
private String currentDate;
private AWSV4Auth(Builder builder) {
accessKeyID = builder.accessKeyID;
cretAccessKey = AccessKey;
regionName = ionName;
rviceName = builder.rviceName;
httpMethodName = builder.httpMethodName;
canonicalURI = builder.canonicalURI;
queryParametes = builder.queryParametes;
awsHeaders = builder.awsHeaders;
payload = builder.payload;
debug = builder.debug;
/* Get current timestamp value.(UTC) */
xAmzDate = getTimeStamp();
currentDate = getDate();
}
/**
俄语
* 任务 1:针对签名版本 4 创建规范请求
earn
*
* @return
*/
private String prepareCanonicalRequest() {
StringBuilder canonicalURL = new StringBuilder("");
/* Step 1.1 以HTTP⽅法(GET, PUT, POST, etc.)开头, 然后换⾏. */
canonicalURL.append(httpMethodName).append("\n");
/* Step 1.2 添加URI参数,换⾏. */
canonicalURI = canonicalURI == null || im().isEmpty() ? "/" : canonicalURI;        canonicalURL.append(canonicalURI).append("\n");
/* Step 1.3 添加查询参数,换⾏. */
StringBuilder queryString = new StringBuilder("");
if (queryParametes != null && !queryParametes.isEmpty()) {
for (Map.Entry<String, String> entrySet : Set()) {
String key = Key();
String value = Value();
queryString.append(key).append("=").append(encodeParameter(value)).append("&");            }
queryString.deleteCharAt(queryString.lastIndexOf("&"));
queryString.append("\n");
} el {
queryString.append("\n");
}
canonicalURL.append(queryString);
/* Step 1.4 添加headers, 每个header都需要换⾏. */
StringBuilder signedHeaders = new StringBuilder("");
if (awsHeaders != null && !awsHeaders.isEmpty()) {
for (Map.Entry<String, String> entrySet : Set()) {
everyoneString key = Key();
String value = Value();
signedHeaders.append(key).append(";");
孤独的小猪canonicalURL.append(key).append(":").append(value).append("\n");
}
canonicalURL.append("\n");
} el {
canonicalURL.append("\n");
}
/* Step 1.5 添加签名的headers并换⾏. */
strSignedHeader = signedHeaders.substring(0, signedHeaders.length() - 1); // 删掉最后的 ";"        canonicalURL.append(strSignedHeader).append("\n");
/* Step 1.6 对HTTP或HTTPS的body进⾏SHA256处理. */
payload = payload == null ? "" : payload;
canonicalURL.append(generateHex(payload));
if (debug) {
System.out.println("##Canonical Request:\n" + String());
}
String();
}
/**
* 任务 2:创建签名版本 4 的待签字符串
*
* @param canonicalURL
* @return
*/
private String prepareStringToSign(String canonicalURL) {
String stringToSign = "";
/* Step 2.1 以算法名称开头,并换⾏. */
stringToSign = HMACAlgorithm + "\n";
/* Step 2.2 添加⽇期,并换⾏. */
stringToSign += xAmzDate + "\n";
/* Step 2.3 添加认证范围,并换⾏. */
stringToSign += currentDate + "/" + regionName + "/" + rviceName + "/" + aws4Request + "\n";        /* Step 2.4 添加任务1返回的规范URL哈希处理结果,然后换⾏. */
stringToSign += generateHex(canonicalURL);
if (debug) {
System.out.println("##String to sign:\n" + stringToSign);
}
return stringToSign;
}
/**
* 任务 3:为 AWS Signature 版本 4 计算签名
*
* @param stringToSign
* @return
*/
sp是什么意思private String calculateSignature(String stringToSign) {
try {
/* Step 3.1 ⽣成签名的key */

本文发布于:2023-05-25 15:31:32,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/122269.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:添加   并换   签名   创建   规范   版本   留学   链接
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图