Powered By GitBook
Authenticating with a User and Password
For this example, start the server using:
1
> nats-server --user myname --pass password
Copied!
You can encrypt passwords to pass to nats-server using a simple tool:
1
> nats server passwd
2
? Enter password [? for help] **********************
3
? Reenter password [? for help] **********************
4
5
$2a$11$qbtrnb0mSG2eV55xoyPqHOZx/lLBlryHRhU3LK2oOPFRwGF/5rtGK
Copied!
and use the hashed password in the server config. The client still uses the plain text version.
The code uses localhost:4222 so that you can start the server on your machine to try them out.

Connecting with a User/Password

When logging in with a password nats-server will take either a plain text password or an encrypted password.
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Set a user and plain text password
2
nc, err := nats.Connect("127.0.0.1", nats.UserInfo("myname", "password"))
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Options options = new Options.Builder().
2
server("nats://localhost:4222").
3
userInfo("myname","password"). // Set a user and plain text password
4
build();
5
Connection nc = Nats.connect(options);
6
7
// Do something with the connection
8
9
nc.close();
Copied!
1
let nc = NATS.connect({url: server.nats, user: "myname", pass: "password"});
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://myname:[email protected]:4222"])
4
5
# Do something with the connection.
Copied!
1
require 'nats/client'
2
3
NATS.start(servers:["nats://myname:[email protected]:4222"], name: "my-connection") do |nc|
4
nc.on_error do |e|
5
puts "Error: #{e}"
6
end
7
8
nc.on_reconnect do
9
puts "Got reconnected to #{nc.connected_server}"
10
end
11
12
nc.on_disconnect do |reason|
13
puts "Got disconnected! #{reason}"
14
end
15
16
nc.close
17
end
Copied!
1
let nc = await connect({url: server.nats, user: "myname", pass: "password"});
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetUserInfo(opts, "myname", "password");
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!

Connecting with a User/Password in the URL

Most clients make it easy to pass the user name and password by accepting them in the URL for the server. This standard format is:
nats://user:password@server:port
Using this format, you can connect to a server using authentication as easily as you connected with a URL:
Go
Java
JavaScript
Python
Ruby
TypeScript
C
1
// Set a user and plain text password
2
nc, err := nats.Connect("myname:[email protected]")
3
if err != nil {
4
log.Fatal(err)
5
}
6
defer nc.Close()
7
8
// Do something with the connection
Copied!
1
Connection nc = Nats.connect("nats://myname:[email protected]:4222");
2
3
// Do something with the connection
4
5
nc.close();
Copied!
1
let url = `nats://myname:[email protected]:${port}`;
2
let nc = NATS.connect(url);
Copied!
1
nc = NATS()
2
3
await nc.connect(servers=["nats://myname:[email protected]:4222"])
4
5
# Do something with the connection.
Copied!
1
require 'nats/client'
2
3
NATS.start(servers:["nats://myname:[email protected]:4222"], name: "my-connection") do |nc|
4
nc.on_error do |e|
5
puts "Error: #{e}"
6
end
7
8
nc.on_reconnect do
9
puts "Got reconnected to #{nc.connected_server}"
10
end
11
12
nc.on_disconnect do |reason|
13
puts "Got disconnected! #{reason}"
14
end
15
16
nc.close
17
end
Copied!
1
let url = `nats://myname:[email protected]:${port}`;
2
let nc = await connect({url: url});
Copied!
1
natsConnection *conn = NULL;
2
natsOptions *opts = NULL;
3
natsStatus s = NATS_OK;
4
5
s = natsOptions_Create(&opts);
6
if (s == NATS_OK)
7
s = natsOptions_SetURL(opts, "nats://myname:[email protected]:4222");
8
if (s == NATS_OK)
9
s = natsConnection_Connect(&conn, opts);
10
11
(...)
12
13
// Destroy objects that were created
14
natsConnection_Destroy(conn);
15
natsOptions_Destroy(opts);
Copied!
Last modified 8mo ago