Skip to content
JDBC adapter for Casbin
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples
src
.gitignore
.travis.yml
LICENSE
README.md
pom.xml

README.md

JDBC Adapter

codebeat badge Build Status Coverage Status Javadocs Maven Central Gitter

JDBC Adapter is the JDBC adapter for jCasbin. With this library, jCasbin can load policy from JDBC supported database or save policy to it.

Based on Supported JDBC Drivers and Databases, The current supported databases are:

  • MySQL
  • Java DB
  • Oracle
  • PostgreSQL
  • DB2
  • Sybase
  • Microsoft SQL Server

Installation

For Maven:

<dependency>
  <groupId>org.casbin</groupId>
  <artifactId>jdbc-adapter</artifactId>
  <version>1.1.3</version>
</dependency>

Simple Example

package com.company.test;

import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.util.Util;
import org.casbin.adapter.JDBCAdapter;

public class Test {
    public static void main() {
        // Initialize a JDBC adapter and use it in a jCasbin enforcer:
        // The adapter will use the MySQL database named "casbin".
        // If it doesn't exist, the adapter will create it automatically.
        JDBCAdapter a = new JDBCAdapter("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/", "root", "123"); // Your driver and URL. 

        // Or you can use an existing DB "abc" like this:
        // The adapter will use the table named "casbin_rule".
        // If it doesn't exist, the adapter will create it automatically.
        // JDBCAdapter a = new JDBCAdapter("com.mysql.cj.jdbc.Driver", "jdbc:mysql://localhost:3306/casbin", "root", "123", true);

        Enforcer e = new Enforcer("examples/rbac_model.conf", a);

        // Load the policy from DB.
        e.loadPolicy();

        // Check the permission.
        e.enforce("alice", "data1", "read");

        // Modify the policy.
        // e.addPolicy(...);
        // e.removePolicy(...);

        // Save the policy back to DB.
        e.savePolicy();
    }
}

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.

You can’t perform that action at this time.