#至少需要具有manage_security权限来使用此API



创建和更新用户

#创建或更新用户,用户名和密码不可更新 ps: POST/PUT均可
POST /_security/user/<username>
PUT /_security/user/<username>


#示例,创建用户
$ curl -u elastic:Chaofeng521 -X POST "localhost:9200/_security/user/chaofeng?pretty" -H 'Content-Type: application/json' -d'
> {
>   "password" : "Chaofeng521",
>   "roles" : [ "admin", "other_role1" ],
>   "full_name" : "ChaofengLi",
>   "email" : "cn_lichaofeng@163.com",
>   "metadata" : {    #metadata , 指定任意属性
>     "intelligence" : 7 ,
>     "weight" : 65
>   }
> }
> '
{
  "created" : true  #当非创建,而是更新了已有用户时,created返回值为false。
}


更改密码

#每个用户可以更改自己的密码
#具有manage_security权限的用户可以更改其他用户的密码
POST /_security/user/_password  #更改当前用户密码
POST /_security/user/<username>/_password  #更改指定用户密码

#示例:
$ curl -u chaofeng:Chaofeng521.2 -X POST "localhost:9200/_security/user/chaofeng/_password?pretty" -H 'Content-Type: application/json' -d'
> {
>   "password" : "s3cr3t"
> }
> '
{ }  #执行成功会返回空JSON


$ curl -u chaofeng:s3cr3t -X POST "localhost:9200/_security/user/_password?pretty" -H 'Content-Type: application/json' -d'
> {
>   "password" : "Chaofeng521"
> }
> '
{ }


删除用户

DELETE /_security/user/<username>

#示例,
$ curl -u elastic:Chaofeng521  -X DELETE "localhost:9200/_security/user/chaofeng2?pretty"
{
  "found" : true #成功删除返回found true,否则返回false。
}


禁用用户

PUT /_security/user/<username>/_disable

#示例
$ curl -u elastic:Chaofeng521 -X PUT "localhost:9200/_security/user/chaofeng/_disable?pretty"
{ }


启用用户

PUT /_security/user/<username>/_enable

#示例
$ curl -u elastic:Chaofeng521 -X PUT "localhost:9200/_security/user/chaofeng/_enable?pretty"
{ }


查询用户

#查询所有用户
GET /_security/user 
#查询指定用户
GET /_security/user/<username>

#示例
$ curl -u elastic:Chaofeng521 -X GET "localhost:9200/_security/user/chaofeng2?pretty"
{
  "chaofeng2" : {
    "username" : "chaofeng2",
    "roles" : [
      "admin",
      "other_role1"
    ],
    "full_name" : "ChaofengLi",
    "email" : "cn_lichaofeng@163.com",
    "metadata" : {
      "weight" : 65,
      "intelligence" : 7
    },
    "enabled" : true
  }
}
  • No labels