Java Spring安全测试:检索令牌
我已经成功地实现了允许我通过OAuth和UserDetailsService从应用程序中检索JWT令牌的代码 Atm我正试图实施测试以确认这一点,但我不断收到400个错误请求Java Spring安全测试:检索令牌,java,testing,spring-security,jwt,integration-testing,Java,Testing,Spring Security,Jwt,Integration Testing,我已经成功地实现了允许我通过OAuth和UserDetailsService从应用程序中检索JWT令牌的代码 Atm我正试图实施测试以确认这一点,但我不断收到400个错误请求 @Test public void is_status_up() throws InterruptedException, JSONException { // Given final String TOKEN_URL = "http://localhost:8080/oauth/token";
@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();
}
@测试
public void是_status_up()抛出InterruptedException,JSONException{
//给定
最终字符串标记\u URL=”http://localhost:8080/oauth/token";
多值映射参数=新的LinkedMultiValueMap();
参数添加(“用户名”、“用户名”);
参数添加(“凭证”、“凭证”);
参数添加(“授权类型”、“密码”);
HttpHeaders=新的HttpHeaders();
setAccept(Collections.singletonList(MediaType.APPLICATION\ujson\uutf8));
headers.setContentType(MediaType.APPLICATION\u FORM\u URLENCODED);
HttpEntity=新的HttpEntity(“参数”,标题);
HttpEntity请求=新的HttpEntity(参数、头);
RestTemplate RestTemplate=新RestTemplate();
restemplate.getInterceptors().add(新的BasicAuthorizationInterceptor(“客户端id”、“客户端机密”));
//什么时候
String result=restemplate.postForObject(TOKEN\u URL,request,String.class);
//然后
assertThat(result.isNotNull();
}
这应该尝试从Spring安全授权服务器的/oauth/token端点检索令牌,因为我遇到了400个错误请求
在成功检索令牌的postman中,我设置了请求
标题
授权:基本
内容类型:application/x-www-form-urlencoded
正文(选定x-www-form-urlencoded)
用户名:
密码:
grant_type:“password”问题是我在参数映射中放入了凭证,而凭证应该是password。我最初这样做是因为Sonarint或IntelliJ在它下面加了下划线,并发出嘶嘶声,以至于密码这个词被公开声明
- 解决方案
- 将参数映射中的“凭证”更改为“密码”