From dbfc31cc3074b27ca97a1278c9dcab838614608c Mon Sep 17 00:00:00 2001 From: Jay Patel <36803168+jay-babu@users.noreply.github.com> Date: Sat, 29 Jun 2024 21:42:14 -0400 Subject: [PATCH] feat: add DistributedEnforcer auto-configuration via useDistributedEnforcer (#102) --- README.md | 3 +++ .../boot/autoconfigure/CasbinAutoConfiguration.java | 6 +++++- .../autoconfigure/properties/CasbinProperties.java | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2928869..4de3583 100644 --- a/README.md +++ b/README.md @@ -52,6 +52,9 @@ casbin: enableCasbin: true #Whether to use thread-synchronized Enforcer, default false useSyncedEnforcer: false + #Whether to use distributed Enforcer, default false. + #If both useSyncedEnforcer and useDistributedEnforcer are set to true, useDistributedEnforcer will take effect. + useDistributedEnforcer: 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, db2] 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 710d5a4..04dfebb 100644 --- a/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java +++ b/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java @@ -4,6 +4,7 @@ import org.casbin.annotation.CasbinDataSource; import org.casbin.exception.CasbinAdapterException; import org.casbin.exception.CasbinModelConfigNotFoundException; +import org.casbin.jcasbin.main.DistributedEnforcer; import org.casbin.jcasbin.main.Enforcer; import org.casbin.jcasbin.main.SyncedEnforcer; import org.casbin.jcasbin.model.Model; @@ -147,7 +148,10 @@ public Enforcer enforcer(CasbinProperties properties, Adapter adapter) { model.addDef("m", "m", "g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act"); } Enforcer enforcer; - if (properties.isUseSyncedEnforcer()) { + if (properties.isUseDistributedEnforcer()) { + enforcer = new DistributedEnforcer(model, adapter); + logger.info("Casbin use DistributedEnforcer"); + } else if (properties.isUseSyncedEnforcer()) { enforcer = new SyncedEnforcer(model, adapter); logger.info("Casbin use SyncedEnforcer"); } else { 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 3aab9fd..69d9364 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 @@ -29,6 +29,10 @@ public class CasbinProperties { * Whether to use a synchronized Enforcer */ private boolean useSyncedEnforcer = false; + /** + * Whether to use a distributed Enforcer + */ + private boolean useDistributedEnforcer = false; /** * Local model file */ @@ -111,6 +115,14 @@ public void setUseSyncedEnforcer(boolean useSyncedEnforcer) { this.useSyncedEnforcer = useSyncedEnforcer; } + public boolean isUseDistributedEnforcer() { + return useDistributedEnforcer; + } + + public void setUseDistributedEnforcer(boolean useDistributedEnforcer) { + this.useDistributedEnforcer = useDistributedEnforcer; + } + public String getModel() { return model; }