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

标签 java rest spring-mvc spring-boot oauth

基于此示例 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/

相关文章:

java - 无法在android中将值插入SQLite数据库

java - 检测 JButton 中的文本溢出

spring - Thymeleaf 中的有条件关闭标签

java - spring 中的 org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException

java - 从字符串反序列化Java二叉树

java - 无法获得 JDK 11、Spring Boot 的组合。持久性和 JavaFX 工作

c# - 格式化用于ESRI地理编码器的RestSharp请求

java - 为什么 swagger ui 不显示我带注释的 REST 方法?

java - hibernate 一对一映射不更新子表

java - Spring MVC/Hibernate/mySQL "read"始终为空