Fork me on GitHub

Relational database (v5.6)

pac4j allows you to validate username/password and create, update and delete users on a SQL database.

1) Dependency

You need to use the following module: pac4j-sql.

Example (Maven dependency):

<dependency>
    <groupId>org.pac4j</groupId>
    <artifactId>pac4j-sql</artifactId>
    <version>${pac4j.version}</version>
</dependency>

2) DbProfileService

The DbProfileService allows you to:

It works with a DbProfile.

It is built from a javax.sql.DataSource.

Example:

DataSource dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test", dbuser, dbpwd);
DbProfileService dbProfileService = new DbProfileService(dataSource);

The users table in the database must be created with the following script:

CREATE TABLE users
(
  id varchar(255),
  username varchar(255),
  password varchar(255),
  linkedid varchar(255),
  serializedprofile varchar(10000)
);

ALTER TABLE users
	ADD PRIMARY KEY (id),
	ADD KEY username (username),
	ADD KEY linkedid (linkedid);

The name of the table in the database can be changed via the setUsersTable method. As well as the id, username and password columns using the setIdAttribute, setUsernameAttribute and setPasswordAttribute methods.

The attributes of the user profile can be managed in the database in two ways:

This DbProfileService supports the use of a specific PasswordEncoder to encode the passwords in the database.

Starting with v3.9.0 in the 3.x stream, v4.2.0 in the 4.x stream and v5.0, the serializedprofile is written in JSON instead of using the Java serialization.