【发布时间】:2018-08-14 22:07:16
【问题描述】:
我已经在 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();
}
我在邮递员中尝试了以下 API:
POST 类型 http://localhost:8080/oauth/token 与内容类型 application/json
在正文部分,我选择了 raw 和 JSON:
{
"grant_type":"password",
"client_id":"clientIdPassword",
"username":"a",
"password":"a"
}
显示 401 Unauthorized。然后我也这样尝试:
内容类型为application/json、http://localhost:8080/oauth/token?grant_type=password&client_id=clientIdPassword&username=a&password=a 的帖子类型。这也显示 401 Unauthorized。
我的问题是如何在 Postman 中将 MultiValueMap 设置为参数?
【问题讨论】:
-
从 Postman 中设置基本身份验证?
-
嘿@Eniss 你得到解决方案了吗?
标签: java spring spring-boot junit postman