Spring Boot与AWS IoT API的连接出现403禁止错误。

huangapple 未分类评论47阅读模式
英文:

Spring Boot with AWS IoT api's connection getting error 403 forbidden

问题

以下是使用AWS文档示例创建签名的部分代码:

static byte[] HmacSHA256(String data, byte[] key) throws Exception {
    String algorithm = "HmacSHA256";
    Mac mac = Mac.getInstance(algorithm);
    mac.init(new SecretKeySpec(key, algorithm));
    return mac.doFinal(data.getBytes("UTF-8"));
}

static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
    byte[] kSecret = ("AWS4" + key).getBytes("UTF-8");
    byte[] kDate = HmacSHA256(dateStamp, kSecret);
    byte[] kRegion = HmacSHA256(regionName, kDate);
    byte[] kService = HmacSHA256(serviceName, kRegion);
    byte[] kSigning = HmacSHA256("aws4_request", kService);
    return kSigning;
}

String url = "https://iot.us-east-1.amazonaws.com/things?thingTypeName={thingTypeName}";

@RequestMapping(value = "/things", produces = MediaType.APPLICATION_JSON_VALUE)
public void getThingLists() throws Exception {
    SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
    String date = f.format(new Date());
    byte[] signature1 = getSignatureKey(secretKey, date, regionName, serviceName);
    String signature = Hex.encodeHexString(signature1);
    //String s = Base64.getEncoder().encodeToString(signature1);
    System.out.println("Signature: " + signature);
    String str = "Signature=" + signature;
    str = "AWS4-HMAC-SHA256 Credential=YOUR_ACCESS_KEY_ID/20200724/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date," + str;
    System.out.println(str);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    headers.add("Content-Type", MediaType.APPLICATION_JSON_VALUE);
    headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
    headers.add("x-amz-date", date);
    headers.add("Authorization", str);
    HttpEntity<String> entity = new HttpEntity<String>(headers);
    // Send request with GET method, and Headers.
    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, "SmartLed001");
    //return restTemplate.exchange(url, HttpMethod.GET, entity, String.class, "SmartLed001").getBody();
    String result = response.getBody();
    System.out.println(result);
}

出现的错误是:

org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:83)

你如何解决这个问题?

英文:

I am creating signature using AWS doc sample:

static byte[] HmacSHA256(String data, byte[] key) throws Exception {
	    String algorithm=&quot;HmacSHA256&quot;;
	    Mac mac = Mac.getInstance(algorithm);
	    mac.init(new SecretKeySpec(key, algorithm));
	    return mac.doFinal(data.getBytes(&quot;UTF-8&quot;));
	}

static byte[] getSignatureKey(String key, String dateStamp, String regionName, String serviceName) throws Exception {
	    byte[] kSecret = (&quot;AWS4&quot; + key).getBytes(&quot;UTF-8&quot;);
	    byte[] kDate = HmacSHA256(dateStamp, kSecret);
	    byte[] kRegion = HmacSHA256(regionName, kDate);
	    byte[] kService = HmacSHA256(serviceName, kRegion);
	    byte[] kSigning = HmacSHA256(&quot;aws4_request&quot;, kService);
	    return kSigning;
	}


String url = &quot;https://iot.us-east-1.amazonaws.com/things?thingTypeName={thingTypeName}&quot;;

	@RequestMapping(value = &quot;/things&quot;,produces=MediaType.APPLICATION_JSON_VALUE)
	public void getThingLists() throws Exception {
		 SimpleDateFormat f = new SimpleDateFormat(&quot;yyyyMMdd&#39;T&#39;HHmmss&#39;Z&#39;&quot;);
		 String date=f.format(new Date());
		    byte[] signature1 = getSignatureKey(secretKey, date, regionName, serviceName);
		    String signature=Hex.encodeHexString(signature1);
		    //String s = Base64.getEncoder().encodeToString(signature1);
		    System.out.println(&quot;Signature : &quot; + signature);
		    String str=&quot;Signature=&quot;+signature ;
			str=&quot;AWS4-HMAC-SHA256 Credential=AKIA4XDJYZKLHCNVHNBA/20200724/us-east-1/execute-api/aws4_request, SignedHeaders=host;x-amz-date,&quot;+str ;
			System.out.println(str);
		HttpHeaders headers = new HttpHeaders();
		headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
		headers.add(&quot;Content-Type&quot;, MediaType.APPLICATION_JSON_VALUE);
		headers.add(&quot;user-agent&quot;, &quot;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36&quot;);
		headers.add(&quot;x-amz-date&quot;, date);
		headers.add(&quot;Authorization&quot;,str);
		HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(headers);
        // Send request with GET method, and Headers.
        ResponseEntity&lt;String&gt; response = restTemplate.exchange(url,
                HttpMethod.GET, entity, String.class,&quot;SmartLed001&quot;);
		//return restTemplate.exchange(url, HttpMethod.GET, entity, String.class,&quot;SmartLed001&quot;).getBody();
		 String result = response.getBody();
	        System.out.println(result);
	}

The error I am getting is:

org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden
	at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:83)

How can I solve this?

huangapple
  • 本文由 发表于 2020年7月23日 16:05:51
  • 转载请务必保留本文链接:https://java.coder-hub.com/63049649.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定