Adding Users to a GlassFish Realm via REST
Wednesday, March 09, 2011 |A user on the GlassFish forums recently asked how to create users in bulk. The asadmin
command create-file-user
doesn’t support passing the password as a parameter, which makes scripting difficult. The REST interface, though, can help there, and it’s really pretty simple.
The REST endpoint of interest is link: http://localhost:4848/management/domain/configs/config/server-config/security-service/auth-realm/file/create-user, and here’s a sample bash shell script to exercise it:
1
2
3
4
5
#!/bin/bash
for USER in user1 user2 user3 user4 user5 user6 user7 user8 user9 user10 ; do
curl -X POST -H 'Accept: application/json' \
-d id=$USER -d AS_ADMIN_USERPASSWORD=$USER http://localhost:4848/management/domain/configs/config/server-config/security-service/auth-realm/file/create-user
done
Want to see what users exist in the realm?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
$ curl -H 'Accept: application/json' http://localhost:4848/management/domain/configs/config/server-config/security-service/auth-realm/file/list-users
{
"command": "list-file-users AdminCommand",
"exit_code": "SUCCESS",
"extraProperties": {"users": [
{
"name": "user10",
"groups": []
},
{
"name": "user9",
"groups": []
},
// ...
]},
"children": [
{
"message": "user10",
"properties": {}
},
{
"message": "user9",
"properties": {}
},
// ...
]
}
Minor update
I forgot that the poster asked about deleting users. Here’s how you do that:
1
2
3
4
5
$ curl -H 'Accept: application/json' -X DELETE -d id=user1 $mgmt/configs/config/server-config/security-service/auth-realm/file/delete-user
{
"command": "delete-file-user AdminCommand",
"exit_code": "SUCCESS"
}
It’s important to note that only FileRealm
-based realms support user management in this manner. For other realm types, like LdapRealm
and JdbcRealm
, for example, the system administrator will need to use tools that are appropriate for the type of realm in question.
If you have any questions, leave a message. :)