gpt4 book ai didi

java - Spring Boot 和 OAuth2 : Getting ResourceAccessException: I/O error on POST request for "http://localhost:5555/oauth/token": Connection refused: connect

转载 作者:行者123 更新时间:2023-12-02 13:32:55 27 4
gpt4 key购买 nike

基于此示例https://github.com/royclarkson/spring-rest-service-oauth我修改了源代码,添加了存储库和其他休息 Controller 。资源和授权服务器在同一应用程序中运行/配置。

我添加了一个带有请求正文的端点“/api/login”(POST):

{"usernamme":"foo", "password":"bar"}

我想在 Controller 调用的服务中获取该用户的访问 token 。此实现如下所示:

Controller

public ResponseEntity<OAuth2AccessToken> login(@RequestBody @Valid final LoginCommand credentials)
throws LoginFailedException, UnexpectedErrorException {
try {
final OAuth2AccessToken token = securityService.authenticate(credentials.getUsername(), credentials.getPassword());
return ResponseEntity.ok(token);
} catch (final InvalidGrantException badCredentialExeption) {
throw new LoginFailedException(badCredentialExeption.getMessage());
} catch (final Exception e) {
throw new UnexpectedErrorException(e.getMessage());
}
}

服务

@Autowired
@Qualifier("OAuth2RestOperations")
private OAuth2RestOperations client;

@Override
public OAuth2AccessToken authenticate(final String username, final String password) {
final ResourceOwnerPasswordResourceDetails resourceDetails = (ResourceOwnerPasswordResourceDetails) client.getResource();
resourceDetails.setUsername(username);
resourceDetails.setPassword(password);
return client.getAccessToken();
}

其余客户端配置

@Configuration
@Import({ OauthProperties2.class })
@EnableOAuth2Client
public class RestClientConfig {

@Autowired
private OauthProperties2 oauth;

@Bean(name = "OAuth2RestOperations")
public OAuth2RestOperations restTemplate(final OAuth2ClientContext oauth2ClientContext) {
return new OAuth2RestTemplate(resource(), oauth2ClientContext);
}

@Bean
public OAuth2ProtectedResourceDetails resource() {
final ResourceOwnerPasswordResourceDetails resource = new ResourceOwnerPasswordResourceDetails();
resource.setAccessTokenUri(oauth.getClient().getAccessTokenUri());
resource.setClientId(oauth.getClient().getClientId());
resource.setClientSecret(oauth.getClient().getClientSecret());
resource.setGrantType(oauth.getClient().getGrantType());
resource.setScope(oauth.getClient().getScope());
return resource;
}

}

测试

public class SecurityApiControllerTest extends TestBase {

@InjectMocks
private SecurityApiController controller;

@Test
public void loginOK() throws Exception {
final String credentials = FileUtils.readContent("requests/loginOK.json");
// @formatter:off
mvc.perform(post("/api/login")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(credentials))
.andExpect(status().isOk());
// @formatter:on
}

}

测试库

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@SpringBootTest(classes = Application.class)
public class TestBase {

@Autowired
protected WebApplicationContext context;

@Autowired
protected FilterChainProxy springSecurityFilterChain;

protected MockMvc mvc;

@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mvc = MockMvcBuilders.webAppContextSetup(context).addFilter(springSecurityFilterChain).build();
}
}

运行应用程序时,我可以使用 POSTMAN 等调用端点。在测试过程中,我收到连接被拒绝的信息,如问题标题中所述。我试图找出测试不起作用的原因。有人可以给我提示来解决这个问题吗?

最佳答案

这真是太奇怪了。它有效,因为我已将 TestBase 类更改为:

@RunWith(SpringJUnit4ClassRunner.class)
// @WebAppConfiguration
@ContextConfiguration(classes = Application.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)

关于java - Spring Boot 和 OAuth2 : Getting ResourceAccessException: I/O error on POST request for "http://localhost:5555/oauth/token": Connection refused: connect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43126406/

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