【问题标题】:Unable to make request with feign, "feign.FeignException$Forbidden: [403 Forbidden]"无法使用 feign 提出请求,“feign.FeignException$Forbidden: [403 Forbidden]”
【发布时间】:2020-08-25 05:47:08
【问题描述】:

我尝试在我的 Spring Boot 应用程序上配置 OpenFeign,我使用 pokeapi 进行测试。

我做这个代码:

@FeignClient(value = "pokeapi", url = "https://pokeapi.co")
public interface PokeApiClient {

@Headers("Content-Type: application/json")
@RequestMapping(method = RequestMethod.GET, value = "/api/v2/pokemon/{name}", consumes = 
"application/json")
Optional<Pokemon> findPokemonByName(@PathVariable("name") String name);

}

但是当我进行此调用时,会发生此错误:feign.FeignException$Forbidden: [403 Forbidden] during [GET] to [https://pokeapi.co/api/v2/pokemon/ditto] [PokeApiClient#findPokemonByName(String)]: [error code: 1010]

在这种情况下我该怎么办?

我尝试用这个来配置 WebSecurity:

@Configuration
@EnableWebSecurity
public class HttpConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .csrf().disable()
                .authorizeRequests()
                .antMatchers(HttpMethod.POST, "/**/authenticate").permitAll()
                .antMatchers(HttpMethod.GET, "/**/get-public-key").permitAll()
                .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
                .anyRequest().permitAll();
    }
}

【问题讨论】:

    标签: java spring spring-boot spring-mvc spring-cloud-feign


    【解决方案1】:

    经过很长时间后,通过在 RequestHeader 下面添加来解决问题 "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"

    之后一切正常。

    我正在使用 Feign Client 调用 POST API,下面是 scrrentshot enter image description here

    【讨论】:

      【解决方案2】:

      403 禁止表示服务器理解请求但拒绝授权,因此可能是权限问题。错误代码 1010 有时是网站所有者根据您的浏览器阻止的请求...希望这些想法会有所帮助? :)

      【讨论】:

      • 我想了解什么是错的。我在浏览器上输入这个网址就可以了:(
      • 我遇到了一个与您的问题非常相似的问题,但不一样。我解决了它,编写了一个从 URL 获取 JSON 作为字符串的方法(使用 GET 请求):gist.github.com/jsfan3/f1b224635f5e13f6ed17015da15e7d7f
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2020-07-03
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多