MCPcopy Index your code
hub / github.com/apache/casbin-spring-boot-starter

github.com/apache/casbin-spring-boot-starter @v2.0.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v2.0.0 ↗ · + Follow
83 symbols 135 edges 19 files 26 documented · 31%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Casbin Spring Boot Starter

Codecov branch GitHub Actions Maven Central License SpringBootVersion JavaVersion CasbinVersion

Casbin Spring Boot Starter is designed to help you easily integrate jCasbin into your Spring Boot project.

Requirements

  • Java 17 or higher
  • Spring Boot 4.0.0 or higher

how to use

  1. Add casbin-spring-boot-starter to the Spring Boot project.

Maven


<dependency>
    <groupId>org.casbin</groupId>
    <artifactId>casbin-spring-boot-starter</artifactId>
    <version>version</version>
</dependency>

Gradle

implementation 'org.casbin:casbin-spring-boot-starter:version'
  1. Inject the Enforcer where you need to use it

@Component
public class Test {
    @Autowired
    private Enforcer enforcer;
}
  1. Add configuration
casbin:
  #Whether to enable Casbin, it is enabled by default.
  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]
  #Welcome to write and submit the jdbc adapter you are using, see: org.casbin.adapter.OracleAdapter
  #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
  model: classpath:casbin/model.conf
  #If the model configuration file is not found in the default location and casbin.model is not set correctly, the built-in default rbac model is used, which takes effect by default.
  useDefaultModelIfModelNotSetting: true
  #Local policy configuration file address, the default reading location: classpath: casbin/policy.csv
  #If the configuration file is not found in the default location, an exception will be thrown.
  #This configuration item takes effect only when casbin.storeType is set to file.
  policy: classpath:casbin/policy.csv
  #Whether to enable the CasbinWatcher mechanism, the default is not enabled.
  #If the mechanism is enabled, casbin.storeType must be jdbc, otherwise the configuration is invalid.
  enableWatcher: false
  #CasbinWatcher notification mode, defaults to use Redis for notification synchronization, temporarily only supports Redis
  #After opening Watcher, you need to manually add spring-boot-starter-data-redis dependency.
  watcherType: redis
  #Watcher to support spring tx, the default is not enable.
  #If the mechanism is enabled, When updating policy in a spring transaction, watcher will trigger the update after commit
  watcherTxSupport: false
  exception:
    ... See Schedule A for exception settings.
  1. The simplest configuration

  2. Do not use other add-on configurations

casbin:
  #If you are using a model profile at this address, no configuration is required
  model: classpath:casbin/model.conf
  • Turn on Watcher
casbin:
  #If the model profile you are using is located at this address, you do not need this configuration
  model: classpath:casbin/model.conf
  #When you open Watcher, the default use of RedisWatcher requires manual addition of spring-boot-starter-data-redis dependency.
  enableWatcher: true
  1. Use custom independent data sources

  2. Only increase @CasbinDataSource annotation when injecting custom data source


@Configuration
public class CasbinDataSourceConfiguration {
    @Bean
    @CasbinDataSource
    public DataSource casbinDataSource() {
        return DataSourceBuilder.create().url("jdbc:h2:mem:casbin").build();
    }
}
Schedule A
  • ExceptionSettings(casbin.exception)
name description default
removePolicyFailed Throws an exception when the delete policy fails false
Note: If you do not set another data source, or set the storage file location for H2, the data is stored in memory by default using H2.

Notice:

Since version 0.0.11, casbin-spring-boot-starter adds an id field to the database table structure by default.

The version before 0.0.11 is upgraded to version 0.0.11 and later requires the user to manually add the id field.

See https://github.com/jcasbin/casbin-spring-boot-starter/issues/21 for details

Core symbols most depended-on inside this repo

getPolicyTopic
called by 3
src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java
getFileAsInputStream
called by 2
src/main/java/org/casbin/utils/FileUtils.java
isRemovePolicyFailed
called by 2
src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinExceptionProperties.java
getWatcherLettuceRedisType
called by 2
src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java
removePrefix
called by 1
src/main/java/org/casbin/utils/FileUtils.java
getFile
called by 1
src/main/java/org/casbin/utils/FileUtils.java
getFileAsText
called by 1
src/main/java/org/casbin/utils/FileUtils.java
update
called by 1
src/main/java/org/casbin/spring/boot/autoconfigure/TxWatcher.java

Shape

Method 64
Class 15
Enum 4

Languages

Java100%

Modules by API surface

src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinProperties.java33 symbols
src/main/java/org/casbin/spring/boot/autoconfigure/TxWatcher.java7 symbols
src/test/java/org/casbin/adapter/JdbcAdapterTest.java6 symbols
src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java6 symbols
src/main/java/org/casbin/utils/FileUtils.java5 symbols
src/main/java/org/casbin/spring/boot/autoconfigure/CasbinRedisWatcherAutoConfiguration.java4 symbols
src/test/java/org/casbin/EnforcerTest.java3 symbols
src/main/java/org/casbin/spring/boot/autoconfigure/properties/CasbinExceptionProperties.java3 symbols
src/test/java/org/casbin/config/CasbinDataSourceConfiguration.java2 symbols
src/test/java/org/casbin/CasbinSpringBootStarterApplicationTest.java2 symbols
src/main/java/org/casbin/exception/CasbinWatcherLettuceTypeUnsupportedException.java2 symbols
src/main/java/org/casbin/exception/CasbinPolicyConfigNotFoundException.java2 symbols

Datastores touched

(mysql)Database · 1 repos
casbinDatabase · 1 repos

For agents

$ claude mcp add casbin-spring-boot-starter \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact