gitee.com/haifengat/cas@v0.0.6/README.md (about)

     1  # study_cas
     2  
     3  学习 cas 单点登录 [casdoor 官网](https://casdoor.org/zh/docs/overview)
     4  
     5  ## 介绍
     6  
     7  ## 软件架构
     8  
     9  软件架构说明
    10  
    11  ## 安装教程
    12  
    13  ### 部署 casdoor
    14  
    15  ```sh
    16  # 默认: admin/123
    17  docker run -itd --name casdoor -p 8000:8000 casbin/casdoor-all-in-one
    18  ```
    19  
    20  ## 使用说明
    21  
    22  ### casdoor 服务器配置
    23  
    24  [http://localhost:8000]
    25  
    26  1. "证书"添加
    27     1. 名称: cert_haifengat
    28     2. 下载证书: token_jwt_key.pem 保存到程序目录 config 下
    29  2. "组织"菜单中添加组织
    30     1. 名称: org_haifengat
    31     2. 万能密码: 12345
    32     3. 软删除
    33  3. "角色"菜单中添加角色
    34     1. 选择组织: org_haifengat
    35     2. 名称: role_normal
    36     3. 显示名称: 普通成员
    37  4. "模型"添加
    38     1. 组织: org_haifengat
    39     2. 名称: model_haifengat
    40     3. 模型文本: (RBAC)
    41  
    42  ```ini
    43    # 请求
    44    [request_definition]
    45    r = sub, obj, act
    46    # 策略
    47    [policy_definition]
    48    p = sub, obj, act
    49    # 角色
    50    [role_definition]
    51    g = _, _
    52    # 策略效果
    53    [policy_effect]
    54    e = some(where (p.eft == allow))
    55    # 匹配器
    56    [matchers]
    57    m = g(r.sub, p.sub)&& r.obj == p.obj && r.act == p.act
    58  ```
    59  
    60  5. "应用"添加
    61     1. 名称: app_test01
    62     2. 选择组织: org_haifengat
    63     3. 得到
    64        1. clientid: cd3518599975d2e97f5c
    65        2. secret: 16f49d8622bf8159e64144351a1a5a8454f7dff5
    66     4. 选择证书: cert_haifengat
    67     5. 添加重定向 URLs:
    68        1. 登录页面 url 中的 redict_uri 必须在此配置中
    69           1. <http://localhost:9000/api/signin> 测试本地后端 api
    70        2. 测试 api 时填后端地址
    71        3. 正常流程: 前端通过 redict_uri 得到 code&state, 再向后端请求登录
    72     6. accessToken 过期时间 168h=7d
    73     7. [x] 保持登录会话
    74     8. [x] 启用自动登录
    75     9. [ ] 启用验证码登录
    76     10. [ ] 启用 WebAuthn 登录
    77     11. OAuth 授权类型
    78         - [x] Authorization Code
    79         - [x] Token
    80         - [ ] Password
    81         - [ ] ID Token
    82     12. 保存
    83     13. 测试用户注册 [http://localhost:8000/signup/app_test01]
    84         1. test001/123456
    85         2. 登录[http://localhost:8000/login/org_haifengat]
    86  6. "权限"添加
    87     1. 选择组织: org_haifengat
    88     2. 名称: perm_normal
    89     3. 显示名称:低级权限
    90     4. 模型: model_haifengat
    91     5. 包含用户: org_haifengat/\* **手写**
    92     6. 包含角色: org_home/role_normal 模型中采用 RBAC
    93     7. 资源类型: 应用
    94     8. 资源: app_test01
    95     9. 动作: 读权限 写权限
    96     10. 状态: 审批通过
    97  7. "用户"菜单添加
    98     1. 选择组织: org_haifengat
    99     2. 名称: haifengat
   100     3. 显示名称: 海风
   101     4. 用户类型: normal-user
   102     5. 密码: 123456
   103     6. 注册应用: app_test01
   104     7. [ ] 管理员
   105     8. [ ] 全局管理员
   106  8. 获取的数据
   107     | Name (in order) | Must | Description |
   108     | ---------------- | ---- | --------------------------------------------------- |
   109     | endpoint | Yes | Casdoor server URL, such as <http://localhost:8000> |
   110     | clientId | Yes | Application.clientId |
   111     | clientSecret | Yes | Application.clientSecret |
   112     | certificate | Yes | x509 certificate content of Application.cert |
   113     | organizationName | Yes | Application.organization |
   114     | applicationName | Yes | Application.applicationName |
   115  
   116  ### 登录
   117  
   118  [参考代码](https://github.com/casbin/casnode/blob/master/controllers/account.go)
   119  
   120  1. 获取 code
   121  
   122     ```sh
   123     export code=$(echo $(curl -X POST -H 'Content-Type:application/json' \
   124     'http://localhost:8000/api/login?clientId=cd3518599975d2e97f5c&responseType=code&redirectUri=http%3A%2F%2Flocalhost%3A9000%2Fapi%2Fsignin&scope=read&state=app_test01' \
   125     -d '{
   126        "application": "app_test01",
   127        "organization": "org_haifengat",
   128        "username": "hf001",
   129        "password": "123456",
   130        "autoSignin": true,
   131        "type": "code"
   132     }') | awk -F ',' '{print $5}' |awk -F ':' '{print $2}'|awk -F '"' '{print $2}')
   133     echo $code
   134     ```
   135  
   136  2. 获取 accessCode
   137  
   138     ```sh
   139     export accessToken=$(echo $(curl http://localhost:9000/api/signin?code=$code&state=app_test01) |grep -Pos "\"accessToken\":\"[^,]*\""|awk -F '"' '{print $4}')
   140     echo $accessToken
   141     ```
   142  
   143  3. 测试
   144  
   145     ```sh
   146  
   147     ```
   148  
   149  ### casbin 权限
   150  
   151  [rbac_model.conf](config/rbac_model.conf)
   152  
   153  ```go
   154  var (
   155     dmDSN     string = "dm://SYSDBA:SYSDBA001@localhost:5236"
   156     modelFile string = "./config/rbac_model.conf"
   157  )
   158  if err := middleware.InitCasAdapter(dmDSN, modelFile); err != nil {
   159     logrus.Error(err)
   160     os.Exit(1)
   161  }
   162   r.Use(middleware.CheckRole)
   163  ```
   164  
   165  ## 附
   166  
   167  [RBAC api](https://casbin.org/zh/docs/rbac-api)
   168  
   169  [apiFox 测试用例](https://www.apifox.cn/apidoc/shared-4a9dcf33-a2b6-4702-a20b-e96ef8d07f60)
   170  
   171  [导入用户(xlsx 示例)](xlsx/users.xlsx)
   172  
   173  [示例代码](demo/main.go)