Skip to content

6 Integrating JPA Security

arne edited this page May 15, 2020 · 1 revision

In order to add access-control to our application we first need to integrate JPA Security. Integrating JPA Security is as simple as configuring the persistence provider:

<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    version="1.0">

  <persistence-unit name="contacts" transaction-type="RESOURCE_LOCAL">

    <provider>org.jpasecurity.persistence.SecurePersistenceProvider</provider>

    <class>org.jpasecurity.contacts.model.User</class>
    <class>org.jpasecurity.contacts.model.Contact</class>

    <properties>
      <property name="org.jpasecurity.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider" />
      <property name="org.jpasecurity.security.context" value="org.jpasecurity.security.authentication.StaticSecurityContext"/>
      <property name="hibernate.hbm2ddl.auto" value="create-drop" />
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
      <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver" />
      <property name="hibernate.connection.url" value="jdbc:hsqldb:mem:contacts" />
      <property name="hibernate.connection.username" value="sa" />
      <property name="hibernate.connection.password" value="" />
    </properties>

  </persistence-unit>

</persistence>

The following line has changed in order to use JPA Security:

<provider>org.jpasecurity.persistence.SecurePersistenceProvider</provider>

The following lines are added to specify the persistence provider:

<property name="org.jpasecurity.persistence.provider" value="org.hibernate.jpa.HibernatePersistenceProvider" />
<property name="org.jpasecurity.security.context" value="org.jpasecurity.security.authentication.StaticSecurityContext"/>

OK, let's run our program again and let's see what happens...

As you can see from the output: nothing happend, everything stayed as before. JPA Security smoothly integrated into our application, we even can't see it work. We have not defined access rules for now.

Clone this wiki locally