【问题标题】:Bad Request while RestTemplate TestingRestTemplate 测试时出现错误请求
【发布时间】:2019-02-19 07:25:06
【问题描述】:

我有一个问题,当我用 maven 测试我的测试应用程序时,它显示 400 Bad Request。我将 oauth2 与基本身份验证和 grant_type password 一起使用。如果我使用 Postman 等程序进行身份验证。有用。但是,如果我使用 Http-Headers 添加基本身份验证,我会收到错误的请求。

我尝试从 HttpEntity 中删除 HttpHeaders() 对象,然后得到 401 Unauthorized。我尝试将加密的标头手动放入HttpHeaders() 对象中,但这也让我得到400 Bad Request。如果我使用Postman 进行请求,请选择POST 作为方法并选择Basic Auth 作为身份验证,使用username, password, grant_type 作为正文(JSON)它可以工作。

所以这是我的测试课 @SpringBootTest 公共类 TestAppApplicationTests {

    private RestTemplate restTemplate = new RestTemplate();


    @Test
    public void testOauthToken(){

        String plainCreds = "client:topsecret";
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);

        Object oauthRequest = new OauthRequest("Lukas", "test123", "password");

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic "+ base64Creds);
        HttpEntity<Object> reqData = new HttpEntity<>(oauthRequest, headers);

        ResponseEntity<Object> entity  = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
            reqData,
            Object.class);

    }


}

这是我的OauthRequest 课程

@Data
public class OauthRequest {
    private String username;
    private String password;
    private String grant_type;

    public OauthRequest(String username, String password, String grant_type){
        this.username = username;
        this.password = password;
        this.grant_type = grant_type;
    }
}

我想从服务器获取access_tokenrefresh_token

编辑:我这里有一个不工作的存储库,如果您想了解有关代码的更多信息:https://github.com/precodeeu/test-spring-oauth

Edit²:如果我将原始 json 字符串放入其中,它也不起作用。

    OauthRequest oauthRequest = new OauthRequest("Lukas", "test123", "password");
    String json;

    try {
        json = mapper.writeValueAsString(oauthRequest);
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + base64Creds);
        headers.add("Content-Type", "application/json");
        HttpEntity<Object> reqData = new HttpEntity<>(json, headers);

        ResponseEntity<Object> entity = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
                reqData,
                Object.class);

    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

编辑³ 我刚刚注意到,内容类型应该是Application_form_urlencoded

解决方案: 我已经更新了代码并构建了一个 Stringbuilder

@SpringBootTest 公共类 TestAppApplicationTests {

private RestTemplate restTemplate = new RestTemplate();


private String UrlEncodedBuilder(List<String[]> list){
    String returnString = "";
    for(String[] param : list){
        returnString+=param[0]+"="+param[1]+"&";
    }

    return returnString;
}

@Test
public void testOauthToken() {

    String plainCreds = "client:topsecret";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);

    List<String[]> formData = new ArrayList();

    formData.add(new String[]{"username", "Lukas"});
    formData.add(new String[]{"password", "test123"});
    formData.add(new String[]{"grant_type", "password"});

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    headers.add("Authorization", "Basic " + base64Creds);
    HttpEntity<Object> reqData = new HttpEntity<>(UrlEncodedBuilder(formData), headers);

    ResponseEntity<Object> entity = restTemplate.exchange("http://localhost:8080/oauth/token", HttpMethod.POST,
            reqData,
            Object.class);


}
}

【问题讨论】:

    标签: java spring http oauth


    【解决方案1】:

    首先,调查您的请求的具体情况,并将其与可行的请求进行比较。 如果是 400 代码,还要检查响应正文,正文可能包含一些提示。

    【讨论】:

    • 嘿伙计,我只是错误地提交了 application/json 而不是 application/x-form-urlencoded。现在我已经构建了一个 Stringbuilder 并且它可以工作。我在这里发布我的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 2023-03-26
    • 2020-04-07
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    相关资源
    最近更新 更多