Skip to content

Commit

Permalink
Merge pull request #18 from shy1st/master
Browse files Browse the repository at this point in the history
Support the reading mode of classpath.
  • Loading branch information
hsluoyz committed Feb 14, 2021
2 parents c1cbddb + 43816f5 commit a6c327d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
28 changes: 25 additions & 3 deletions src/main/java/org/casbin/shiro/factory/EnforcerFactory.java
Expand Up @@ -19,12 +19,12 @@
import org.casbin.shiro.config.EnforcerConfigProperties;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import javax.sql.DataSource;
import java.io.FileNotFoundException;

/**
* When the container is loaded, it is initialized to load the information in the configuration file.
Expand All @@ -45,10 +45,15 @@ public class EnforcerFactory implements InitializingBean {
@Autowired
private DataSource dataSource;

/**
* Initialize the JDBCAdapter and Enforcer.
*/
@Override
public void afterPropertiesSet() throws Exception {
jdbcAdapter = new JDBCAdapter(dataSource);
enforcer = new Enforcer(properties.getModelPath(), properties.getPolicyPath());
String modelPath = getResourcePath(this.properties.getModelPath());
String policyPath = getResourcePath(this.properties.getPolicyPath());
enforcer = new Enforcer(modelPath, policyPath);
jdbcAdapter.savePolicy(enforcer.getModel());
}

Expand All @@ -59,4 +64,21 @@ public static Enforcer getEnforcer() {
public static JDBCAdapter getJdbcAdapter() {
return jdbcAdapter;
}

/**
* Get the resource path which can used by enforcer.
*
* @param resourcePath the path in the configuration.
* @return path;
* @throws FileNotFoundException the resource path exists error.
*/
private String getResourcePath(String resourcePath) throws FileNotFoundException {
String path;
if (ResourceUtils.isUrl(resourcePath)) {
path = ResourceUtils.getURL(resourcePath).getPath();
} else {
path = resourcePath;
}
return path;
}
}
4 changes: 2 additions & 2 deletions src/test/resources/application.yml
@@ -1,6 +1,6 @@
shiro-casbin:
modelPath: src/test/resources/model/rbac_model.conf
policyPath: src/test/resources/model/rbac_policy.csv
modelPath: classpath:casbin/rbac_model.conf
policyPath: classpath:casbin/rbac_policy.csv

spring:
datasource:
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit a6c327d

Please sign in to comment.