我成功地实现了代码,它允许我通过OAuth和UserDetailsService从我的应用程序中检索一个JWT令牌。
我正在尝试实现测试来确认这一点,但是我总是收到400条坏请求
@Test
public void is_status_up() throws InterruptedException, JSONException {
// Given
final String TOKEN_URL = "http://localhost:8080/oauth/token";
MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
params.add("username","username");
params.add("credential","credentials");
params.add("grant_type","password");
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON_UTF8));
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<String> entity = new HttpEntity<>("parameters" , headers);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(params, headers);
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(new BasicAuthorizationInterceptor("client-id","client-secret"));
//When
String result = restTemplate.postForObject(TOKEN_URL, request, String.class);
//Then
assertThat(result).isNotNull();
}这应该尝试从的/oauth/token端点检索令牌,我遇到了一个400坏的请求。
在成功检索令牌的postman中,我已经设置了请求。
标题授权:基本内容-类型: application/x-www-form-urlencoded
正文(x-www-form-urlencoded所选)用户名:密码:grant_type:“密码”
发布于 2018-06-12 15:00:51
问题是,当密码应该是密码时,我将凭证放在我的param中。我最初是这样做的,就像SonarLint或IntelliJ所强调的那样,并抛出了一个令人讨厌的词密码是如此公开的声明。
https://stackoverflow.com/questions/50820063
复制相似问题