当前位置: 首页 > 工具软件 > jCasbin > 使用案例 >

spring boot+jcasbin权限访问

夏侯博
2023-12-01

1.通过properties加载配置

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Component
@ConfigurationProperties(prefix = "jcasbin")
public class EnforcerConfigProperties {
    private String url;

    private String driverClassName;

    private String username;

    private String password;

    private String modelPath;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getModelPath() {
        return modelPath;
    }

    public void setModelPath(String modelPath) {
        this.modelPath = modelPath;
    }

    @Override
    public String toString() {
        return "EnforcerConfigProperties [url=" + url + ", driverClassName=" + driverClassName + ", username="
                + username + ", password=" + password + ", modelPath=" + modelPath + "]";
    }
}

2.EnforceFactory工厂单例endorce

import org.casbin.adapter.JDBCAdapter;
import org.casbin.jcasbin.main.Enforcer;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class EnforcerFactory implements InitializingBean {
    @Autowired
    private static  Enforcer enforcer;
    @Autowired
    private EnforcerConfigProperties config;
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("config");
        JDBCAdapter adapter = new JDBCAdapter(config.getDriverClassName(),config.getUrl(),config.getUsername(),config.getPassword());
        System.out.println(config.getModelPath());
        enforcer = new Enforcer(config.getModelPath(),adapter);
        enforcer.loadPolicy();
    }
    public static Enforcer getEnforcer(){
        return enforcer;
    }
}

3.过滤器

@WebFilter(urlPatterns = "/*")
@Order(Ordered.HIGHEST_PRECEDENCE)//执行顺序,最高级别最先执行,int从小到大
@Component
public class JCasbinAuthzFilter implements Filter {

   @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        Enforcer enforcer = EnforcerFactory.getEnforcer();
        enforcer.addPolicy("a","b","c");
        System.out.println(enforcer.getAllRoles());
        System.out.println(111);
        filterChain.doFilter(servletRequest, servletResponse);
        //TODO这里需要自己做权限验证
    }

    @Override
    public void destroy() {

    }
}

踩坑:

1.<dependency> <groupId>org.casbin</groupId> <artifactId>jdbc-adapter</artifactId> <version>2.0.1</version> </dependency>

这个依赖使用最新版(写作日期)的时候,确实implement,故用了2.0.1

2.@ConfigurationProperties 下不能使用静态变量,否则无法自动加载配置

 类似资料: