Skip to content

Commit

Permalink
feat: support customized redis topic (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: fabian4 <[email protected]>
  • Loading branch information
fabian4 authored Dec 13, 2021
1 parent c5e289d commit 207d741
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@
public class CasbinRedisWatcherAutoConfiguration {

private final static Logger logger = LoggerFactory.getLogger(CasbinRedisWatcherAutoConfiguration.class);
public final static String CASBIN_POLICY_TOPIC = "CASBIN_POLICY_TOPIC";

@Bean
@ConditionalOnBean(RedisTemplate.class)
@ConditionalOnMissingBean
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public Watcher redisWatcher(StringRedisTemplate stringRedisTemplate, Enforcer enforcer) {
RedisWatcher watcher = new RedisWatcher(stringRedisTemplate);
public Watcher redisWatcher(StringRedisTemplate stringRedisTemplate, Enforcer enforcer, CasbinProperties casbinProperties) {
RedisWatcher watcher = new RedisWatcher(stringRedisTemplate, casbinProperties.getPolicyTopic());
enforcer.setWatcher(watcher);
logger.info("Casbin set watcher: {}", watcher.getClass().getName());
return watcher;
Expand All @@ -71,12 +70,13 @@ public MessageListenerAdapter messageListenerAdapter(Watcher receiver) {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public RedisMessageListenerContainer redisMessageListenerContainer(
RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter
MessageListenerAdapter listenerAdapter,
CasbinProperties casbinProperties
) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
// subscribe to the CASBIN_POLICY_TOPIC channel
container.addMessageListener(listenerAdapter, new ChannelTopic(CASBIN_POLICY_TOPIC));
container.addMessageListener(listenerAdapter, new ChannelTopic(casbinProperties.getPolicyTopic()));
return container;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public class CasbinProperties {
* Watcher synchronization strategy
*/
private CasbinWatcherType watcherType = CasbinWatcherType.REDIS;
/**
* Redis topic for Watcher
*/
private String policyTopic = "CASBIN_POLICY_TOPIC";
/**
* Data table initialization strategy
*/
Expand Down Expand Up @@ -169,5 +173,13 @@ public String getTableName() {
public void setTableName(String tableName) {
this.tableName = tableName;
}

public String getPolicyTopic() {
return policyTopic;
}

public void setPolicyTopic(String policyTopic) {
this.policyTopic = policyTopic;
}
}

10 changes: 6 additions & 4 deletions src/main/java/org/casbin/watcher/RedisWatcher.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.casbin.watcher;

import org.casbin.jcasbin.persist.Watcher;
import org.casbin.spring.boot.autoconfigure.CasbinRedisWatcherAutoConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
Expand All @@ -17,14 +16,17 @@
* @date 2019-4-06 1:58
*/
public class RedisWatcher implements Watcher {

private Runnable updateCallback;
private final String policyTopic;
private final StringRedisTemplate stringRedisTemplate;
private final static String REDIS_WATCHER_UUID = UUID.randomUUID().toString();
private final static Logger logger = LoggerFactory.getLogger(RedisWatcher.class);

public RedisWatcher(StringRedisTemplate stringRedisTemplate) {
public RedisWatcher(StringRedisTemplate stringRedisTemplate, String policyTopic) {
this.policyTopic = policyTopic;
this.stringRedisTemplate = stringRedisTemplate;
logger.info("Current casbin redis watcher uuid: {}", REDIS_WATCHER_UUID);
logger.info("Current casbin redis watcher uuid: {}, subscribe topic: {}", REDIS_WATCHER_UUID, this.policyTopic);
}

@Override
Expand All @@ -35,7 +37,7 @@ public void setUpdateCallback(Runnable runnable) {
@Override
public void update() {
stringRedisTemplate.convertAndSend(
CasbinRedisWatcherAutoConfiguration.CASBIN_POLICY_TOPIC,
this.policyTopic,
"Casbin policy has a new version from redis watcher: " + REDIS_WATCHER_UUID
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@
"sourceType": "org.casbin.spring.boot.autoconfigure.properties.CasbinProperties",
"defaultValue": "redis"
},
{
"name": "casbin.policyTopic",
"type": "java.lang.String",
"description": "Redis topic for Watcher",
"sourceType": "org.casbin.spring.boot.autoconfigure.properties.CasbinProperties",
"defaultValue": "CASBIN_POLICY_TOPIC"
},
{
"name": "casbin.exception.removePolicyFailed",
"type": "java.lang.Boolean",
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ casbin:
# 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
# Add your Customer Topic here or it will go with 'CASBIN_POLICY_TOPIC' by default
policyTopic: 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.
datasource:
Expand Down

0 comments on commit 207d741

Please sign in to comment.