【问题标题】:oauth2 integration test with spring bootoauth2 与 Spring Boot 的集成测试
【发布时间】:2020-04-11 15:20:58
【问题描述】:

我在为我的应用程序编写集成测试时遇到问题。手动测试集成按预期工作得非常好;只是在编写集成测试时遇到问题。我按照https://www.baeldung.com/oauth-api-testing-with-spring-mvc 的指南进行操作,但它似乎不适用于我的应用程序。 (不确定是不是因为我使用的是 JUnit5)。我真的不知道我到底错过了什么。该请求在最后以 401 禁止而不是 200 响应,以设​​置新的访问令牌。

application.yml

spring:
  profiles: test
security:
  oauth:
    client:
      client-id: client-id
      client-secret: client-secret

集成测试

@SpringBootTest
@AutoConfigureMockMvc
@ActiveProfiles("test")
public class MyIntegrationTest {

    private MockMvc mvc;

    @Autowired
    private FilterChainProxy filterChainProxy;

    @Autowired
    private WebApplicationContext webApplicationContext;

    @Value("${security.oauth2.client.client-id}")
    private String clientId;

    @Value("${security.oauth2.client.client-secret}")
    private String clientSecret;

    @BeforeEach
    public void setUp() {
        mvc = MockMvcBuilders
                .webAppContextSetup(webApplicationContext)
                .addFilter(filterChainProxy).build();
    }

    private String obtainAccessToken() throws Exception {
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("grant_type", "client_credentials");
        params.add("client_id", clientId);
        params.add("client_secret", clientSecret);
        params.add("scope", "any");

        String mvcResult = mvc.perform(post("/oauth/token")
            .params(params)
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON))
            .andReturn()
            .getResponse().getContentAsString();

        return new JacksonJsonParser().parseMap(mvcResult).get("access_token").toString();
    }


}

【问题讨论】:

    标签: java spring-boot junit mockmvc


    【解决方案1】:

    你需要改变这个:

    MediaType.APPLICATION_JSON
    

    关于这个:

    MediaType.APPLICATION_JSON_UTF8_VALUE
    

    或者这个,如示例:

    "application/json;charset=UTF-8"
    

    【讨论】:

      【解决方案2】:

      实际上缺少的是客户端的授权。需要添加授权标头。

      private String obtainAccessToken() throws Exception {
              MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
              params.add("grant_type", "client_credentials");
              params.add("scope", "any");
      
              String mvcResult = mvc.perform(post("/oauth/token")
                  .params(params)
                   // including authorization headers
                  .headers("Authorization", "Basic " + new String(Base64Utils.encode((clientId + ":" + clientSecret).getBytes()))))
                  .accept(MediaType.APPLICATION_JSON_UTF8_VALUE))
                  .andExpect(status().isOk())
                  .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
                  .andReturn()
                  .getResponse().getContentAsString();
      
              return new JacksonJsonParser().parseMap(mvcResult).get("access_token").toString();
          }
      

      【讨论】:

        猜你喜欢
        • 2017-10-27
        • 2018-02-08
        • 2020-02-25
        • 1970-01-01
        • 1970-01-01
        • 2015-08-20
        • 2020-04-24
        • 1970-01-01
        • 2017-06-28
        相关资源
        最近更新 更多