Skip to content

Commit

Permalink
feat: fix RedisWatcher bug in starter (#87)
Browse files Browse the repository at this point in the history
updated CasbinRedisWatcherAutoConfiguration.java: added casbinProperties parameter to redisWatcher method
updated application.yml: changed property names to kebab-case and added single quotes around policy-topic value
  • Loading branch information
ShingmoYeung authored Aug 2, 2023
1 parent b608b3e commit 03a7f35
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
@Configuration
@EnableConfigurationProperties({CasbinProperties.class, CasbinExceptionProperties.class, DataSourceProperties.class})
@AutoConfigureAfter({JdbcTemplateAutoConfiguration.class})
@ConditionalOnExpression("${casbin.enableCasbin:true}")
@ConditionalOnExpression("${casbin.enable-casbin:true}")
public class CasbinAutoConfiguration {

private static final Logger logger = LoggerFactory.getLogger(CasbinAutoConfiguration.class);
Expand All @@ -56,7 +56,7 @@ public class CasbinAutoConfiguration {
* Automatic configuration file storage adapter
*/
@Bean
@ConditionalOnProperty(name = "casbin.storeType", havingValue = "file")
@ConditionalOnProperty(name = "casbin.store-type", havingValue = "file")
@ConditionalOnMissingBean
public Adapter autoConfigFileAdapter(CasbinProperties properties) {
// if the file storage is chosen and the policy file location is set correctly, then create a file adapter
Expand All @@ -73,7 +73,7 @@ public Adapter autoConfigFileAdapter(CasbinProperties properties) {
* Automatic configuration of JDBC adapter
*/
@Bean
@ConditionalOnProperty(name = "casbin.storeType", havingValue = "jdbc", matchIfMissing = true)
@ConditionalOnProperty(name = "casbin.store-type", havingValue = "jdbc", matchIfMissing = true)
@ConditionalOnBean(JdbcTemplate.class)
@ConditionalOnMissingBean
public Adapter autoConfigJdbcAdapter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
@Configuration
@EnableConfigurationProperties({CasbinProperties.class, RedisProperties.class})
@AutoConfigureAfter({RedisAutoConfiguration.class, CasbinAutoConfiguration.class})
@ConditionalOnExpression("'jdbc'.equalsIgnoreCase('${casbin.storeType:jdbc}') && ${casbin.enableWatcher:false} && 'redis'.equalsIgnoreCase('${casbin.watcherType:redis}') ")
@ConditionalOnExpression("'jdbc'.equalsIgnoreCase('${casbin.store-type:jdbc}') && ${casbin.enable-watcher:false} && 'redis'.equalsIgnoreCase('${casbin.watcher-type:redis}') ")
public class CasbinRedisWatcherAutoConfiguration {

private final static Logger logger = LoggerFactory.getLogger(CasbinRedisWatcherAutoConfiguration.class);

@Bean
@ConditionalOnBean(RedisTemplate.class)
@ConditionalOnMissingBean
public Watcher redisWatcher(RedisProperties redisProperties, Enforcer enforcer) {
public Watcher redisWatcher(RedisProperties redisProperties, CasbinProperties casbinProperties, Enforcer enforcer) {
int timeout = redisProperties.getTimeout() != null ? (int) redisProperties.getTimeout().toMillis() : 2000;
RedisWatcher watcher = new RedisWatcher(redisProperties.getHost(), redisProperties.getPort(), redisProperties.getClientName(), timeout, redisProperties.getPassword());
RedisWatcher watcher = new RedisWatcher(redisProperties.getHost(), redisProperties.getPort(),
casbinProperties.getPolicyTopic(), timeout, redisProperties.getPassword());
enforcer.setWatcher(watcher);
logger.info("Casbin set watcher: {}", watcher.getClass().getName());
return watcher;
}
}
}
16 changes: 8 additions & 8 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
casbin:
# Whether to turn on Casbin, it is turned on by default
enableCasbin: true
enable-casbin: true
# Whether to enable automatic saving of the policy, if the adapter supports this function, it is enabled by default
autoSave: true
auto-save: true
# Storage type [file,jdbc], currently supported jdbc database [mysql(mariadb),h2,oracle,postgresql]
# 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
# Use jdbc by default, and use the built-in h2 database for memory storage
storeType: jdbc
store-type: jdbc
# Data source initialization strategy [create (automatically create a data table, if it has been created,
# then no longer initialize), never (always do not initialize)]
initializeSchema: create
initialize-schema: create
# Local model configuration file address, agreed by default read 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
use-default-model-if-model-not-setting: true
# Local policy configuration file address, agreed by default read location: classpath:casbin/policy.csv
# If the configuration file is not found in the default location, an exception will be thrown
# This configuration item only takes effect when storeType is set to file
policy: classpath:casbin/policy.csv
# Whether to turn on the CasbinWatcher mechanism, it is not turned on by default
# If the mechanism is turned on, the storeType must be jdbc, otherwise the configuration is invalid
enableWatcher: true
enable-watcher: true
# CasbinWatcher notification method, Redis is used by default for notification synchronization,
# and only Redis is supported for the time being
# After opening Watcher, you need to manually add spring-boot-starter-data-redis dependency
watcherType: redis
watcher-type: redis
# Add your Customer Topic here or it will go with 'CASBIN_POLICY_TOPIC' by default
policyTopic: YOUR_CUSTOMER_TOPIC
policy-topic: 'YOUR_CUSTOMER_TOPIC'

spring:
# Although the data source is configured here, the custom data source is switched to H2 when the unit test is tested.
Expand Down

0 comments on commit 03a7f35

Please sign in to comment.