PicketLink example with database

PicketLink is a authentication framework for Java EE.

I created a PicketLink example application using database. This application is for user management.

picketlink-database-example

Environment
Windows 7
JDK 1.8.0
Eclipse Mars.1
JBoss Tools 4.3.0.Final

Server
WildFly 9.0.1.Final
MySQL Community Server 5.6.27

Library
PicketLink 2.7.0.Final
PrimeFaces 5.3

Example code
https://github.com/subsonicsystems/picketlink-database-example

Advance preparation – MySQL
Create a schema that named picketlink_database_example. Secondly create a database user and grant this schema access for this user.

Database tables will be created automatically when the application is deployed.

Advance preparation – WildFly
Set database driver at WildFly.

Secondly set datasource. A setting example is as follows.

WildFly installation folder\standalone\configuration\standalone.xml

<datasource jndi-name="java:jboss/datasources/picketlink-database-example-ds" pool-name="picketlink-database-example-ds" enabled="true">
    <connection-url>jdbc:mysql://localhost:3306/picketlink_database_example</connection-url>
    <driver>mysql</driver>
    <security>
        <user-name>admin</user-name>
        <password>admin</password>
    </security>
    <validation>
        <valid-connection-checker class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLValidConnectionChecker"/>
        <background-validation>true</background-validation>
        <background-validation-millis>60000</background-validation-millis>
        <exception-sorter class-name="org.jboss.jca.adapters.jdbc.extensions.mysql.MySQLExceptionSorter"/>
    </validation>
</datasource>

Library
This application uses Apache Maven.

Libraries are as follows.

PicketLink
picketlink-2.7.0.Final.jar
picketlink-idm-simple-schema-2.7.0.Final.jar

PrimeFaces
primefaces-5.3.jar
bootstrap-1.0.10.jar

Apache Commons Lang
commons-lang3-3.4.jar

persistence.xml
/WEB-INF/classes/META-INF/persistence.xml is as follows. Change jta-data-source as to your environment.

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
             xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="primary">
        <jta-data-source>java:jboss/datasources/picketlink-database-example-ds</jta-data-source>
        <class>org.picketlink.idm.jpa.model.sample.simple.AttributedTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.AccountTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.RoleTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.GroupTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.IdentityTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.RelationshipTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.RelationshipIdentityTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.PartitionTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.PasswordCredentialTypeEntity</class>
        <class>org.picketlink.idm.jpa.model.sample.simple.AttributeTypeEntity</class>
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create"/>
        </properties>
    </persistence-unit>
</persistence>

When the application is deployed, database tables will be created automatically beacuse property of hibernate.hbm2ddl.auto is set create.

As this configuration causes database data erasing when WildFly is restarted, after database tables are created, comment out this property.