JDBC adapter for Casbin

Clone this repo:
  1. 0b24efa build: prepare Apache release with tag-driven version by Yang Luo · 3 weeks ago master
  2. 989a593 feat(close-connections): Add try-with-resources for connections (#94) by Nipun Sharma · 10 weeks ago
  3. 4950c38 feat: update repo URLs by Yang Luo · 9 weeks ago
  4. 705a540 feat: add NOTICE and DISCLAIMER by Yang Luo · 9 weeks ago
  5. 7d71287 feat: replace semantic-release with tag-triggered manual release by Yang Luo · 9 weeks ago

JDBC Adapter

build codecov Javadocs Maven Central Discord

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 currently 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>LATEST</version>
</dependency>

Notice:

Since version 2.0.2, jdbc-adapter adds an id field to the database table structure by default.

If you want to upgrade to 2.0.2 - 2.2.0, you have to add the id field manually. It is recommended to add the id field. If you don't want to add the id field, please use 2.2.1+.

Oracle driver

The Oracle JDBC driver is not redistributable, so this adapter does not pull it in as a dependency. Oracle support in the adapter itself is unchanged: applications using Oracle obtain a driver matching their Java and database versions from Oracle JDBC downloads, review its terms, and add it to their own runtime classpath. Then pass the configured javax.sql.DataSource to new JDBCAdapter(dataSource), or use the driver-name/URL constructor. Applications using other databases need no extra driver, and the automated MySQL/PostgreSQL/SQL Server tests do not need Oracle.

Simple Example

package com.company.test;

import org.casbin.adapter.JDBCAdapter;
import org.casbin.jcasbin.main.Enforcer;
import com.mysql.cj.jdbc.MysqlDataSource;

public class Test {
    public static void main() {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/db_name";
        String username = "root";
        String password = "123456";

        // The adapter will use the table named "casbin_rule".
        // Use driver, url, username and password to initialize a JDBC adapter.
        JDBCAdapter a = new JDBCAdapter(driver, url, username, password); 
        
        // Recommend use DataSource to initialize a JDBC adapter.
        // Implementer of DataSource interface, such as hikari, c3p0, durid, etc.
        MysqlDataSource dataSource = new MysqlDataSource();
        dataSource.setURL(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);

        a = JDBCAdapter(dataSource);        

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

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

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

        // Save the policy back to DB.
        e.savePolicy();
        // Close the connection.
        a.close();
    }
}

Getting Help

Incubation status

Apache Casbin (Incubating) is an effort undergoing incubation at the Apache Software Foundation (ASF), sponsored by the Apache Incubator PMC.

Incubation is required of all newly accepted projects until a further review indicates that the infrastructure, communications, and decision making process have stabilized in a manner consistent with other successful ASF projects.

While incubation status is not necessarily a reflection of the completeness or stability of the code, it does indicate that the project has yet to be fully endorsed by the ASF.

License

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