下面是OSS java sdk生成签名串的反编译源码,似乎其生成规则与API中描述不同(主要指数据项顺序),请指教
import com.aliyun.common.comm.RequestMessage;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
public class SignUtils
{
private static final String NEW_LINE = "\n";
private static final List<String> SIGNED_PARAMTERS = Arrays.asList(new String[] { "acl", "uploadId", "partNumber", "uploads", "location", "cors", "logging", "website", "response-cache-control", "response-content-disposition", "response-content-encoding", "response-content-language", "response-content-type", "response-expires" });
public static String buildCanonicalString(String method, String resourcePath, RequestMessage request, String expires)
{
StringBuilder builder = new StringBuilder();
builder.append(new StringBuilder().append(method).append("\n").toString());
Map headers = request.getHeaders();
TreeMap headersToSign = new TreeMap();
if (headers != null) {
for (Map.Entry header : headers.entrySet()) {
if (header.getKey() != null)
{
String lowerKey = ((String)header.getKey()).toLowerCase();
if ((lowerKey.equals("Content-Type".toLowerCase())) || (lowerKey.equals("Content-MD5".toLowerCase())) || (lowerKey.equals("Date".toLowerCase())) || (lowerKey.startsWith("x-oss-")))
{
headersToSign.put(lowerKey, header.getValue());
}
}
}
}
if (!headersToSign.containsKey("Content-Type".toLowerCase())) {
headersToSign.put("Content-Type".toLowerCase(), "");
}
if (!headersToSign.containsKey("Content-MD5".toLowerCase())) {
headersToSign.put("Content-MD5".toLowerCase(), "");
}
if (request.getParameters() != null) {
for (Map.Entry p : request.getParameters().entrySet()) {
if (((String)p.getKey()).startsWith("x-oss-")) {
headersToSign.put(p.getKey(), p.getValue());
}
}
}
for (Map.Entry entry : headersToSign.entrySet()) {
String key = (String)entry.getKey();
Object value = entry.getValue();
if (key.startsWith("x-oss-"))
builder.append(key).append(':').append(value);
else {
builder.append(value);
}
builder.append("\n");
}
builder.append(buildCanonicalizedResource(resourcePath, request.getParameters()));
return builder.toString();
}
private static String buildCanonicalizedResource(String resourcePath, Map<String, String> parameters) {
assert (resourcePath.startsWith("/"));
StringBuilder builder = new StringBuilder();
builder.append(resourcePath);
if (parameters != null) {
String[] parameterNames = (String[])parameters.keySet().toArray(new String[parameters.size()]);
Arrays.sort(parameterNames);
char separater = '?';
for (String paramName : parameterNames) {
if (SIGNED_PARAMTERS.contains(paramName))
{
builder.append(separater);
builder.append(paramName);
String paramValue = (String)parameters.get(paramName);
if (paramValue != null) {
builder.append("=").append(paramValue);
}
separater = '&';
}
}
}
return builder.toString();
}
}
-------------------------
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。