You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by de...@apache.org on 2023/01/13 07:46:20 UTC
[shenyu-website] branch main updated: [doc: jwt plugin] update documentation of Jwt plugin (#840)
This is an automated email from the ASF dual-hosted git repository.
dengliming pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/shenyu-website.git
The following commit(s) were added to refs/heads/main by this push:
new 63d479d6325 [doc: jwt plugin] update documentation of Jwt plugin (#840)
63d479d6325 is described below
commit 63d479d6325dbb7b9181ce8e7a8ecfe4954e5e1e
Author: 愿凌飞 <ti...@foxmail.com>
AuthorDate: Fri Jan 13 15:46:14 2023 +0800
[doc: jwt plugin] update documentation of Jwt plugin (#840)
* [doc: jwt plugin] update documentation of Jwt plugin
* fix style
---
docs/developer/custom-jwt-covert-algorithm.md | 92 +++++++++++++++++++++
docs/plugin-center/security/jwt-plugin.md | 2 +
.../developer/custom-jwt-covert-algorithm.md | 93 ++++++++++++++++++++++
.../current/plugin-center/security/jwt-plugin.md | 2 +
4 files changed, 189 insertions(+)
diff --git a/docs/developer/custom-jwt-covert-algorithm.md b/docs/developer/custom-jwt-covert-algorithm.md
new file mode 100644
index 00000000000..b51343ed3b5
--- /dev/null
+++ b/docs/developer/custom-jwt-covert-algorithm.md
@@ -0,0 +1,92 @@
+---
+title: Custom Jwt convert Algorithm
+keywords: ["Custom Jwt Convert"]
+description: Custom Jwt convert Algorithm
+---
+
+
+
+## Description
+
+* Users can customize the convert algorithm of `Jwt-plugin` to achieve convert.
+
+
+
+## Extension
+
+ The default implementation is `org.apache.shenyu.plugin.jwt.strategy.DefaultJwtConvertStrategy`. The SPI mechanism is adopted for extension, and the steps are as follows:
+
+1. Implements interface `org.apache.shenyu.plugin.jwt.strategy.JwtConvertStrategy`
+
+ ```java
+ /**
+ * Represents a conversion strategy that convert jwt to some attributes of
+ * serverWebExchange, especially attributes of the request header.
+ */
+ @SPI
+ public interface JwtConvertStrategy {
+
+ /**
+ * HandleJson needs to be parsed into jwtRuleHandle in order to
+ * specify how to convert jwt.
+ *
+ * @param handleJson handleJson from rule
+ * @return jwtRuleHandle
+ */
+ JwtRuleHandle parseHandleJson(String handleJson);
+
+ /**
+ * Converts jwt to some attributes of serverWebExchange based on jwtRuleHandle.
+ *
+ * @param jwtRuleHandle jwtRuleHandle
+ * @param exchange exchange
+ * @param jwtBody jwtBody
+ * @return serverWebExchange
+ */
+ ServerWebExchange convert(JwtRuleHandle jwtRuleHandle, ServerWebExchange exchange, Map<String, Object> jwtBody);
+
+ }
+ ```
+
+ ```java
+
+ @Join
+ public class CustomJwtConvertStrategy implements JwtConvertStrategy {
+
+ @Override
+ public CustomJwtRuleHandle parseHandleJson(final String handleJson) {
+
+ return GsonUtils.getInstance().fromJson(handleJson, CustomJwtRuleHandle.class);
+ }
+
+ @Override
+ public ServerWebExchange convert(final JwtRuleHandle jwtRuleHandle, final ServerWebExchange exchange, final Map<String, Object> jwtBody) {
+ final CustomJwtRuleHandle customJwtRuleHandle = (CustomJwtRuleHandle) jwtRuleHandle;
+ String customConvert = customJwtRuleHandle.getCustomConvert();
+ ServerHttpRequest modifiedRequest =
+ exchange.getRequest().mutate().header("custom", customConvert).build();
+
+ return exchange.mutate().request(modifiedRequest).build();
+ }
+ }
+ ```
+
+2. Configures SPI
+
+ ```tex
+ custom=org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy
+ ```
+
+
+
+The project would use different conversion strategies based on the`handleType` parameter of `JwtRuleHandle` . For example, for the following `JwtRuleHandle`,the project would use our above `CustomJwtConvertStrategy` . (Note: ` handleType ` is `default` or nonexistent, the project would use default `DefaultJwtConvertStrategy`)
+
+```json
+{
+ "handleType":"custom",
+ "customConvert":"customConvert"
+}
+```
+
+The case code is available for viewing in `org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy`
+
diff --git a/docs/plugin-center/security/jwt-plugin.md b/docs/plugin-center/security/jwt-plugin.md
index e98c8af478d..1408d0b42b5 100644
--- a/docs/plugin-center/security/jwt-plugin.md
+++ b/docs/plugin-center/security/jwt-plugin.md
@@ -70,6 +70,8 @@ description: JWT plugin
* jwtVal: jwt of body name
* headerVal: jwt header name
+custom covert algorithm:[custom-jwt-covert-algorithm](../../developer/custom-jwt-covert-algorithm.md)
+
## 2.5 Examples
### 2.5.1 Use jwt token for authentication judgment
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/developer/custom-jwt-covert-algorithm.md b/i18n/zh/docusaurus-plugin-content-docs/current/developer/custom-jwt-covert-algorithm.md
new file mode 100644
index 00000000000..b4a7cdfd73a
--- /dev/null
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/developer/custom-jwt-covert-algorithm.md
@@ -0,0 +1,93 @@
+---
+title: 自定义JWT插件转化算法
+description: 自定义JWT插件转化算法
+---
+
+
+
+## 说明
+
++ 用户可以自定义JWT插件中转化算法
+
+
+
+## 扩展
+
+ 转化算法的默认实现为 `org.apache.shenyu.plugin.jwt.strategy.DefaultJwtConvertStrategy`,采用的是SPI机制进行扩展,步骤如下:
+
+1. 实现接口`org.apache.shenyu.plugin.jwt.strategy.JwtConvertStrategy`
+
+ ```java
+ /**
+ * Represents a conversion strategy that convert jwt to some attributes of
+ * serverWebExchange, especially attributes of the request header.
+ */
+ @SPI
+ public interface JwtConvertStrategy {
+
+ /**
+ * HandleJson needs to be parsed into jwtRuleHandle in order to
+ * specify how to convert jwt.
+ *
+ * @param handleJson handleJson from rule
+ * @return jwtRuleHandle
+ */
+ JwtRuleHandle parseHandleJson(String handleJson);
+
+ /**
+ * Converts jwt to some attributes of serverWebExchange based on jwtRuleHandle.
+ *
+ * @param jwtRuleHandle jwtRuleHandle
+ * @param exchange exchange
+ * @param jwtBody jwtBody
+ * @return serverWebExchange
+ */
+ ServerWebExchange convert(JwtRuleHandle jwtRuleHandle, ServerWebExchange exchange, Map<String, Object> jwtBody);
+
+ }
+ ```
+
+ ```java
+ @Join
+ public class CustomJwtConvertStrategy implements JwtConvertStrategy {
+
+ @Override
+ public CustomJwtRuleHandle parseHandleJson(final String handleJson) {
+
+ return GsonUtils.getInstance().fromJson(handleJson, CustomJwtRuleHandle.class);
+ }
+
+ @Override
+ public ServerWebExchange convert(final JwtRuleHandle jwtRuleHandle, final ServerWebExchange exchange, final Map<String, Object> jwtBody) {
+ final CustomJwtRuleHandle customJwtRuleHandle = (CustomJwtRuleHandle) jwtRuleHandle;
+ String customConvert = customJwtRuleHandle.getCustomConvert();
+ ServerHttpRequest modifiedRequest =
+ exchange.getRequest().mutate().header("custom", customConvert).build();
+
+ return exchange.mutate().request(modifiedRequest).build();
+ }
+ }
+
+ ```
+
+
+
+2. 配置SPI
+
+ ```tex
+ custom=org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy
+ ```
+
+
+
+说明:系统会根据`JwtRuleHandle`的`handleType`参数来使用不同转化策略,比如下面的`JwtRuleHandle`系统会使用我们上面自定义的`CustomJwtConvertStrategy`。(注意:`handleType`为`default`或者不存在`handleType`属性,系统默认使用`DefaultJwtConvertStrategy`)
+
+```json
+{
+ "handleType":"custom",
+ "customConvert":"customConvert"
+}
+```
+
+案例代码可查看`org.apache.shenyu.plugin.jwt.strategy.CustomJwtConvertStrategy`
+
diff --git a/i18n/zh/docusaurus-plugin-content-docs/current/plugin-center/security/jwt-plugin.md b/i18n/zh/docusaurus-plugin-content-docs/current/plugin-center/security/jwt-plugin.md
index daaeac666dd..ce594023d9b 100644
--- a/i18n/zh/docusaurus-plugin-content-docs/current/plugin-center/security/jwt-plugin.md
+++ b/i18n/zh/docusaurus-plugin-content-docs/current/plugin-center/security/jwt-plugin.md
@@ -70,6 +70,8 @@ description: JWT插件
* jwtVal: jwt 请求体的名称
* headerVal: jwt请求头的名称
+自定义转化算法请查看:[自定义JWT插件转化算法](../../developer/custom-jwt-covert-algorithm.md)
+
## 2.5 示例
### 2.5.1 使用jwt插件进行权限认证