我正在关注如何使用oauth2测试我的REST控制器的示例。 Testing an OAuth Secured API with Spring MVC
我坚持使用的代码是 .with(httpBasic("fooClientIdPassword","secret"))
这一行
有谁知道httpBasic方法来自哪里?它是如何实例化的?等等?谢谢。
private String obtainAccessToken(String username, String password) throws Exception {
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "password");
params.add("client_id", "fooClientIdPassword");
params.add("username", username);
params.add("password", password);
ResultActions result
= mockMvc.perform(post("/oauth/token")
.params(params)
.with(httpBasic("fooClientIdPassword","secret"))
.accept("application/json;charset=UTF-8"))
.andExpect(status().isOk())
.andExpect(content().contentType("application/json;charset=UTF-8"));
String resultString = result.andReturn().getResponse().getContentAsString();
JacksonJsonParser jsonParser = new JacksonJsonParser();
return jsonParser.parseMap(resultString).get("access_token").toString();
}
答案 0 :(得分:1)
httpBasic方法来自SecurityMockMvcRequestPostProcessors
我想您找不到它,因为您没有在项目中导入依赖项。添加
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
您可以在pom中导入和使用它。