gpt4 book ai didi

spring - 在流模式下获取 java.net.HttpRetryException : cannot retry due to server authentication,

转载 作者:行者123 更新时间:2023-12-03 09:50:00 60 4
gpt4 key购买 nike

我为创建新用户创建了一个测试:

private static String USERS_ENDPOINT = "http://localhost:8080/users/";
private static String GROUPS_ENDPOINT = "http://localhost:8080/groups/";

@Test
@DirtiesContext(classMode = ClassMode.BEFORE_EACH_TEST_METHOD)
public void whenCreateAppUser() {

AppUser appUser = new AppUser();
appUser.setUsername("test@example.com");
appUser.setPassword("password");

// Throws java.net.HttpRetryException
template.postForEntity(USERS_ENDPOINT, appUser, AppUser.class);

ResponseEntity<AppUser> appUserResponse = template.getForEntity(USERS_ENDPOINT + "1/", AppUser.class);

assertEquals("Username is incorrect. AppUser not created?",
appUser.getUsername(), appUserResponse.getBody().getUsername());
}

但是,出于某种原因,我得到了:
Caused by: java.net.HttpRetryException: cannot retry due to server authentication, in streaming mode
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1692)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
at org.springframework.http.client.SimpleClientHttpResponse.getRawStatusCode(SimpleClientHttpResponse.java:55)
at org.springframework.web.client.DefaultResponseErrorHandler.hasError(DefaultResponseErrorHandler.java:49)
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:735)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:700)
... 34 more

对于通话
template.postForEntity(USERS_ENDPOINT, appUser, AppUser.class);

我实际上不知道我改变了什么,因为这曾经对我有用。知道是什么导致了这个问题吗?

我的 WebSecurity设置是:
@Override
public void configure(WebSecurity web) throws Exception {

final String[] SWAGGER_UI = {
"/swagger-resources/**",
"/swagger-ui.html",
"/v2/api-docs",
"/webjars/**"
};

web.ignoring().antMatchers("/pub/**", "/users")
.antMatchers(SWAGGER_UI);
}

最佳答案

Spring 的RestTemplate和 Spring Boot 的 TestRestTemplate将在 JDK 的内部 HttpURLConnection默认情况下实现,其中无法访问状态为 401“未授权”的 HTTP 响应的正文。

一个神奇的解决方案是将 Apache 的 HTTP 客户端包含在类路径中 — 例如与 Maven:

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
<scope>test</scope>
</dependency>

在这种情况下,Spring(或 Spring Boot)将使用 Apache 的 HTTP 客户端,它不会遇到问题。

另见: https://github.com/spring-projects/spring-security-oauth/issues/441#issuecomment-92033542

附言我刚刚解决了同样的问题:)

关于spring - 在流模式下获取 java.net.HttpRetryException : cannot retry due to server authentication,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49119354/

60 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com