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
For Maven:
<dependency>
<groupId>org.casbin</groupId>
<artifactId>jdbc-adapter</artifactId>
<version>1.1.2-FIX</version>
</dependency>
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();
}
}
This project is under Apache 2.0 License. See the LICENSE file for the full license text.