Fork me on GitHub

MongoDB (v4.5)

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

1) Dependency

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

Example (Maven dependency):

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

2) MongoProfileService

The MongoProfileService allows you to:

It works with a MongoProfile.

It is built from a com.mongodb.MongoClient.

Example:

MongoClient mongoClient = new MongoClient(server, port);
MongoProfileService mongoProfileService = new MongoProfileService(mongoClient);

The users are managed in a users database in a users collection, but both can be changed via the setUsersDatabase(String) and setUsersCollection(String) methods. As well as the id, username and password attribute names using the setIdAttribute, setUsernameAttribute and setPasswordAttribute methods.

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

This MongoProfileService supports the use of a specific PasswordEncoder to encode the passwords in the MongoDB 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.