Database as a Data Source for Authentication

A database table is used as data source for the access data.

  • Name of the database table - The table name can be chosen freely, usually USER.
  • Required fields - The following data fields are obligatory: 

    Required fieldsData typeExplanation
    USERNAMEStringSaves the username   as a string.
    PASSWORDbyte[]Saves the password as a byte array, usually encrypted.
  • Data structure generated by RapidClipse 

    • Entities

      AttributeData typeExplanation
      usernameStringSaves the username as a string.
      passwordbyte[]Saves the password as a byte array, usually encrypted.
    • Data Access

      • UserDAO

      • RoleDAO

      • ResourceDAO

  • Additional data fields - If required, the USER table can also include other data fields, since they are not relevant for authentication. Frequently used information about a user include e-mail, status (enabled or disabled), image, time zone, last session, IP address, URL for a log file, etc.
  • Dealing with an existing  USER table -  If you already have a table for managing users in your database, make sure that there is an appropriate entity with corresponding DAO in your Project Management in Entities or Data Access. If not, you can generate the missing entity and DAO with the Create JPA entities from table import function. Different table and field names are no problem because the mapping is performed later. 

  1. User Entity already exists - Select your existing User entity.
    No User Entity available yet - Click New Entity... to create a new User entity, including the UserDAO DAO. 
  2. Select the attribute for the username under Mapping > Username.
  3. Select the attribute for the password under Mapping > Password.
  4. Select the encryption algorithm for the password under Settings > Password hashing strategy, e.g. PBKDF2WithHmacSHA1.
  5. Click Finish.

Go to authorize

Create database table USER - A corresponding table has to be created for the new User entity in the USER database. For this, RapidClipse offers an export feature.

  • Mapping

    • Username - Selection of the attribute for the user name. 

    • Password - Selection of the attribute for the password. 

  • Password hashing strategy
    • MD5 - The message digest algorithm is a hash function that creates a 128-bit hash value from a password. However, this is no longer considered secure.

      // Example RapidClipse
    • SHA1 - The Secure Hash Algorithm 1 is a hash function that creates a 160-bit hash value from a password. 

      // Example RapidClipse
    • SHA2 - The Secure Hash Algorithm 2 is the currently recommended standard for SHA that creates a 224-, 256-, 384- or 512-bit hash value from a password.

      // Example RapidClipse
    • PBKDF2WithHmacSHA1 - Combination of PBKDF2, HMAC and SHA1 which creates a 160-bit hash value. PBKDF2 (Password-Based Key Derivation Function 2) is a standardized function for deriving a key from a password and is often used for password-based authentication. HMAC (Keyed-Hash Message Authentication Code) is a Message Authentication Code (MAC), the construction of which is based on a cryptographic hash function. SHA1 - is a hash function that creates a password from a 160-bit hash value.

      // Example RapidClipse
  • Project Management > Entities - The entity class is created with the attribute username of the type string and the attribute password of the type byte[] or by selecting an existing entity. 

    EntityAttributeData typeExplanation
    UserusernameStringSaves the username as a string.
    passwordbyte[]Saves the password, usually encoded as a byte array.
  • Project Management > Data Access - The DAO class is generated. When selecting existing entities no new DAO is generated. 

  • Project Management > Business Objects - The class is generated.

    public class ExampleAuthenticationProvider
    		implements AuthenticatorProvider<CredentialsUsernamePassword, CredentialsUsernamePassword> {
    	private static ExampleAuthenticationProvider INSTANCE;
    	public static ExampleAuthenticationProvider getInstance() {
    		if (INSTANCE == null) {
    			INSTANCE = new ExampleAuthenticationProvider();
    		return INSTANCE;
    	private JPAAuthenticator authenticator;
    	private ExampleAuthenticationProvider() {
    	public Authenticator<CredentialsUsernamePassword, CredentialsUsernamePassword> provideAuthenticator() {
    		if (this.authenticator == null) {
    			this.authenticator = new JPAAuthenticator(User.class);
    			this.authenticator.setHashStrategy(new PBKDF2WithHmacSHA1());
    		return this.authenticator;
  • Save encrypted password

    String password = this.passwordField.getValue();
    byte[] encryptedPassword = new HashStrategy.SHA2().hashPassword(pw.getBytes());
    User user = new User();
    try {
    	new UserDAO().save(user);
    } catch (Exception e) {
    	// TODO: handle exception
  • Save password and edit -  Forms are commonly used for both storing and editing data. However, for storing and editing passwords, you need to create different forms. When saving, the password is entered into the database table encrypted so that you will always receive an encrypted password during reading access. Re-saving would encrypt the already encrypted password again and thus it would be invalid.
  • Enter passwords in the database manually