Since SurrealDB is a database that is designed to be used in a distributed environment, it is important to secure the database and the data that is stored in it. SurrealDB provides a number of methods for authenticating users and securing the database.
In your SurrealDB database, you can configure authentication using the DEFINE USER
or DEFINE ACCESS
statements.
Method | Description |
---|---|
db.signin(Signin credentials) | Authenticate with provided credentials |
You can call the .signin()
method on a Surreal
instance to authenticate with the database. The signin
method takes any valid Signin
implementation as an argument, which contains the credentials required to authenticate with the database.
The following Signin
implementations are available:
new Root(String username, String password)
new Namespace(String username, String password, String namespace)
new Database(String username, String password, String namespace, String database)
Here is an example of the .signin()
methods in action.
Example.javapackage com.surrealdb.example; import com.surrealdb.Surreal; import com.surrealdb.signin.Database; import com.surrealdb.signin.Namespace; import com.surrealdb.signin.Root; public class Example { public static void main(String[] args) { try (final Surreal driver = new Surreal()) { driver.connect("wss://example.com"); driver.useNs("example").useDb("example"); // Authenticate as root user driver.signin(new Root("root", "root")); // Authenticate as a namespace user driver.signin(new Namespace("root", "root", "ns")); // Authenticate as a database user driver.signin(new Database("root", "root", "ns", "db")); } } }