OpenFGA

1.什么是OpenFGA

Fine-Grained Authorization 细粒度关系型授权

2.什么是细粒度授权

细粒度授权 (FGA) 意味着能够授予特定用户在特定资源中执行特定操作的权限。 精心设计的 FGA 系统允许您管理数百万个对象和用户的权限。随着系统不断添加对象并更新用户的访问权限,这些权限可能会迅速变化。

3.概念

类型 授权模型 存储 对象 用户 关系元组 关系

一个存储相当于 存储了一个或多个版本的 授权模型 ,授权模型是不可变的,只能由版本进行不断添加来迭代

授权模型里面定义了 类型 与 关系 之间直接与暗示的关联

对象 万物抽象的具体 皆对象

所以 用户其实也是用户类型下的一个对象, 关系是指在 一个类型中 所在与其他类型或 用户集等直接的关联

用户也是一个抽象 既 对资源访问的对象 既用户 也可以说 自己若是访问自己,那自己就是用户

关系元组 由 用户 关系 与对象 被授权模型与类型来作限制 共同创建出来的 一个个的关系元组

这就是总体的理解

4.安装

环境 Docker MySQL

1.创建docker-compose.yaml文件

bash 复制代码
mkdir -p /opt/openFGA
cd /opt/openFGA
bash 复制代码
touch docker-compose.yaml
XML 复制代码
version: '3.8'

networks:
  openfga:

services:
  mysql:
    image: mysql:8
    container_name: mysql
    networks:
      - openfga
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=secret
      - MYSQL_DATABASE=openfga
    healthcheck:
      test: ["CMD", 'mysqladmin', 'ping', '-h', 'localhost', '-u', 'root', '-p$$MYSQL_ROOT_PASSWORD' ]
      timeout: 20s
      retries: 5

  migrate:
    depends_on:
        mysql:
            condition: service_healthy
    image: openfga/openfga:latest
    container_name: migrate
    command: migrate
    environment:
      - OPENFGA_DATASTORE_ENGINE=mysql
      - OPENFGA_DATASTORE_URI=root:secret@tcp(mysql:3306)/openfga?parseTime=true
    networks:
      - openfga

  openfga:
    depends_on:
      migrate:
        condition: service_completed_successfully
    image: openfga/openfga:latest
    container_name: openfga
    environment:
      - OPENFGA_DATASTORE_ENGINE=mysql
      - OPENFGA_DATASTORE_URI=root:secret@tcp(mysql:3306)/openfga?parseTime=true
      - OPENFGA_LOG_FORMAT=json
    command: run --profiler-enabled --profiler-addr :3002 --playground-enabled --playground-port 3001
    networks:
      - openfga
    ports:
      # Needed for the http server
      - "8080:8080"
      # Needed for the grpc server (if used)
      - "8081:8081"
      # Needed for the playground (Do not enable in prod!)
      - "3000:3000"
      - "3002:3002"
      - "3001:3001"

这里用的测试环境 所以Mysql 不改了 就是 直接默认的 密码 是 secret

2.启动容器

bash 复制代码
docker compose up -d

5.创建Java 项目

导入依赖

XML 复制代码
  <dependency>
            <groupId>dev.openfga</groupId>
            <artifactId>openfga-sdk</artifactId>
            <version>0.3.1</version>
        </dependency>

6.创建一个Store

java 复制代码
package com.jmj.openfgastudy.createStore;

import dev.openfga.sdk.api.client.OpenFgaClient;
import dev.openfga.sdk.api.client.model.ClientCreateStoreResponse;
import dev.openfga.sdk.api.configuration.ClientConfiguration;
import dev.openfga.sdk.api.model.CreateStoreRequest;
import dev.openfga.sdk.errors.FgaInvalidParameterException;

import java.util.concurrent.ExecutionException;

import static com.jmj.openfgastudy.common.GlobalConst.FGA_API_URL;
import static com.jmj.openfgastudy.common.GlobalConst.FGA_STORE_NAME;

/**
 * 创建一个Store
 */
public class CreateStore {
    public static void main(String[] args) throws FgaInvalidParameterException, ExecutionException, InterruptedException {
        //创建配置
        ClientConfiguration config = new ClientConfiguration()
                .apiUrl(FGA_API_URL);

        //创建客户端
        OpenFgaClient fgaClient = new OpenFgaClient(config);

        /**
         * 请求体
         */
        CreateStoreRequest body = new CreateStoreRequest().name(FGA_STORE_NAME);

        ClientCreateStoreResponse response = fgaClient.createStore(body).get();

        String rawResponse = response.getRawResponse();
        System.out.println("rawResponse:"+rawResponse);
        String id = response.getId();
        System.out.println("id:"+id);
        String name = response.getName();
        System.out.println("name:"+name);
    }
}

id为StoreId

相关推荐
m0_748247802 分钟前
SOA(面向服务架构)全面解析
java·微服务·架构
m0_740154675 分钟前
Tomcat
java·tomcat
爱敲代码的小冰12 分钟前
【Excel】 Power Query抓取多页数据导入到Excel
android·java·excel
心流时间26 分钟前
[Java基础] JVM常量池介绍(BeanUtils.copyProperties(source, target)中的属性值引用的是同一个对象吗)
java·开发语言·jvm
瑞金彭于晏43 分钟前
通俗易懂版 Maven 科普,maven是什么?
java·maven
好看资源平台1 小时前
Java Web开发实战与项目——Spring Boot与Spring Cloud微服务项目实战
java
.猫的树1 小时前
Java集合List快速实现重复判断的10种方法深度解析
java·开发语言·list·集合
littlegirll1 小时前
命令行方式安装KFS同步KES到KADB
java·大数据·数据库
itachi-uchiha1 小时前
深入理解 Linux 中的 last 和 lastb 命令
java·linux·服务器
xiaoyustudiowww1 小时前
JSP + Servlet 实现 AJAX(纯JS版)
java·javascript·servlet