jcasbin / casbin-spring-boot-starter

Spring Boot 2.x & 3.x Starter for Casbin, see example at: https://github.com/jcasbin/casbin-spring-boot-example

Home Page:https://mvnrepository.com/artifact/org.casbin/casbin-spring-boot-starter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Casbin Starter Can't Use Dynamic Data Source

llb841863085 opened this issue · comments

As the project needed, we used the dynamic data source, so there is no config as follows:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://ip:3306/dev
    username: root
    password: root

What we have used is:

spring:
  datasource:
    dynamic:
      datasource:
        # master
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://ip:3306/dev
          username: root
          password: root

When I start my application, I got an error something like datasource init failed because of the dataSourceProperties information is null
Code Path:casbin-spring-boot-starter/src/main/java/org/casbin/spring/boot/autoconfigure/CasbinAutoConfiguration.java:72

   /**
     * Automatic configuration of JDBC adapter
     */
    @Bean
    @ConditionalOnProperty(name = "casbin.storeType", havingValue = "jdbc", matchIfMissing = true)
    @ConditionalOnBean(JdbcTemplate.class)
    @ConditionalOnMissingBean
    public Adapter autoConfigJdbcAdapter(
            @CasbinDataSource ObjectProvider<DataSource> casbinDataSource,
            JdbcTemplate jdbcTemplate,
            CasbinProperties properties,
            CasbinExceptionProperties exceptionProperties,
            DataSourceProperties dataSourceProperties
    ) throws Exception {
        JdbcTemplate jdbcTemplateToUse = getJdbcTemplate(jdbcTemplate, casbinDataSource);
        String databaseName = getDatabaseName(jdbcTemplateToUse.getDataSource());
        CasbinDataSourceInitializationMode initializeSchema = properties.getInitializeSchema();
        boolean autoCreateTable = initializeSchema == CasbinDataSourceInitializationMode.CREATE;
        String tableName = properties.getTableName();
        logger.info("Casbin current use database product: {}", databaseName);
        return new JDBCAdapter(dataSourceProperties.determineDriverClassName(), dataSourceProperties.getUrl(),
                dataSourceProperties.getUsername(), dataSourceProperties.getPassword(),
                exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);

    }

To solve this problem, I have modified this code block as follows:

/**
     * Automatic configuration of JDBC adapter
     */
    @Bean
    @ConditionalOnProperty(name = "casbin.storeType", havingValue = "jdbc", matchIfMissing = true)
    @ConditionalOnBean(JdbcTemplate.class)
    @ConditionalOnMissingBean
    public Adapter autoConfigJdbcAdapter(
            @CasbinDataSource ObjectProvider<DataSource> casbinDataSource,
            JdbcTemplate jdbcTemplate,
            CasbinProperties properties,
            CasbinExceptionProperties exceptionProperties,
            DataSourceProperties dataSourceProperties
    ) throws Exception {
        JdbcTemplate jdbcTemplateToUse = getJdbcTemplate(jdbcTemplate, casbinDataSource);
        if (jdbcTemplateToUse == null || jdbcTemplateToUse.getDataSource() == null) {
            throw new CasbinAdapterException("Cannot create jdbc adapter, because jdbc template is not set");
        }
        String databaseName = getDatabaseName(jdbcTemplateToUse.getDataSource());
        CasbinDataSourceInitializationMode initializeSchema = properties.getInitializeSchema();
        boolean autoCreateTable = initializeSchema == CasbinDataSourceInitializationMode.CREATE;
        String tableName = properties.getTableName();
        logger.info("Casbin current use database product: {}", databaseName);
        // datasource properties are configed
        if (dataSourceProperties.getUrl() != null) {
            return new JDBCAdapter(dataSourceProperties.determineDriverClassName(), dataSourceProperties.getUrl(),
                    dataSourceProperties.getUsername(), dataSourceProperties.getPassword(),
                    exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);
        }
        // when datasource properties are not configed, use the default datasource in jdbcTemplate
        else {
            return new JDBCAdapter(jdbcTemplateToUse.getDataSource(), exceptionProperties.isRemovePolicyFailed(), tableName, autoCreateTable);
        }
    }

@llb841863085 hi, as part of the global open-source community, can you modify the texts into English so the global developers will be easier to read your message?

@llb841863085 hi, as part of the global open-source community, can you modify the texts into English so the global developers will be easier to read your message?

Done.