403 Forbidden: “Access Denied”你没有权限访问

huangapple go评论113阅读模式
英文:

403 Forbidden: "<HTML><HEAD><EOL><TITLE>Access Denied</TITLE><EOL></HEAD><BODY><EOL><H1>You don't have permission to access

问题

我正在使用Spring和Rest Template来访问和获取JSON数据。

这是我的代码:

@RequestMapping(value = "/api/v1/forecast", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<ReturnMessage> getForecast(@RequestParam Map<String, Object> params, ModelMap model) {

    final Logger LOGGER = LoggerFactory.getLogger(ForecastController.class);

    ReturnMessage message = new ReturnMessage();
    RestTemplate restTemplate = new RestTemplate();    

    String url = "https://api.golde.gov";

    System.out.println("loggg=======");
    LOGGER.info("link part " + url);        

    HttpEntity<String> entity = new HttpEntity<String>(jsonObject.toString(), headers);
    String result = restTemplate.postForObject(url, entity, String.class);

    System.out.println(result);

    return ResponseEntity.ok(message);
}

我在请求中包含了一个"User-Agent"头部,但当我发送请求到weather.gov时,收到了一个异常错误:

org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: "<HTML><HEAD><EOL><TITLE>Access Denied</TITLE><EOL></HEAD><BODY><EOL><H1>Access Denied</H1><EOL> <EOL>You don't have permission to access "http&#58;&#47;&#47;api&#46;weather&#46;gov&#47;gridpoints&#47;OKX&#47;33&#44;35&#47;forecast" on this server.<P><EOL>Reference&#32;&#35;18&#46;177c2117&#46;1685609346&#46;4b1d971<EOL></BODY><EOL></HTML><EOL>"
    at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) ~[spring-web-6.0.9.jar:6.0.9]
    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:481) ~[spring-web-6.0.9.jar:6.0.9]
    at com.forecast.veriserve.controller.ForecastController.getForecast(ForecastController.java:105) ~[classes/:na]

如何解决这个问题?非常感谢。

英文:

I'm using Spring and Rest Template to access and get data json

this is my code:

@RequestMapping(value = &quot;/api/v1/forecast&quot;, method = RequestMethod.GET)
@ResponseBody
public ResponseEntity&lt;ReturnMessage&gt; getForecast(@RequestParam Map&lt;String, Object&gt; params, ModelMap model) {	
	
    final Logger LOGGER = LoggerFactory.getLogger(ForecastController.class);
	
	ReturnMessage message = new ReturnMessage();
	RestTemplate restTemplate = new RestTemplate();		
	
	String url = &quot;https://api.golde.gov&quot;;
									
	System.out.println(&quot;loggg=======&quot;);
	LOGGER.info(&quot;link part &quot; + url);		
    		
	HttpEntity&lt;String&gt; entity = new HttpEntity&lt;String&gt;(jsonObject.toString(), headers);
	String result = restTemplate.postForObject(url, entity, String.class);
	
	System.out.println(result);
	
	return ResponseEntity.ok(message);
}

I have included a User-Agent header in my request but when i send to request to weather.gov to response an exception error:

org.springframework.web.client.HttpClientErrorException$Forbidden: 403 Forbidden: &quot;&lt;HTML&gt;&lt;HEAD&gt;&lt;EOL&gt;&lt;TITLE&gt;Access Denied&lt;/TITLE&gt;&lt;EOL&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;EOL&gt;&lt;H1&gt;Access Denied&lt;/H1&gt;&lt;EOL&gt; &lt;EOL&gt;You don&#39;t have permission to access &quot;http&amp;#58;&amp;#47;&amp;#47;api&amp;#46;weather&amp;#46;gov&amp;#47;gridpoints&amp;#47;OKX&amp;#47;33&amp;#44;35&amp;#47;forecast&quot; on this server.&lt;P&gt;&lt;EOL&gt;Reference&amp;#32;&amp;#35;18&amp;#46;177c2117&amp;#46;1685609346&amp;#46;4b1d971&lt;EOL&gt;&lt;/BODY&gt;&lt;EOL&gt;&lt;/HTML&gt;&lt;EOL&gt;&quot;
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:109) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:915) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:864) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:764) ~[spring-web-6.0.9.jar:6.0.9]
at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:481) ~[spring-web-6.0.9.jar:6.0.9]
at com.forecast.veriserve.controller.ForecastController.getForecast(ForecastController.java:105) ~[classes/:na]

How to fix the problem ? many thank

答案1

得分: 1

以下是您要翻译的内容:

One problem could be:

User-Agent: (myweatherapp.com, contact@myweatherapp.com)

..when reading the "Pricing" and "Authentication" sections.

I could *reproduce the issue immediately* with HTTP POST! (-> 403), so don't use that (unless API changes).

Here is the working mvc version with comments:

