diff --git a/README.md b/README.md index e950178..ff3b489 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,8 @@ casbin: #The jdbc adapter will actively look for the data source information you configured in spring.datasource #Default use jdbc, and use the built-in h2 database for memory storage storeType: jdbc + #Customized policy table name when use jdbc, casbin_rule as default. + tableName: casbin_rule #Data source initialization policy [create (automatically create data table, no longer initialized if created), never (always do not initialize)] initializeSchema: create #Local model configuration file address, the default reading location: classpath: casbin/model.conf diff --git a/README_CN.md b/README_CN.md index 8745a85..cfc2537 100644 --- a/README_CN.md +++ b/README_CN.md @@ -60,6 +60,8 @@ casbin: #jdbc适配器将主动寻找您在spring.datasource配置的数据源信息 #默认使用jdbc,并使用内置h2数据库进行内存存储 storeType: jdbc + #当使用jdbc时,定制化数据库表名,默认表名是casbin_rule + tableName: casbin_rule #数据源初始化策略[create(自动创建数据表,如已创建则不再进行初始化),never(始终不进行初始化)] initializeSchema: create #本地模型配置文件地址,约定默认读取位置:classpath:casbin/model.conf diff --git a/src/main/java/org/casbin/adapter/DB2Adapter.java b/src/main/java/org/casbin/adapter/DB2Adapter.java index fe5afea..5ca9447 100644 --- a/src/main/java/org/casbin/adapter/DB2Adapter.java +++ b/src/main/java/org/casbin/adapter/DB2Adapter.java @@ -11,11 +11,11 @@ */ public class DB2Adapter extends JdbcAdapter { - private static final String CHECK_TABLE_SQL = "select 1 from syscat.tables where tabname = upper('CASBIN_RULE')"; + private static final String CHECK_TABLE_SQL = "select 1 from syscat.tables where tabname = upper('casbin_rule')"; - private static final String DROP_TABLE_SQL = "DROP TABLE CASBIN_RULE"; + private static final String DROP_TABLE_SQL = "DROP TABLE casbin_rule"; - private static final String INIT_TABLE_SQL = "CREATE TABLE CASBIN_RULE (" + + private static final String INIT_TABLE_SQL = "CREATE TABLE casbin_rule (" + " ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1) ,"+ " PTYPE VARCHAR(255) NOT NULL ," + " V0 VARCHAR(255) DEFAULT NULL ," + @@ -34,24 +34,32 @@ public class DB2Adapter extends JdbcAdapter { public DB2Adapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) { super(jdbcTemplate, casbinExceptionProperties, autoCreateTable); } + + public DB2Adapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) { + super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable); + } @Override protected void initTable() { try { - jdbcTemplate.queryForObject(CHECK_TABLE_SQL, Integer.class); + jdbcTemplate.queryForObject(getCheckTableSql(), Integer.class); } catch (EmptyResultDataAccessException e) { super.initTable(); } } + + protected String getCheckTableSql() { + return renderActualSql(CHECK_TABLE_SQL); + } @Override protected String getInitTableSql() { - return INIT_TABLE_SQL; + return renderActualSql(INIT_TABLE_SQL); } @Override protected String getDropTableSql() { - return DROP_TABLE_SQL; + return renderActualSql(DROP_TABLE_SQL); } } diff --git a/src/main/java/org/casbin/adapter/JdbcAdapter.java b/src/main/java/org/casbin/adapter/JdbcAdapter.java index ca14683..481e7ed 100644 --- a/src/main/java/org/casbin/adapter/JdbcAdapter.java +++ b/src/main/java/org/casbin/adapter/JdbcAdapter.java @@ -1,5 +1,13 @@ package org.casbin.adapter; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + import org.casbin.exception.CasbinAdapterException; import org.casbin.jcasbin.model.Model; import org.casbin.jcasbin.persist.FilteredAdapter; @@ -11,11 +19,6 @@ import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.annotation.Transactional; -import java.sql.PreparedStatement; -import java.sql.SQLException; -import java.util.*; -import java.util.stream.Collectors; - /** * @author shy * @version V1.1 @@ -26,9 +29,13 @@ */ public class JdbcAdapter implements FilteredAdapter { - private final static Logger logger = LoggerFactory.getLogger(JdbcAdapter.class); + private static final Logger logger = LoggerFactory.getLogger(JdbcAdapter.class); + + public static final String DEFAULT_TABLE_NAME = "casbin_rule"; + + private String tableName; - private final static String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" + + private static final String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" + " id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "+ " ptype varchar(255) NOT NULL," + " v0 varchar(255) DEFAULT NULL," + @@ -38,11 +45,11 @@ public class JdbcAdapter implements FilteredAdapter { " v4 varchar(255) DEFAULT NULL," + " v5 varchar(255) DEFAULT NULL" + ")"; - private final static String DROP_TABLE_SQL = "DROP TABLE IF EXISTS casbin_rule"; - private final static String DELETE_TABLE_CONTENT_SQL = "DELETE FROM casbin_rule"; - private final static String LOAD_POLICY_SQL = "SELECT * FROM casbin_rule"; - private final static String INSERT_POLICY_SQL = "INSERT INTO casbin_rule(ptype, v0, v1, v2, v3, v4, v5) VALUES(?, ?, ?, ?, ?, ?, ?)"; - private final static String DELETE_POLICY_SQL = "DELETE FROM casbin_rule WHERE ptype = ? "; + private static final String DROP_TABLE_SQL = "DROP TABLE IF EXISTS casbin_rule"; + private static final String DELETE_TABLE_CONTENT_SQL = "DELETE FROM casbin_rule"; + private static final String LOAD_POLICY_SQL = "SELECT * FROM casbin_rule"; + private static final String INSERT_POLICY_SQL = "INSERT INTO casbin_rule(ptype, v0, v1, v2, v3, v4, v5) VALUES(?, ?, ?, ?, ?, ?, ?)"; + private static final String DELETE_POLICY_SQL = "DELETE FROM casbin_rule WHERE ptype = ? "; protected JdbcTemplate jdbcTemplate; protected CasbinExceptionProperties casbinExceptionProperties; @@ -50,7 +57,12 @@ public class JdbcAdapter implements FilteredAdapter { private volatile boolean isFiltered = true; public JdbcAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) { + this(jdbcTemplate,casbinExceptionProperties,DEFAULT_TABLE_NAME,autoCreateTable); + } + + public JdbcAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) { this.jdbcTemplate = jdbcTemplate; + this.tableName = tableName; this.casbinExceptionProperties = casbinExceptionProperties; if (autoCreateTable) { initTable(); @@ -65,24 +77,35 @@ public static class Filter { public String[] p; public String[] g; } - - + + protected String renderActualSql(String sql) { + return sql.replace(DEFAULT_TABLE_NAME, tableName); + } + protected String getInitTableSql() { - return INIT_TABLE_SQL; + return renderActualSql(INIT_TABLE_SQL); } protected String getDropTableSql() { - return DROP_TABLE_SQL; + return renderActualSql(DROP_TABLE_SQL); } protected String getLoadPolicySql() { - return LOAD_POLICY_SQL; + return renderActualSql(LOAD_POLICY_SQL); } protected String getDeleteTableContentSql() { - return DELETE_TABLE_CONTENT_SQL; + return renderActualSql(DELETE_TABLE_CONTENT_SQL); } - + + protected String getInsertPolicySql() { + return renderActualSql(INSERT_POLICY_SQL); + } + + protected String getDeletePolicySql() { + return renderActualSql(DELETE_POLICY_SQL); + } + /** * Initialize the table structure */ @@ -145,7 +168,7 @@ public void savePolicy(Model model) { deleteTableContent(); List casbinRules = CasbinRule.transformToCasbinRule(model); int[] rows = jdbcTemplate.batchUpdate( - INSERT_POLICY_SQL, + getInsertPolicySql(), new BatchPreparedStatementSetter() { @Override @@ -190,7 +213,7 @@ public void addPolicy(String sec, String ptype, List rule) { for (int i = 0; i < 6 - rule.size(); i++) { rules.add(null); } - int rows = jdbcTemplate.update(INSERT_POLICY_SQL, rules.toArray()); + int rows = jdbcTemplate.update(getInsertPolicySql(), rules.toArray()); if (rows != 1) { throw new CasbinAdapterException(String.format("Add policy error, add %d rows, expect %d rows", rows, 1)); } @@ -229,7 +252,7 @@ public void removeFilteredPolicy(String sec, String ptype, int fieldIndex, Strin } List params = new ArrayList<>(Arrays.asList(fieldValues)); params.add(0, ptype); - String delSql = DELETE_POLICY_SQL; + String delSql = getDeletePolicySql(); int columnIndex = fieldIndex; for (int i = 0; i < fieldValues.length; i++) { delSql = String.format("%s%s%s%s", delSql, " AND v", columnIndex, " = ? "); diff --git a/src/main/java/org/casbin/adapter/OracleAdapter.java b/src/main/java/org/casbin/adapter/OracleAdapter.java index dcef62f..4c6791c 100644 --- a/src/main/java/org/casbin/adapter/OracleAdapter.java +++ b/src/main/java/org/casbin/adapter/OracleAdapter.java @@ -13,7 +13,7 @@ */ public class OracleAdapter extends JdbcAdapter { - private final static String INIT_TABLE_SQL = "CREATE TABLE CASBIN_RULE (" + + private static final String INIT_TABLE_SQL = "CREATE TABLE casbin_rule (" + " ID int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "+ " PTYPE VARCHAR2(255) NOT NULL ," + " V0 VARCHAR2(255) DEFAULT NULL ," + @@ -23,28 +23,36 @@ public class OracleAdapter extends JdbcAdapter { " V4 VARCHAR2(255) DEFAULT NULL ," + " V5 VARCHAR2(255) DEFAULT NULL" + ")"; - private final static String DROP_TABLE_SQL = "DROP TABLE CASBIN_RULE"; - private final static String CHECK_TABLE_EXIST = "SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = UPPER('CASBIN_RULE')"; + private static final String DROP_TABLE_SQL = "DROP TABLE casbin_rule"; + private static final String CHECK_TABLE_EXIST = "SELECT COUNT(*) FROM USER_TABLES WHERE TABLE_NAME = UPPER('casbin_rule')"; public OracleAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) { super(jdbcTemplate, casbinExceptionProperties, autoCreateTable); } + public OracleAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) { + super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable); + } + @Override protected void initTable() { - Integer count = jdbcTemplate.queryForObject(CHECK_TABLE_EXIST, Integer.class); + Integer count = jdbcTemplate.queryForObject(getCheckTableExistSql(), Integer.class); if (count != null && count == 0) { super.initTable(); } } + + protected String getCheckTableExistSql() { + return renderActualSql(CHECK_TABLE_EXIST); + } @Override protected String getInitTableSql() { - return INIT_TABLE_SQL; + return renderActualSql(INIT_TABLE_SQL); } @Override protected String getDropTableSql() { - return DROP_TABLE_SQL; + return renderActualSql(DROP_TABLE_SQL); } } diff --git a/src/main/java/org/casbin/adapter/PostgreSQLAdapter.java b/src/main/java/org/casbin/adapter/PostgreSQLAdapter.java index 07d8e7a..1f23e2c 100644 --- a/src/main/java/org/casbin/adapter/PostgreSQLAdapter.java +++ b/src/main/java/org/casbin/adapter/PostgreSQLAdapter.java @@ -13,7 +13,7 @@ */ public class PostgreSQLAdapter extends JdbcAdapter { - private final static String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS CASBIN_RULE (" + + private static final String INIT_TABLE_SQL = "CREATE TABLE IF NOT EXISTS casbin_rule (" + " id SERIAL PRIMARY KEY, "+ " ptype varchar(255) NOT NULL," + " v0 varchar(255) DEFAULT NULL," + @@ -27,9 +27,13 @@ public class PostgreSQLAdapter extends JdbcAdapter { public PostgreSQLAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, boolean autoCreateTable) { super(jdbcTemplate, casbinExceptionProperties, autoCreateTable); } + + public PostgreSQLAdapter(JdbcTemplate jdbcTemplate, CasbinExceptionProperties casbinExceptionProperties, String tableName, boolean autoCreateTable) { + super(jdbcTemplate, casbinExceptionProperties, tableName, autoCreateTable); + } @Override protected String getInitTableSql() { - return INIT_TABLE_SQL; + return renderActualSql(INIT_TABLE_SQL); } } \ No newline at end of file diff --git a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java index 532034f..c4e24b6 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java @@ -52,7 +52,7 @@ @ConditionalOnExpression("${casbin.enableCasbin:true}") public class CasbinAutoConfiguration { - private final static Logger logger = LoggerFactory.getLogger(CasbinAutoConfiguration.class); + private static final Logger logger = LoggerFactory.getLogger(CasbinAutoConfiguration.class); /** * Automatic configuration file storage adapter @@ -89,17 +89,18 @@ public Adapter autoConfigJdbcAdapter( String databaseName = getDatabaseName(jdbcTemplateToUse.getDataSource()); CasbinDataSourceInitializationMode initializeSchema = properties.getInitializeSchema(); boolean autoCreateTable = initializeSchema == CasbinDataSourceInitializationMode.CREATE; + String tableName = properties.getTableName(); logger.info("Casbin current use database product: {}", databaseName); switch (databaseName) { case "mysql": case "h2": - return new JdbcAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable); + return new JdbcAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable); case "postgresql": - return new PostgreSQLAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable); + return new PostgreSQLAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable); case "oracle": - return new OracleAdapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable); + return new OracleAdapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable); case "db2": - return new DB2Adapter(jdbcTemplateToUse, exceptionProperties, autoCreateTable); + return new DB2Adapter(jdbcTemplateToUse, exceptionProperties, tableName, autoCreateTable); default: throw new CasbinAdapterException("Can't find " + databaseName + " jdbc adapter"); } diff --git a/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java b/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java index 0d9429d..b754db6 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java @@ -17,6 +17,10 @@ */ @ConfigurationProperties("casbin") public class CasbinProperties { + /** + * Default table name when storage strategy is JDBC + */ + private String tableName = "casbin_rule"; /** * Enable Casbin */ @@ -157,5 +161,13 @@ public boolean isUseDefaultModelIfModelNotSetting() { public void setUseDefaultModelIfModelNotSetting(boolean useDefaultModelIfModelNotSetting) { this.useDefaultModelIfModelNotSetting = useDefaultModelIfModelNotSetting; } + + public String getTableName() { + return tableName; + } + + public void setTableName(String tableName) { + this.tableName = tableName; + } }