's
built-in authentication mechanism is suitable only for development and testing
purposes. It is strongly recommended that production systems rely on LDAP or a
user-defined class for authentication. It is also strongly recommended that
production systems protect network connections with SSL/TLS.
This example is a single long program. A similar example that uses the client
driver, in
,
uses one program to set properties and a second program to perform database
operations. Either example would work equally well in the other format.
See
for an example similar to this one that uses SQL authorization.
The program does the following:
- Starts and creates
a database named authEmbDB, using the embedded driver.
- Sets database properties that create users with different levels of access
(read-only and full access), require authentication, and set the default access
level to no access.
- Closes the connection, then stops and restarts the database so that the
authentication changes can take effect.
- Tries to connect to the database without a username and password, raising
an exception.
- Connects to the database as a user with read-only access; the connection
succeeds, but an attempt to create a table raises an exception.
- Connects to the database as a user with full access; this user can create
and populate a table.
- Deletes the table.
- Closes the connection, shuts down the database, then shuts down
.
Make sure that the javac command is in your path, then
compile the program as follows:
javac AuthExampleEmbedded.java
When you run AuthExampleEmbedded, make
sure that %DERBY_HOME%\lib\derby.jar (or
$DERBY_HOME/lib/derby.jar) is in your classpath. For example,
you might use the following command on a UNIX system:
java -cp .:${DERBY_HOME}/lib/derby.jar AuthExampleEmbedded