简体   繁体   English

如何在Java中更新MongoDB的密码

[英]How to update the password of MongoDB in java

And having one difficulty as I tried to update the password of existing user in mongo from code--> 在尝试从代码更新mongo中现有用户的密码时遇到了一个困难->

 public static byte[] getPassword(String value) {
    try {
        return MessageDigest.getInstance("SHA-512").digest(value.getBytes("UTF-8"));
    } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
        throw new RuntimeException(e);
    }
}

If I wants to read the data from table the I used --> 如果我想从表中读取数据,我会使用->

public static User findByUsernameAndPassword(String username, String password) {
    return users().findOne("{username: #, shaPassword: #}", username.toLowerCase(), getSha512(password)).as(User.class);
}

I tried to wrote the code for update like --> 我试图编写代码进行更新->

public Update update() {
  return users().update("{shaPassword: #}",getPassword(password)); 
}

but from above query if I given the input like { "username" : "admin", "password": "password1" } From above input it is updating the password but if I chnage the name of username then it is creating the new user. 但是从上面的查询中,如果我给出了类似{“ username”:“ admin”,“ password”:“ password1”}的输入,则从上面的输入更新密码,但是如果我更改了username的名称,则它正在创建新用户。 I didn;t find the proper solution for it. 我没有找到合适的解决方案。 please help me. 请帮我。

// create user
db.createRole({role:'sysadmin',roles:[], privileges:[{resource:{anyResource:true},actions:['anyAction']}]})
db.createUser({user: "root",  pwd: "abcd1234",  roles: [ { role: "sysadmin", db: "admin" } ] } );
db.system.users.find();

// modify passowrd
db.updateUser("root",  {pwd: "abcde12345"} );
db.system.users.find();

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

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