简体   繁体   English

无法使用HTTP PUT将用户添加到CouchDB中的数据库/ _security

[英]Cannot add user to database/_security in CouchDB using HTTP PUT

The problem 问题

In my Android app, I am trying to add a user to the /_security document of my CouchDB database via HTTP PUT. 在我的Android应用中,我试图通过HTTP PUT将用户添加到CouchDB数据库的/ _security文档中。 If I am trying to do this by authenticating my admin user using Cookie-authentication or by simply inserting the admin data into the url like in the following, I receive an error. 如果我尝试通过使用Cookie身份验证对我的管理员用户进行身份验证,或者仅通过将管理数据插入url中(如下所示)来执行此操作,则会收到错误消息。

URL on which the PUT is directed (if not using Cookie-authentication): 指向PUT的URL(如果未使用Cookie身份验证):

http://admin_name:admin_password@url:port/databasename/_security

Error I receive in both cases: 在两种情况下我都收到错误消息:

Authentication error: Unable to respond to any of these challenges: {} {"error":"unauthorized","reason":"You are not a db or server admin."} 验证错误:无法应对以下任何挑战:{} {“错误”:“未授权”,“原因”:“您不是数据库或服务器管理员。”}

If I am doing this via command-line using curl, the user is inserted without any problems: 如果我通过命令行使用curl进行此操作,则可以毫无问题地插入用户:

~$ curl -X PUT http://admin:pw@ip:port/databasename/_security -d '{"admins":{"names":[],"roles":[]},"members":{"names":["participant_1"],"roles":[]}}'

> {"ok":true}

My aproach 我的方法

As soon as I authenticate using the "Authorization" option in one of my HTTP PUT's headers, athentication is no problem anymore. 一旦使用HTTP PUT标头之一中的“授权”选项进行身份验证,身份认证就不再是问题。

private boolean putJSONWithAuthentication(String userName, String password, String json, String url) {

    // url = http://url:port/databasename/_security
    // json = {"admins":{"names":[],"roles":[]},"members":{"names":["participant_1"],"roles":[]}}
    HttpClient client = new DefaultHttpClient();
    HttpPut put = new HttpPut(url);

    String authenticationData = userName+":"+password;
    String encoding = Base64.encodeToString(authenticationData.getBytes(Charset.forName("utf-8")), Base64.DEFAULT);

    try {
        StringEntity stringEntity = new StringEntity(json,"utf-8");
        put.setEntity(stringEntity);
        put.setHeader("Content-type", "application/json; charset=utf-8");
        put.setHeader("Accept", "application/json");
        put.setHeader("Authorization", "Basic " + encoding);

        HttpResponse response = client.execute(put);
        BufferedReader rd     = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
        return true;

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }       
}

However, I am receiving this error: 但是,我收到此错误:

> {"error":"bad_request","reason":"invalid_json"}

If I am inserting my user-JSON as a usual document using the above method, eg, to http://url:port/databasename/new_document , the JSON is inserted without any errors. 如果我使用上述方法将用户JSON作为普通文档插入(例如)到http://url:port/databasename/new_document ,则插入JSON不会出现任何错误。 Consequently, I guess the JSON string should be formatted correctly. 因此,我猜应该正确格式化JSON字符串。

Thus, my questions is, what am I missing here? 因此,我的问题是,我在这里想念什么? It seems like I cannot authenticate and put data in the request body at once. 看来我无法立即进行身份验证并将数据放入请求正文中。 How do I correctly insert a user to the /_security document of a database from code? 如何从代码正确地将用户插入数据库的/ _security文档?

Instead of creating the basic auth header manually, can you try create it using UsernamePasswordCredentials , eg 您可以尝试使用UsernamePasswordCredentials创建它,而不是手动创建基本的auth标头,例如

HttpPut put = new HttpPut(url);
UsernamePasswordCredentials creds = new UsernamePasswordCredentials(userName, password);
put.addHeader( BasicScheme.authenticate(creds,"US-ASCII",false) );
...
HttpResponse response = client.execute(put);

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

相关问题 使用CouchDB进行身份验证,并通过Android应用中的HTTP PUT插入新数据库 - Authenticate with CouchDB and insert new database via HTTP PUT in Android app HTTP PUT请求将数据保存到CouchDB服务器-Java Android吗? - HTTP PUT request to save data to CouchDB Server - Java Android? 在Spring Boot安全性中无法从表单添加用户 - Cannot add user from form in spring boot security 无法从Java向数据库添加新用户 - Cannot add new user to database from java 如何使用Couchdb4j库向CouchDB添加附件 - How to add attachment to CouchDB using Couchdb4j library HTTP PUT使用部分Json更新数据库-Spring,Postgres - HTTP PUT to update database using partial Json - Spring, Postgres 使用Spring Security,我如何使用HTTP方法(例如GET,PUT,POST)来确定特定URL模式的安全性? - Using Spring Security, how can I use HTTP methods (e.g. GET, PUT, POST) to distingush security for particular URL patterns? 使用Spring Security和自定义用户服务的Oracle数据库连接问题 - oracle database connecting issue using spring security and custom user service 无法将条目放入mysql数据库 - Cannot put entry in the mysql database 我不能在数据库中添加多个用户 - I cannot add more than one user in the database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM