Authentication
The Java SDK supports signing in as a root, namespace, database, or record-level user. After signing in, the connection is authenticated for all subsequent operations until the session is invalidated or the connection is closed.
You can configure authentication in your SurrealDB database using the DEFINE USER or DEFINE ACCESS statements.
API References
| Method | Description |
|---|---|
db.signin(credential) | Authenticates with the provided credentials |
db.signup(credential) | Signs up a new record user |
db.authenticate(token) | Authenticates with a JWT token |
db.invalidate() | Invalidates the current authentication |
Signing in as a system user
System users are defined with the DEFINE USER statement and have access at the root, namespace, or database level. Use the corresponding credential class to sign in.
Signing in as a record user
Record users authenticate against a DEFINE ACCESS method defined on a database. Use RecordCredential with the access method name and any parameters required by the access definition.
Signing up a record user
The .signup() method registers a new record user through a record access method and returns a Token. Signup is only available for record-level access.
Using authentication tokens
The .signin() and .signup() methods return a Token object. Use .getAccess() to retrieve the JWT access token and .getRefresh() to retrieve the optional refresh token. You can store these tokens and use them later to re-authenticate without credentials.
Authenticating with a bearer token
If you have a bearer key — for example, one defined with a bearer access method — use BearerCredential to authenticate.
Invalidating authentication
The .invalidate() method clears the authentication state for the current connection. After invalidation, subsequent operations execute as an unauthenticated user.
Learn more
Surreal API reference for method signatures
Java Types reference for credential class details
Connecting to SurrealDB for connection setup
DEFINE USER for configuring system users
DEFINE ACCESS for configuring record access
Security best practices for token management
SurrealDB authentication overview for system users, record users, and token concepts