// import java.util., org.spr..fr..
@RestController
static class WeatherController {
@Autowired
RestTemplateBuilder rtplB;

@GetMapping("/forecast")
public Map getForecast() {
	// customize your api call(+response) from here:
	HttpHeaders headers = new HttpHeaders();
	headers.put("User-Agent",
			List.of(UUID.randomUUID().toString(), // <- until API changes
					"contact@my.funky.website.com")); // <- *your* contact, if you like
	headers.setAccept(List.of(MediaType.valueOf("application/geo+json")));
	HttpEntity<Void> ent = new HttpEntity<>(headers);
	return rtplB
			.build()
			.exchange(
					"https://api.weather.gov/gridpoints/OKX/33,35/forecast", // customize, parametrize, call dynamically
					HttpMethod.GET, // <- POST works not well (->403), didn't try the others..
					ent, // <- request headers (+ body)
					Map.class // response type
					/* , uriVars/... */
			).getBody(); // <- returning ResponseEntity directly gave "strange" issues, "referencing" the body performs better (in browser).
}

}


Here the webflux version:
```lang-java
@Autowired
WebClient.Builder wcB;

@GetMapping("/forecast")
public Mono<Map> getForecast(/*...*/) {
	return wcB.build()
			.get()
			.uri("https://api.weather.gov/gridpoints/OKX/33,35/forecast")
			.headers(
			    h -> {
                    h.put("User-Agent", List.of(UUID.randomUUID().toString(), "contact@my.funky.website.com"));
                    h.setAccept(List.of(MediaType.valueOf("application/geo+json")));
                }
            )
			.retrieve().bodyToMono(Map.class);
}

BTW

I had the "strange issues" (browser loading behavior) only on the mvc version, when directly returning resttemplate response, a "hold of the body" improves/fixes this. And a minimalist version:

return rtplB.build()
 .getForObject(
   "https://api.weather.gov/gridpoints/OKX/33,35/forecast",
   Map.class
 ); // #

... "seems to work just fine".


<details>
<summary>英文:</summary>

One problem could be:

User-Agent: (myweatherapp.com, contact@myweatherapp.com)

..when reading the &quot;Pricing&quot; and &quot;Authentication&quot; sections.

I could *reproduce the issue immediately* with HTTP POST! (-&gt; 403), so don&#39;t use that (unless API changes).

Here is the working mvc version with comments:

// import java.util., org.spr..fr..
@RestController
static class WeatherController {
@Autowired
RestTemplateBuilder rtplB;

@GetMapping(&quot;/forecast&quot;)
public Map getForecast() {
	// customize your api call(+response) from here:
	HttpHeaders headers = new HttpHeaders();
	headers.put(&quot;User-Agent&quot;,
			List.of(UUID.randomUUID().toString(), // &lt;- until API changes
					&quot;contact@my.funky.website.com&quot;)); // &lt;- *your* contact, if you like
	headers.setAccept(List.of(MediaType.valueOf(&quot;application/geo+json&quot;)));
	HttpEntity&lt;Void&gt; ent = new HttpEntity&lt;&gt;(headers);
	return rtplB
			.build()
			.exchange(
					&quot;https://api.weather.gov/gridpoints/OKX/33,35/forecast&quot;, // customize, parametrize, call dynamically
					HttpMethod.GET, // &lt;- POST works not well (-&gt;403), didn&#39;t try the others..
					ent, // &lt;- request headers (+ body)
					Map.class // response type
					/* , uriVars/... */
			).getBody(); // &lt;- returning ResponseEntity directly gave &quot;strange&quot; issues, &quot;referencing&quot; the body performs better (in browser).
}

}


Here the webflux version:
```lang-java
@Autowired
WebClient.Builder wcB;

@GetMapping(&quot;/forecast&quot;)
public Mono&lt;Map&gt; getForecast(/*...*/) {
	return wcB.build()
			.get()
			.uri(&quot;https://api.weather.gov/gridpoints/OKX/33,35/forecast&quot;)
			.headers(
			    h -&gt; {
                    h.put(&quot;User-Agent&quot;, List.of(UUID.randomUUID().toString(), &quot;contact@my.funky.website.com&quot;));
                    h.setAccept(List.of(MediaType.valueOf(&quot;application/geo+json&quot;)));
                }
            )
			.retrieve().bodyToMono(Map.class);
}

BTW

I had the "strange issues" (browser loading behavior) only on the mvc version, when directly returning resttemplate response, a "hold of the body" improves/fixes this. And a minimalist version:

return rtplB.build()
 .getForObject(
   &quot;https://api.weather.gov/gridpoints/OKX/33,35/forecast&quot;,
   Map.class
 ); // #

... "seems to work just fine".

huangapple
  • 本文由 发表于 2023年6月1日 16:50:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76380175.html
匿名

发表评论

匿名网友

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

确定