Skip to content

Commit

Permalink
update jcasbin
Browse files Browse the repository at this point in the history
support SpringBoot 1.5.9
support SyncedEnforcer config
  • Loading branch information
fangzhengjin committed Jul 1, 2019
1 parent 95bf42d commit b506c54
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public class Test{
casbin:
  #Whether to enable Casbin, it is enabled by default.
  enableCasbin: true
#Whether to use thread-synchronized Enforcer, default false
casbin.useSyncedEnforcer: false
  #Whether to enable automatic policy saving, if the adapter supports this function, it is enabled by default.
  autoSave: true
  #Storage type [file, jdbc], currently supported jdbc database [mysql (mariadb), h2, oracle, postgresql]
Expand Down
2 changes: 2 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class Test{
casbin:
#是否开启Casbin,默认开启
enableCasbin: true
#是否使用线程同步的Enforcer,默认false
casbin.useSyncedEnforcer: false
#是否开启策略自动保存,如适配器支持该功能,默认开启
autoSave: true
#存储类型[file,jdbc],目前支持的jdbc数据库[mysql(mariadb),h2,oracle,postgresql]
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ configurations {
}

dependencies {
compile 'org.casbin:jcasbin:1.3.0'
compile 'org.casbin:jcasbin:1.4.0'
compile 'com.h2database:h2'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'

Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BINTRAY_SUBJECT=casbin
#GAV坐标
GROUP=org.casbin
ARTIFACT_ID=casbin-spring-boot-starter
VERSION_CODE=0.0.7
VERSION_CODE=0.0.8
#项目地址相关
GITHUB_REPO=jcasbin/casbin-spring-boot-starter
POM_URL=https://github.com/jcasbin/casbin-spring-boot-starter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.casbin.spring.boot.autoconfigure;

import javax.sql.DataSource;

import lombok.extern.slf4j.Slf4j;
import org.casbin.adapter.DB2Adapter;
import org.casbin.adapter.JdbcAdapter;
import org.casbin.adapter.OracleAdapter;
import org.casbin.exception.CasbinAdapterException;
import org.casbin.exception.CasbinModelConfigNotFoundException;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.main.SyncedEnforcer;
import org.casbin.jcasbin.model.Model;
import org.casbin.jcasbin.persist.Adapter;
import org.casbin.jcasbin.persist.Watcher;
import org.casbin.jcasbin.persist.file_adapter.FileAdapter;
import org.casbin.spring.boot.autoconfigure.properties.CasbinDataSourceInitializationMode;
import org.casbin.spring.boot.autoconfigure.properties.CasbinProperties;
Expand All @@ -27,10 +26,9 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.JdbcUtils;
import org.springframework.jdbc.support.MetaDataAccessException;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import lombok.extern.slf4j.Slf4j;
import javax.sql.DataSource;

/**
* @author fangzhengjin
Expand All @@ -44,7 +42,7 @@
@Slf4j
@Configuration
@EnableConfigurationProperties(CasbinProperties.class)
@AutoConfigureAfter({JdbcTemplateAutoConfiguration.class, CasbinRedisWatcherAutoConfiguration.class})
@AutoConfigureAfter({JdbcTemplateAutoConfiguration.class})
@ConditionalOnExpression("${casbin.enableCasbin:true}")
public class CasbinAutoConfiguration {

Expand Down Expand Up @@ -95,7 +93,7 @@ public Adapter autoConfigJdbcAdapter(JdbcTemplate jdbcTemplate, CasbinProperties
@Bean
@ConditionalOnMissingBean
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public Enforcer enforcer(CasbinProperties properties, Adapter adapter, @Nullable Watcher watcher) {
public Enforcer enforcer(CasbinProperties properties, Adapter adapter) {
Model model = new Model();
try {
String modelRealPath = properties.getModelRealPath();
Expand All @@ -117,12 +115,14 @@ public Enforcer enforcer(CasbinProperties properties, Adapter adapter, @Nullable
// matchers
model.addDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act");
}
Enforcer enforcer = new Enforcer(model, adapter);
enforcer.enableAutoSave(properties.isAutoSave());
if (watcher != null) {
logger.info("Casbin set watcher: {}", watcher.getClass().getName());
enforcer.setWatcher(watcher);
Enforcer enforcer;
if (properties.isUseSyncedEnforcer()) {
enforcer = new SyncedEnforcer(model, adapter);
logger.info("Casbin use SyncedEnforcer");
} else {
enforcer = new Enforcer(model, adapter);
}
enforcer.enableAutoSave(properties.isAutoSave());
return enforcer;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.casbin.spring.boot.autoconfigure;

import lombok.extern.slf4j.Slf4j;
import org.casbin.jcasbin.main.Enforcer;
import org.casbin.jcasbin.persist.Watcher;
import org.casbin.spring.boot.autoconfigure.properties.CasbinProperties;
import org.casbin.watcher.RedisWatcher;
Expand Down Expand Up @@ -31,8 +32,8 @@
@Slf4j
@Configuration
@EnableConfigurationProperties(CasbinProperties.class)
@AutoConfigureAfter({RedisAutoConfiguration.class})
@ConditionalOnExpression("'jdbc'.equalsIgnoreCase('${casbin.storeType}') && ${casbin.enableWatcher:true} && 'redis'.equalsIgnoreCase('${casbin.watcherType}') ")
@AutoConfigureAfter({RedisAutoConfiguration.class, CasbinAutoConfiguration.class})
@ConditionalOnExpression("'jdbc'.equalsIgnoreCase('${casbin.storeType:jdbc}') && ${casbin.enableWatcher:false} && 'redis'.equalsIgnoreCase('${casbin.watcherType:redis}') ")
public class CasbinRedisWatcherAutoConfiguration {

public final static String CASBIN_POLICY_TOPIC = "CASBIN_POLICY_TOPIC";
Expand All @@ -41,8 +42,11 @@ public class CasbinRedisWatcherAutoConfiguration {
@ConditionalOnBean(RedisTemplate.class)
@ConditionalOnMissingBean
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public Watcher redisWatcher(StringRedisTemplate stringRedisTemplate) {
return new RedisWatcher(stringRedisTemplate);
public Watcher redisWatcher(StringRedisTemplate stringRedisTemplate, Enforcer enforcer) {
RedisWatcher watcher = new RedisWatcher(stringRedisTemplate);
enforcer.setWatcher(watcher);
logger.info("Casbin set watcher: {}", watcher.getClass().getName());
return watcher;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public class CasbinProperties {
* 启用Casbin
*/
private boolean enableCasbin = true;
/**
* 是否使用同步的Enforcer
*/
private boolean useSyncedEnforcer = false;
/**
* 本地model文件
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public enum CasbinWatcherType {
/**
* 使用Redis通知同步策略
*/
REDIS
REDIS,
JMS
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"sourceType": "org.casbin.spring.boot.autoconfigure.properties.CasbinProperties",
"defaultValue": true
},
{
"name": "casbin.useSyncedEnforcer",
"type": "java.lang.Boolean",
"description": "use Synced Enforcer",
"sourceType": "org.casbin.spring.boot.autoconfigure.properties.CasbinProperties",
"defaultValue": false
},
{
"name": "casbin.model",
"type": "java.lang.String",
Expand Down

0 comments on commit b506c54

Please sign in to comment.