Webclient 401未经授权

时间:2020-10-27 10:30:37

标签: java spring-boot http-status-code-401 spring-webclient

我正在使用WebClient访问端点(它们是简单的回显端点)来测试连接。 每次我调用WebClient的get方法时,都会收到401错误,但是当我尝试使用具有相同凭据的Swagger访问端点时,它可以正常工作。我敢肯定我忘了一些简单的事情,但是看不同的教程,看来我已经正确地完成了所有事情。

我的代码:

try{
WebClient webClient = WebClient
            .builder()
                .baseUrl("http://localhost:8030")
                .defaultHeaders(headers -> headers.setBasicAuth("admin","adminPassword"))
            .build();

String getValue = webClient.get()
                    .uri(uriBuilder -> uriBuilder
                            .path("/echo")
                            .queryParam("inputString", "{input}")
                            .build("get test"))
                    .retrieve()
                    .bodyToMono(String.class)
                    .block();
            System.out.println(getValue);
}catch (WebClientResponseException ex){
            System.out.println("WebClientResponseException code: " + ex.getStatusCode() +"\n"+"WebClientResponseMessage: " + ex.getResponseBodyAsString());
            throw ex;
        }

这是我得到的打印错误:

WebClientResponseException code: 401 UNAUTHORIZED
WebClientResponseMessage: <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>401 Unauthorized</title>
    <meta name="robots" content="noindex,nofollow"/>
  </head>
  <body>
    <h1>401 Unauthorized</h1>
  </body>
</html>

0 个答案:

没有答案