Spring boot 需要帮助测试需要oauth2的REST控制器吗

Spring boot 需要帮助测试需要oauth2的REST控制器吗,spring-boot,oauth-2.0,Spring Boot,Oauth 2.0,我下面的示例介绍如何使用oauth2测试REST控制器 我遇到的代码是这一行,带有(httpBasic(“fooClientIdPassword”,“secret”) 有人知道httpBasic方法来自哪里吗?它是如何实例化的,等等。?多谢各位 private String obtainAccessToken(String username, String password) throws Exception { MultiValueMap<String, String>

我下面的示例介绍如何使用oauth2测试REST控制器

我遇到的代码是这一行
,带有(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();
}
私有字符串ActainAccessToken(字符串用户名、字符串密码)引发异常{
多值映射参数=新的LinkedMultiValueMap();
参数添加(“授权类型”、“密码”);
添加参数(“客户id”、“FooClientdPassword”);
参数添加(“用户名”,用户名);
参数添加(“密码”,密码);
结果
=mockMvc.perform(post(“/oauth/token”)
.params(params)
.with(httpBasic(“fooClientIdPassword”、“secret”))
.accept(“application/json;charset=UTF-8”))
.andExpect(状态().isOk())
.andExpect(content().contentType(“application/json;charset=UTF-8”);
字符串resultString=result.andReturn().getResponse().getContentAsString();
JacksonJsonParser=新的JacksonJsonParser();
返回jsonParser.parseMap(resultString.get(“访问令牌”).toString();
}

httpBasic方法来自

我想您找不到它,因为您没有在项目中导入依赖项。一旦你添加

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
</dependency>

org.springframework.security
弹簧安全性试验
测试

在pom中,您可以导入并使用它。

httpBasic方法来自

我想您找不到它,因为您没有在项目中导入依赖项。一旦你添加

<dependency>
  <groupId>org.springframework.security</groupId>
  <artifactId>spring-security-test</artifactId>
  <scope>test</scope>
</dependency>

org.springframework.security
弹簧安全性试验
测试
在pom中,您将能够导入并使用它