我已经在 Spring boot 中实现了 OAuth2。在 JUnit 中测试它时效果很好,但当我在 postman 中尝试该 API 时,我总是遇到未经授权的情况。
JUnit 中的测试函数:
private String obtainAccessToken(String username, String password) throws Exception {
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("grant_type", "password");
params.add("client_id", CLIENT_ID);
params.add("username", username);
params.add("password", password);
ResultActions result = mockMvc.perform(post("/oauth/token")
.params(params)
.with(httpBasic(CLIENT_ID, CLIENT_SECRET))
.accept(CONTENT_TYPE))
.andExpect(status().isOk())
.andExpect(content().contentType(CONTENT_TYPE));
String resultString = result.andReturn().getResponse().getContentAsString();
JacksonJsonParser jsonParser = new JacksonJsonParser();
return jsonParser.parseMap(resultString).get("access_token").toString();
}
我在 postman 中尝试了以下 API:
POST 类型 http://localhost:8080/oauth/token
内容类型为 application/json
在正文部分我选择了 raw 和 JSON :
{
"grant_type":"password",
"client_id":"clientIdPassword",
"username":"a",
"password":"a"
}
显示401未经授权。然后我也尝试了这样的:
内容类型为application/json
的帖子类型,http://localhost:8080/oauth/token?grant_type=password&client_id=clientIdPassword&username=a&password=a
。这也显示 401 Unauthorized。
我的问题是如何在 Postman 中将 MultiValueMap
设置为参数?
最佳答案
关于java - Postman 中的 Spring Boot MultiValueMap 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141182/