Fork me on GitHub

MongoDB (v6.1)

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-mongodb.

Example (Maven dependency):

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

2) MongoProfileService

The MongoProfileService supersedes the deprecated MongoAuthenticator 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.