From 94d735a1ca72119c7cce78714cd0c332e57b09b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E6=98=95?= Date: Tue, 12 Mar 2024 15:12:18 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E6=AD=A3=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E6=A3=80=E6=9F=A5=E6=B5=81=E6=B0=B4=E7=BA=BF=E8=BF=90=E8=A1=8C?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/application-local.yml | 10 +++++++--- config/application-test.yml | 18 ++++++++++------- .../resources => config}/casbin/model.conf | 0 .../resources => config}/casbin/policy.csv | 0 .../rbac/clean_database.sql | 0 .../main/resources => config}/rbac/data.sql | 0 .../main/resources => config}/rbac/schema.sql | 0 .../configure/CasbinConfigure.java | 20 +++++++++++++++++++ .../misc/CasbinAuthorizeUtils.java | 15 +++++++++++--- cs-database/pom.xml | 9 +++++++++ .../cf/cs/restful/CsRestfulApplication.java | 3 ++- 11 files changed, 61 insertions(+), 14 deletions(-) rename {cs-authentication/src/main/resources => config}/casbin/model.conf (100%) rename {cs-authentication/src/main/resources => config}/casbin/policy.csv (100%) rename {cs-database/src/main/resources => config}/rbac/clean_database.sql (100%) rename {cs-database/src/main/resources => config}/rbac/data.sql (100%) rename {cs-database/src/main/resources => config}/rbac/schema.sql (100%) create mode 100644 cs-authentication/src/main/java/com/cf/cs/authentication/configure/CasbinConfigure.java diff --git a/config/application-local.yml b/config/application-local.yml index c1fc27c..a36a4f8 100644 --- a/config/application-local.yml +++ b/config/application-local.yml @@ -54,8 +54,8 @@ spring : sql : init: encoding : utf8 - schema-locations: classpath:rbac/schema.sql - data-locations : classpath:rbac/data.sql + schema-locations: file:config/rbac/schema.sql + data-locations : file:config/rbac/data.sql # ALWAYS/EMBEDDED/NEVER mode : always enabled : true @@ -64,7 +64,7 @@ spring : #mybatis mybatis : mapper-locations : classpath*:mappers/*.xml - type-aliases-package: com.cmhi.gds.pojo.entry + type-aliases-package: com.cf.cs.database.pojo.entry configuration : default-enum-type-handler: com.cf.cs.database.common.DataBaseEnumHandler @@ -109,3 +109,7 @@ system : controller: enabled: false prefix : /dict + +casbin: + model-path: config/casbin/model.conf + policy-path: config/casbin/policy.csv \ No newline at end of file diff --git a/config/application-test.yml b/config/application-test.yml index bf9d592..8edb218 100644 --- a/config/application-test.yml +++ b/config/application-test.yml @@ -34,12 +34,12 @@ spring : datasource: url : jdbc:mysql://xajhuang.com:3307/common_framework?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true - #url : jdbc:mysql://localhost:3306/common_framework?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true - driver-class-name: com.mysql.cj.jdbc.Driver username : xajhuang password : wkj!nky5cjb!GMV6guq + #url : jdbc:mysql://localhost:3306/common_framework?serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=convertToNull&useUnicode=true #username : root #password : aaaHuang1 + driver-class-name: com.mysql.cj.jdbc.Driver dbcp2 : max-total : 128 max-wait-millis : 10000 @@ -54,8 +54,8 @@ spring : sql : init: encoding : utf8 - schema-locations: classpath:rbac/schema.sql - data-locations : classpath:rbac/data.sql + schema-locations: file:../config/rbac/schema.sql + data-locations : file:../config/rbac/data.sql # ALWAYS/EMBEDDED/NEVER mode : always enabled : true @@ -64,7 +64,7 @@ spring : #mybatis mybatis : mapper-locations : classpath*:mappers/*.xml - type-aliases-package: com.cmhi.gds.pojo.entry + type-aliases-package: com.cf.cs.database.pojo.entry configuration : default-enum-type-handler: com.cf.cs.database.common.DataBaseEnumHandler @@ -81,8 +81,8 @@ pagehelper : params.count : countSql #config log -# logging : -# config: file:config/logback.xml +logging : + config: file:../config/logback.xml log4j : logger: @@ -109,3 +109,7 @@ system : controller: enabled: false prefix : /dict + +casbin: + model-path: ../config/casbin/model.conf + policy-path: ../config/casbin/policy.csv \ No newline at end of file diff --git a/cs-authentication/src/main/resources/casbin/model.conf b/config/casbin/model.conf similarity index 100% rename from cs-authentication/src/main/resources/casbin/model.conf rename to config/casbin/model.conf diff --git a/cs-authentication/src/main/resources/casbin/policy.csv b/config/casbin/policy.csv similarity index 100% rename from cs-authentication/src/main/resources/casbin/policy.csv rename to config/casbin/policy.csv diff --git a/cs-database/src/main/resources/rbac/clean_database.sql b/config/rbac/clean_database.sql similarity index 100% rename from cs-database/src/main/resources/rbac/clean_database.sql rename to config/rbac/clean_database.sql diff --git a/cs-database/src/main/resources/rbac/data.sql b/config/rbac/data.sql similarity index 100% rename from cs-database/src/main/resources/rbac/data.sql rename to config/rbac/data.sql diff --git a/cs-database/src/main/resources/rbac/schema.sql b/config/rbac/schema.sql similarity index 100% rename from cs-database/src/main/resources/rbac/schema.sql rename to config/rbac/schema.sql diff --git a/cs-authentication/src/main/java/com/cf/cs/authentication/configure/CasbinConfigure.java b/cs-authentication/src/main/java/com/cf/cs/authentication/configure/CasbinConfigure.java new file mode 100644 index 0000000..71c06c1 --- /dev/null +++ b/cs-authentication/src/main/java/com/cf/cs/authentication/configure/CasbinConfigure.java @@ -0,0 +1,20 @@ +package com.cf.cs.authentication.configure; + +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * The type Casbin configure. + * + * @author xajhuang @163.com + */ +@Data +@ConfigurationProperties(prefix = "casbin") +@Configuration +@Slf4j +public class CasbinConfigure { + private String modelPath; + private String policyPath; +} diff --git a/cs-authentication/src/main/java/com/cf/cs/authentication/misc/CasbinAuthorizeUtils.java b/cs-authentication/src/main/java/com/cf/cs/authentication/misc/CasbinAuthorizeUtils.java index b16d76d..fa85cfb 100644 --- a/cs-authentication/src/main/java/com/cf/cs/authentication/misc/CasbinAuthorizeUtils.java +++ b/cs-authentication/src/main/java/com/cf/cs/authentication/misc/CasbinAuthorizeUtils.java @@ -1,6 +1,7 @@ package com.cf.cs.authentication.misc; +import com.cf.cs.authentication.configure.CasbinConfigure; import com.cf.cs.authentication.pojo.po.AuthAccountUser; import com.cf.cs.authentication.service.AuthUsersService; import com.cf.cs.database.mapper.RoleMapper; @@ -12,6 +13,9 @@ import lombok.extern.slf4j.Slf4j; import org.casbin.jcasbin.main.Enforcer; import org.springframework.stereotype.Component; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.util.List; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; @@ -28,6 +32,9 @@ import static com.cf.cs.database.pojo.entity.table.RoleTableDef.ROLE; public class CasbinAuthorizeUtils { private static final ConcurrentHashMap ENFORCER_POOL = new ConcurrentHashMap<>(); + @Resource + private CasbinConfigure casbinConfigure; + @Resource private AuthUsersService authUsersService; @@ -44,8 +51,10 @@ public class CasbinAuthorizeUtils { public void init() { roleMapper.selectAll().forEach(role -> { if (!CasbinAuthorizeUtils.ENFORCER_POOL.containsKey(role.getName())) { - String model = Objects.requireNonNull(this.getClass().getResource("/casbin/model.conf")).getFile(); - String policy = Objects.requireNonNull(this.getClass().getResource("/casbin/policy.csv")).getFile(); + String baseDir = System.getProperty("user.dir"); + String model = baseDir + "/" + casbinConfigure.getModelPath(); + String policy = baseDir + "/" + casbinConfigure.getPolicyPath(); + Enforcer e = new Enforcer(model, policy); e.enableAutoSave(false); @@ -60,7 +69,7 @@ public class CasbinAuthorizeUtils { e.addPermissionForUser(role.getName(), k.getResources().getPath(), k.getResources().getHttpMethod(), Boolean.TRUE.equals(k.getAuthorize()) ? "allow" : "deny"); log.trace("++++++[{}, {}, {}: {}]", role.getName(), k.getResources().getPath(), - k.getResources().getHttpMethod(), Boolean.TRUE.equals(k.getAuthorize()) ? "allow" : "deny"); + k.getResources().getHttpMethod(), Boolean.TRUE.equals(k.getAuthorize()) ? "allow" : "deny"); }); // 缓存当前权限验证器 diff --git a/cs-database/pom.xml b/cs-database/pom.xml index 6a6c1a0..797a13c 100644 --- a/cs-database/pom.xml +++ b/cs-database/pom.xml @@ -62,6 +62,15 @@ + + + src/main/java + + **/rbac/*.* + + false + + org.springframework.boot diff --git a/cs-restful/src/main/java/com/cf/cs/restful/CsRestfulApplication.java b/cs-restful/src/main/java/com/cf/cs/restful/CsRestfulApplication.java index 9b74635..30d7146 100644 --- a/cs-restful/src/main/java/com/cf/cs/restful/CsRestfulApplication.java +++ b/cs-restful/src/main/java/com/cf/cs/restful/CsRestfulApplication.java @@ -8,7 +8,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; * * @author xajhuang @163.com */ -@SpringBootApplication(scanBasePackages = {"com.cf.cs.authentication", "com.cf.cs.restful"}) +@SpringBootApplication(scanBasePackages = {"com.cf.cs.base", "com.cf.cs.database", "com.cf.cs.protocol", + "com.cf.cs.authentication", "com.cf.cs.restful"}) public class CsRestfulApplication { /**