简体   繁体   English

使用Java(HttpURLConnection)对Restheart进行身份验证(用于Mongodb)

[英]Using Java (HttpURLConnection) to authenticate to Restheart (for Mongodb)

I am using restheart to provide a restful interface to mongodb. 我正在使用restheart为mongodb提供一个restful接口。 The interface is set up and running and provides the correct answer if a GET request is sent through Chrome. 该界面已设置并正在运行,并且如果通过Chrome发送GET请求,则会提供正确的答案。 However if I use the following java code using a HttpURLConnection I get a 201 response with no content. 但是,如果我通过HttpURLConnection使用以下Java代码,则会收到201响应,但没有内容。

try {
    videos = new URL("http://www.example.com:8080/myflix/videos");
    } catch (Exception et) {
        System.out.println("Videos URL is broken");
        return null;
    }
    HttpURLConnection hc = null;
    try {
        hc = (HttpURLConnection) videos.openConnection();
        String login="admin:admin"; 
        final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);
        final String encoded = Base64.getEncoder().encodeToString(authBytes);
        hc.addRequestProperty("Authorization", "Basic "+encoded);
        hc.setDoInput(true);
        hc.setDoOutput(true);
        hc.setUseCaches(false);
        hc.setRequestMethod("GET");
        hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        hc.setRequestProperty("Content-Type", "application/json");
        hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
    } catch (Exception et) {
        System.out.println("Can't prepare http URL con");
        return (null);
    }
    BufferedReader br = null;
    try {
        OutputStreamWriter writer = new OutputStreamWriter(
                hc.getOutputStream());
    } catch (Exception et) {
        System.out.println("Can't get reader to videos stream");
    }
    String inputLine;
    String sJSON = null;

    try {
        int rc = hc.getResponseCode();

What is the correct way to authenticate using Java to the resthert interface? 使用Java对resthert接口进行身份验证的正确方法是什么? (Details on the restheart authentication is here Restheart authentication (上restheart认证细节在这里Restheart认证

I made few changes (look for inline comments starting with <==) and it works: 我进行了一些更改(查找以<==开头的内联注释),并且可以正常工作:

The way you generate the authentication request header is correct. 您生成身份验证请求标头的方式是正确的。 When I run your code I actually got 415 Unsupported Media Type, that went away commenting out hc.setDoOutput(true). 当我运行您的代码时,我实际上得到了415不支持的媒体类型,而将hc.setDoOutput(true)注释掉了。 A GET is a input operation, in fact you were also trying to get an OutStream from the connection: you need to get an InputStream actually. GET是一种输入操作,实际上您也在尝试从连接中获取OutStream:实际上需要获取InputStream。

    URL url;

    try {
        url = new URL("http://127.0.0.1:8080/test/huge");
    } catch (Exception et) {
        System.out.println("Videos URL is broken");
        Assert.fail(et.getMessage());
        return;
    }

    HttpURLConnection hc = null;

    try {
        hc = (HttpURLConnection) url.openConnection();

        String login = "admin:admin";
        final byte[] authBytes = login.getBytes(StandardCharsets.UTF_8);

        final String encoded = Base64.getEncoder().encodeToString(authBytes);
        hc.addRequestProperty("Authorization", "Basic " + encoded);

        System.out.println("Authorization: " + hc.getRequestProperty("Authorization"));

        hc.setDoInput(true);
        //hc.setDoOutput(true); <== removed, otherwise 415 unsupported media type
        hc.setUseCaches(false);

        hc.setRequestMethod("GET");
        hc.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
        hc.setRequestProperty("Accept", "application/json,text/html,application/hal+json,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*");
    } catch (Exception et) {
        System.out.println("Can't prepare http URL con");
    }

    System.out.println(hc.toString());

    BufferedReader br = null;

    try {
        InputStreamReader reader = new InputStreamReader(hc.getInputStream()); // <== the request is a GET, data is in input
    } catch (Exception et) {
        System.out.println("Can't get reader to videos stream");
    }

    int rc = hc.getResponseCode();

    System.out.println("response code: " + rc);
    System.out.println("response message: " + hc.getResponseMessage());

    Assert.assertEquals(200, rc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM