Title: 用基于接口的可扩展枚举提供更多权限支持 · Issue #286 · APIJSON/APIJSON · GitHub
Open Graph Title: 用基于接口的可扩展枚举提供更多权限支持 · Issue #286 · APIJSON/APIJSON
X Title: 用基于接口的可扩展枚举提供更多权限支持 · Issue #286 · APIJSON/APIJSON
Description: APIJSON 本身的权限定义在枚举 RequestRole 中,且与框架本身的耦合度较高,如果想要在此基础上定义新的权限级别不是很方便。虽然可以通过重载 AbstractVerifier.verifyAccess 方法来自定义鉴权,但是用起来还是相对比较复杂,如(这个 issue https://github.com/Tencent/APIJSON/issues/228)。 为了尝试解决这一问题,我定义了一个新的接口 IRequestRole,令 RequestRol...
Open Graph Description: APIJSON 本身的权限定义在枚举 RequestRole 中,且与框架本身的耦合度较高,如果想要在此基础上定义新的权限级别不是很方便。虽然可以通过重载 AbstractVerifier.verifyAccess 方法来自定义鉴权,但是用起来还是相对比较复杂,如(这个 issue https://github.com/Tencent/APIJSON/issues/228)。 为了尝试解决这...
X Description: APIJSON 本身的权限定义在枚举 RequestRole 中,且与框架本身的耦合度较高,如果想要在此基础上定义新的权限级别不是很方便。虽然可以通过重载 AbstractVerifier.verifyAccess 方法来自定义鉴权,但是用起来还是相对比较复杂,如(这个 issue https://github.com/Tencent/APIJSON/issues/228)。 为了尝试解决这...
Opengraph URL: https://github.com/APIJSON/APIJSON/issues/286
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"用基于接口的可扩展枚举提供更多权限支持","articleBody":"APIJSON 本身的权限定义在枚举 RequestRole 中,且与框架本身的耦合度较高,如果想要在此基础上定义新的权限级别不是很方便。虽然可以通过重载 AbstractVerifier.verifyAccess 方法来自定义鉴权,但是用起来还是相对比较复杂,如(这个 issue https://github.com/Tencent/APIJSON/issues/228)。\r\n\r\n为了尝试解决这一问题,我定义了一个新的接口 IRequestRole,令 RequestRole 实现这个接口,再依次修改框架中其他相关联的位置。通过 IRequestRole.register 方法,用户可以注册自己的权限枚举。通过 IRequestRole.get 方法,可以从权限字符串转换回对应的权限枚举。\r\n\r\n根据我在自己的示例项目上的测试,基本可以满足需要。但是因为包含一些破坏性变更(`MethodAccess` 注解返回 `String[]`),我希望先了解下各位的意见和建议。\r\n\r\n可以从这里获取修改后的代码(含 APIJSON ORM 和 Framework)及示例项目: \r\nhttps://github.com/jerrylususu/apijson_role_extend \r\n\r\n具体的使用方法如下:\r\n\r\n1. 首先定义自己的权限枚举类(可参照 `apijson.demo.config.MyRole`)\r\n\r\n ```java\r\n public enum MyRole implements IRequestRole {\r\n STUDENT,\r\n TEACHER,\r\n PRINCIPAL;\r\n }\r\n ```\r\n\r\n2. 在 `DemoApplication` 的 static 块中注册自己的权限枚举类\r\n\r\n ```java\r\n static {\r\n IRequestRole.register(MyRole.class);\r\n APIJSONApplication.DEFAULT_APIJSON_CREATOR = // ...\r\n }\r\n ```\r\n\r\n3. 在自己的用户类(`model.User`)中添加 `List\u003cString> role` 属性,并添加对应的 getter, setter 方法 \r\n\r\n4. 重载 `AbstractVerifier.verifyAccess`,补全一部分框架中未完成的鉴权逻辑(判断用户请求中声明的权限,是否的确存在于用户的权限列表中)\r\n\r\n > 此部分与用户自己的用户类的类名相关,因此没有在框架中实现(可能可以用 `Visitor` 接口实现?)\r\n\r\n ```java\r\n // 自定义的权限,需要检查是否存在于列表中\r\n if ( !(config.getRole() instanceof RequestRole) ) {\r\n List\u003cString> visitorRoleList = new ArrayList\u003c>();\r\n // 处理数据库中 role 列为 NULL\r\n if (((User) this.visitor).getRole() != null) {\r\n visitorRoleList = ((User) this.visitor).getRole();\r\n }\r\n \r\n if (!visitorRoleList.contains(config.getRole().toString())){\r\n // 用户声明的权限不在自己的 role 列表中(伪造权限)\r\n throw new IllegalAccessException(\"当前用户没有声明的权限!\");\r\n }\r\n \r\n }\r\n ```\r\n\r\n5. 在数据库 `Access` 表中完成对应配置\r\n\r\n * GET/GETS/HEAD/HEADS:[\"STUDENT\", \"TEACHER\",\"PRINCIPAL\"]\r\n * POST/DELETE:[\"TEACHER\"]\r\n * PUT:[\"OWNER\",\"PRINCIPAL\"]\r\n\r\n\r\n6. 在调用时先登录,然后用 `@role` 声明对应的权限\r\n\r\n ```json\r\n // POST /get\r\n {\r\n \"[]\": {\r\n \"Course\": {\r\n \"@role\": \"TEACHER\"\r\n }\r\n }\r\n }\r\n ```\r\n","author":{"url":"https://github.com/jerrylususu","@type":"Person","name":"jerrylususu"},"datePublished":"2021-08-01T08:27:20Z","url":"https://github.com/APIJSON/APIJSON/issues/286"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:433741fa-6d81-6970-3ab6-744701bb44cc |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A82E:3339EE:34665B:47B61C:6AFCAF33 |
| html-safe-nonce | 0f08cb71179063a5ec20005c8507ebe96a46e782fcdc8637b1b45b3e2f4159aa |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBODJFOjMzMzlFRTozNDY2NUI6NDdCNjFDOjZBRkNBRjMzIiwidmlzaXRvcl9pZCI6IjQ1MjI0NTgzMjgwNTEwMDMxODciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | dc439034f294a50b392ef0089e84c5e3006bcad85f2e962afd0fff8b21f7713e |
| hovercard-subject-tag | issue:957429990 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/APIJSON/APIJSON/286/issue_layout |
| twitter:image | https://opengraph.githubassets.com/f83322c37dc9ee5f7badfe894148c83f009582504f4dbb97e00b3550132324a5/APIJSON/APIJSON/issues/286 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/f83322c37dc9ee5f7badfe894148c83f009582504f4dbb97e00b3550132324a5/APIJSON/APIJSON/issues/286 |
| og:image:alt | APIJSON 本身的权限定义在枚举 RequestRole 中,且与框架本身的耦合度较高,如果想要在此基础上定义新的权限级别不是很方便。虽然可以通过重载 AbstractVerifier.verifyAccess 方法来自定义鉴权,但是用起来还是相对比较复杂,如(这个 issue https://github.com/Tencent/APIJSON/issues/228)。 为了尝试解决这... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | jerrylususu |
| hostname | github.com |
| expected-hostname | github.com |
| None | e3084aead77cbcb4f4904fba53851fa1a0546b0dec3619bd250815e72588dc6d |
| turbo-cache-control | no-preview |
| go-import | github.com/APIJSON/APIJSON git https://github.com/APIJSON/APIJSON.git |
| octolytics-dimension-user_id | 41146037 |
| octolytics-dimension-user_login | APIJSON |
| octolytics-dimension-repository_id | 74359442 |
| octolytics-dimension-repository_nwo | APIJSON/APIJSON |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 74359442 |
| octolytics-dimension-repository_network_root_nwo | APIJSON/APIJSON |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 5410e4bebd3e45b138c853ecef3992279d86d5af |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width