You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by xi...@apache.org on 2021/08/25 06:15:05 UTC
[incubator-shenyu] branch master updated: [new featrue] add cryptor
plugin (#1957)
This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new df27f61 [new featrue] add cryptor plugin (#1957)
df27f61 is described below
commit df27f61b6c96f6e24e2054422060fefed612258c
Author: Sinsy <55...@qq.com>
AuthorDate: Wed Aug 25 14:14:58 2021 +0800
[new featrue] add cryptor plugin (#1957)
* To optimize the jwt plugin (#1551)
* fix ci
* refactor param data build to SPI
* fix nacos error
* update image
* To optimize the jwt plugin
* feat cryptor plugin
* fix style
* refactor spi
* Fix typo (#1958)
* Update ci.yml (#1939)
* Update ci.yml
* Upgrade jacoco-maven-plugin version
* fix
* Upgrade spring-boot version
* fix
* fix
* fix
* revert
* fix
* cleanup
* set skip ci
* [Fix: build] fix maven ignore and docker entrypoint (#1964)
* [new feature] add shenyu ext plugin dynamic loading (#1968)
* [new feature] add shenyu ext plugin dynamic loading
* [new feature] add shenyu ext plugin dynamic loading
* Add integrated test for context path (#1965)
* [new feature] add shenyu can update local data (#1969)
* [new feature] add shenyu can update local data
* [new feature] add shenyu can update local data
* add agent package module (#1941)
* add motan demo.
* add agent main process.
* add agent mpackage module.
* Revert "add agent mpackage module."
This reverts commit 1fe8f338
* agent package.
* agent package.
* refactor spi
* remove redundant dependence
* refactor const
* remove contextPath
* refactor change dropDown
* remove dependency-reduced-pom.xml
* add shenyu_dict and refactor cryptor plugin ut
---
.../src/main/resources/META-INF/schema.h2.sql | 12 ++
.../src/main/resources/META-INF/schema.sql | 15 ++-
.../rule/impl/CryptorRequestRuleHandle.java | 101 +++++++++++++++
.../rule/impl/CryptorResponseRuleHandle.java | 95 +++++++++++++++
.../org/apache/shenyu/common/enums/PluginEnum.java | 10 ++
shenyu-plugin/pom.xml | 1 +
.../shenyu/plugin/api/result/ShenyuResultEnum.java | 15 +++
shenyu-plugin/shenyu-plugin-cryptor/pom.xml | 74 +++++++++++
.../plugin/cryptor/common/chain/CryptorChain.java} | 41 +++----
.../plugin/cryptor/common/chain/Executor.java} | 44 ++++---
.../cryptor/common/decorator/RequestDecorator.java | 60 +++++++++
.../common/decorator/ResponseDecorator.java | 47 +++++++
.../handler/CryptorRequestPluginDataHandler.java | 56 +++++++++
.../handler/CryptorResponsePluginDataHandler.java | 57 +++++++++
.../common/strategies/CryptorStrategy.java} | 42 +++----
.../cryptor/common/strategies/RsaStrategy.java | 57 +++++++++
.../plugin/cryptor/common/utils/HttpUtil.java | 75 ++++++++++++
.../plugin/cryptor/common/utils/JsonUtil.java | 135 +++++++++++++++++++++
.../cryptor/request/CryptorRequestPlugin.java | 123 +++++++++++++++++++
.../cryptor/response/CryptorResponsePlugin.java | 124 +++++++++++++++++++
...lugin.cryptor.common.strategies.CryptorStrategy | 17 +++
.../cryptor/request/CryptorRequestPluginTest.java | 103 ++++++++++++++++
.../response/CryptorResponsePluginTest.java | 122 +++++++++++++++++++
.../plugin/jwt/exception/ThrowingFunction.java | 1 -
.../shenyu-spring-boot-starter-plugin/pom.xml | 1 +
.../pom.xml | 52 ++++++++
.../plugin/cryptor/CryptorPluginConfiguration.java | 42 +++----
.../src/main/resources/META-INF/spring.factories | 19 +++
.../cryptor/CryptorPluginConfigurationTest.java | 53 ++++++++
29 files changed, 1500 insertions(+), 94 deletions(-)
diff --git a/shenyu-admin/src/main/resources/META-INF/schema.h2.sql b/shenyu-admin/src/main/resources/META-INF/schema.h2.sql
index 175e4bb..1820561 100644
--- a/shenyu-admin/src/main/resources/META-INF/schema.h2.sql
+++ b/shenyu-admin/src/main/resources/META-INF/schema.h2.sql
@@ -295,6 +295,7 @@ INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_v
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('50', 'automaticTransitionFromOpenToHalfOpenEnabled', 'AUTOMATIC_HALF_OPEN', 'open', 'true', '', 1, 1, '2021-07-18 22:59:17', '2021-07-18 22:59:17');
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('51', 'automaticTransitionFromOpenToHalfOpenEnabled', 'AUTOMATIC_HALF_OPEN', 'close', 'false', '', 2, 1, '2021-07-18 22:59:34', '2021-07-18 22:59:34');
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('52', 'paramType', 'PARAM_TYPE', 'domain', 'domain', 'domain', 8, 1, '2021-05-30 19:40:41', '2021-05-30 20:15:23');
+INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('53', 'strategyName', 'STRATEGY_NAME', 'rsa', 'rsa', 'rsa strategy', 1, 1, '2021-08-25 11:47:29', '2021-08-25 11:48:03');
/*plugin*/
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1','sign','authentication', 7, '0', '2018-06-14 10:17:35', '2018-06-14 10:17:35');
@@ -320,6 +321,8 @@ INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_crea
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('21', 'oauth2', 'authentication', 10, '0', '2021-06-18 10:53:42', '2021-06-18 10:53:42');
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, `enabled`, `date_created`, `date_updated`) VALUES ('22', 'param_mapping','http process', 5,'{"ruleHandlePageType":"custom"}', '0', '2021-06-17 22:34:44', '2021-06-17 22:36:00');
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, `enabled`, `date_created`, `date_updated`) VALUES ('23', 'modifyResponse', 'http process',23, '{"ruleHandlePageType":"custom"}', '0', '2021-05-30 21:26:37', '2021-05-30 23:26:11');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('24', 'cryptor_request', 1, 25, 1, '2021-08-06 13:55:21', '2021-08-17 16:35:41');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('25', 'cryptor_response', 1, 26, 1, '2021-08-06 13:55:30', '2021-08-13 16:03:40');
/**default admin user**/
INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`, `enabled`, `date_created`, `date_updated`) VALUES ('1','admin','bbiB8zbUo3z3oA0VqEB/IA==', '1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');
@@ -444,6 +447,15 @@ INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`
INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('89','19' ,'secretKey','secretKey',2, 3, 0, null, '2020-11-09 01:19:10', '2020-11-09 01:19:10');
INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('90','19' ,'filterPath','filterPath',2, 3, 1, null, '2021-06-12 19:17:10', '2021-06-12 19:17:10');
+/*insert plugin_handle data for plugin cryptor*/
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('101', '24', 'strategyName', 'strategyName', 3, 2, 1, NULL, '2021-08-06 14:35:50', '2021-08-06 14:35:50');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('103', '24', 'fieldNames', 'fieldNames', 2, 2, 3, NULL, '2021-08-06 14:37:13', '2021-08-06 14:37:46');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('104', '24', 'key', 'key', 2, 2, 3, NULL, '2021-08-06 14:37:22', '2021-08-06 14:37:48');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('108', '25', 'strategyName', 'strategyName', 3, 2, 2, NULL, '2021-08-13 15:10:53', '2021-08-13 15:11:11');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('109', '25', 'key', 'key', 2, 2, 3, NULL, '2021-08-13 15:14:07', '2021-08-13 15:14:36');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('110', '25', 'fieldNames', 'fieldNames', 2, 2, 4, NULL, '2021-08-13 15:16:30', '2021-08-13 15:16:45');
+
+
/** insert permission role for role */
INSERT IGNORE INTO `role` (`id`,`role_name`,`description`,`date_created`,`date_updated`) VALUES ('1346358560427216896', 'super', '超级管理员', '2021-01-05 01:31:10', '2021-01-08 17:00:07');
INSERT IGNORE INTO `role` (`id`,`role_name`,`description`,`date_created`,`date_updated`) VALUES ('1385482862971723776', 'default', '普通用户', '2021-04-23 14:37:10', '2021-04-23 14:38:39');
diff --git a/shenyu-admin/src/main/resources/META-INF/schema.sql b/shenyu-admin/src/main/resources/META-INF/schema.sql
index 16fad8c..59b2d78 100644
--- a/shenyu-admin/src/main/resources/META-INF/schema.sql
+++ b/shenyu-admin/src/main/resources/META-INF/schema.sql
@@ -52,7 +52,7 @@ CREATE TABLE IF NOT EXISTS `plugin_handle` (
`data_type` smallint(6) NOT NULL DEFAULT '1' COMMENT 'data type 1 number 2 string',
`type` smallint(6) NULL COMMENT 'type, 1 means selector, 2 means rule, 3 means plugin',
`sort` int(4) NULL COMMENT 'sort',
- `ext_obj` varchar(1024) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'extra configuration (json format data)',
+ `ext_obj` varchar(4096) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'extra configuration (json format data)',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create time',
`date_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update time',
PRIMARY KEY (`id`),
@@ -299,6 +299,7 @@ INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_v
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('50', 'automaticTransitionFromOpenToHalfOpenEnabled', 'AUTOMATIC_HALF_OPEN', 'open', 'true', '', 1, 1, '2021-07-18 22:59:17', '2021-07-18 22:59:17');
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('51', 'automaticTransitionFromOpenToHalfOpenEnabled', 'AUTOMATIC_HALF_OPEN', 'close', 'false', '', 2, 1, '2021-07-18 22:59:34', '2021-07-18 22:59:34');
INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('52', 'paramType', 'PARAM_TYPE', 'domain', 'domain', 'domain', 8, 1, '2021-05-30 19:40:41', '2021-05-30 20:15:23');
+INSERT IGNORE INTO `shenyu_dict` (`id`, `type`,`dict_code`, `dict_name`, `dict_value`, `desc`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('53', 'strategyName', 'STRATEGY_NAME', 'rsa', 'rsa', 'rsa strategy', 1, 1, '2021-08-25 11:47:29', '2021-08-25 11:48:03');
/*plugin*/
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('1','sign','authentication', 7, '0', '2018-06-14 10:17:35', '2018-06-14 10:17:35');
@@ -324,6 +325,9 @@ INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_crea
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('21', 'oauth2', 'authentication', 10, '0', '2021-06-18 10:53:42', '2021-06-18 10:53:42');
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, `enabled`, `date_created`, `date_updated`) VALUES ('22', 'param_mapping','http process', 5,'{"ruleHandlePageType":"custom"}', '0', '2021-06-17 22:34:44', '2021-06-17 22:36:00');
INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `config`, `enabled`, `date_created`, `date_updated`) VALUES ('23', 'modifyResponse', 'http process',23, '{"ruleHandlePageType":"custom"}', '0', '2021-05-30 21:26:37', '2021-05-30 23:26:11');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('24', 'cryptor_request', 1, 25, 1, '2021-08-06 13:55:21', '2021-08-17 16:35:41');
+INSERT IGNORE INTO `plugin` (`id`, `name`, `role`, `sort`, `enabled`, `date_created`, `date_updated`) VALUES ('25', 'cryptor_response', 1, 26, 1, '2021-08-06 13:55:30', '2021-08-13 16:03:40');
+
/**default admin user**/
INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`, `enabled`, `date_created`, `date_updated`) VALUES ('1','admin','bbiB8zbUo3z3oA0VqEB/IA==', '1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');
@@ -447,6 +451,15 @@ INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`
INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('89','19' ,'secretKey','secretKey',2, 3, 0, null, '2020-11-09 01:19:10', '2020-11-09 01:19:10');
INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('90','19' ,'filterPath','filterPath',2, 3, 1, null, '2021-06-12 19:17:10', '2021-06-12 19:17:10');
+/*insert plugin_handle data for plugin cryptor*/
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('101', '24', 'strategyName', 'strategyName', 3, 2, 1, NULL, '2021-08-06 14:35:50', '2021-08-06 14:35:50');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('103', '24', 'fieldNames', 'fieldNames', 2, 2, 3, NULL, '2021-08-06 14:37:13', '2021-08-06 14:37:46');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('104', '24', 'key', 'key', 2, 2, 3, NULL, '2021-08-06 14:37:22', '2021-08-06 14:37:48');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('108', '25', 'strategyName', 'strategyName', 3, 2, 2, NULL, '2021-08-13 15:10:53', '2021-08-13 15:11:11');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('109', '25', 'key', 'key', 2, 2, 3, NULL, '2021-08-13 15:14:07', '2021-08-13 15:14:36');
+INSERT IGNORE INTO plugin_handle (`id`,`plugin_id`,`field`,`label`,`data_type`,`type`,`sort`,`ext_obj`,`date_created`,`date_updated`) VALUES ('110', '25', 'fieldNames', 'fieldNames', 2, 2, 4, NULL, '2021-08-13 15:16:30', '2021-08-13 15:16:45');
+
+
/** insert permission role for role */
INSERT IGNORE INTO `role` (`id`,`role_name`,`description`,`date_created`,`date_updated`) VALUES ('1346358560427216896', 'super', '超级管理员', '2021-01-05 01:31:10', '2021-01-08 17:00:07');
INSERT IGNORE INTO `role` (`id`,`role_name`,`description`,`date_created`,`date_updated`) VALUES ('1385482862971723776', 'default', '普通用户', '2021-04-23 14:37:10', '2021-04-23 14:38:39');
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorRequestRuleHandle.java b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorRequestRuleHandle.java
new file mode 100644
index 0000000..ae53f80
--- /dev/null
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorRequestRuleHandle.java
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.common.dto.convert.rule.impl;
+
+import org.apache.shenyu.common.dto.convert.rule.RuleHandle;
+
+/**
+ * Cryptor request rule handle.
+ */
+public class CryptorRequestRuleHandle implements RuleHandle {
+
+ private String strategyName;
+
+ private String fieldNames;
+
+ private String key;
+
+ /**
+ * no constructor.
+ */
+ public CryptorRequestRuleHandle() {
+ }
+
+ /**
+ * get strategyName.
+ * @return strategyName
+ */
+ public String getStrategyName() {
+ return strategyName;
+ }
+
+ /**
+ * set strategyName.
+ * @param strategyName strategyName
+ */
+ public void setStrategyName(final String strategyName) {
+ this.strategyName = strategyName;
+ }
+
+ /**
+ * get fieldNames.
+ * @return fieldNames
+ */
+ public String getFieldNames() {
+ return fieldNames;
+ }
+
+ /**
+ * set fieldNames.
+ * @param fieldNames fieldNames
+ */
+ public void setFieldNames(final String fieldNames) {
+ this.fieldNames = fieldNames;
+ }
+
+ /**
+ * get key.
+ * @return key
+ */
+ public String getKey() {
+ return key;
+ }
+
+ /**
+ * set key.
+ * @param key key
+ */
+ public void setKey(final String key) {
+ this.key = key;
+ }
+
+ @Override
+ public String toString() {
+ return "CryptorRequestRuleHandle{"
+ + ", strategyName='" + strategyName + '\''
+ + ", fieldNames='" + fieldNames + '\''
+ + ", key='" + key + '\''
+ + '}';
+ }
+
+ @Override
+ public RuleHandle createDefault(final String path) {
+ return this;
+ }
+
+}
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorResponseRuleHandle.java b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorResponseRuleHandle.java
new file mode 100644
index 0000000..d5f24dc
--- /dev/null
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/dto/convert/rule/impl/CryptorResponseRuleHandle.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.common.dto.convert.rule.impl;
+
+import org.apache.shenyu.common.dto.convert.rule.RuleHandle;
+
+/**
+ * Cryptor response rule handle.
+ */
+public class CryptorResponseRuleHandle implements RuleHandle {
+
+ private String strategyName;
+
+ private String key;
+
+ private String fieldNames;
+
+ /**
+ * get strategyName.
+ * @return strategyName
+ */
+ public String getStrategyName() {
+ return strategyName;
+ }
+
+ /**
+ * set strategyName.
+ * @param strategyName strategyName
+ */
+ public void setStrategyName(final String strategyName) {
+ this.strategyName = strategyName;
+ }
+
+ /**
+ * get key.
+ * @return key
+ */
+ public String getKey() {
+ return key;
+ }
+
+ /**
+ * set key.
+ * @param key key
+ */
+ public void setKey(final String key) {
+ this.key = key;
+ }
+
+ /**
+ * get fieldNames.
+ * @return fieldNames
+ */
+ public String getFieldNames() {
+ return fieldNames;
+ }
+
+ /**
+ * set fieldNames.
+ * @param fieldNames fieldNames
+ */
+ public void setFieldNames(final String fieldNames) {
+ this.fieldNames = fieldNames;
+ }
+
+ @Override
+ public String toString() {
+ return "CryptorResponseRuleHandle{"
+ + ", strategyName='" + strategyName + '\''
+ + ", key='" + key + '\''
+ + ", fieldNames='" + fieldNames + '\''
+ + '}';
+ }
+
+ @Override
+ public RuleHandle createDefault(final String path) {
+ return this;
+ }
+
+}
diff --git a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
index 7682f7a..0c6f12d 100644
--- a/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
+++ b/shenyu-common/src/main/java/org/apache/shenyu/common/enums/PluginEnum.java
@@ -71,6 +71,11 @@ public enum PluginEnum {
REWRITE(30, 0, "rewrite"),
/**
+ * Cryptor request plugin enum.
+ */
+ CRYPTOR_REQUEST(30, 0, "cryptor_request"),
+
+ /**
* Redirect plugin enum.
*/
REDIRECT(40, 0, "redirect"),
@@ -156,6 +161,11 @@ public enum PluginEnum {
MONITOR(80, 0, "monitor"),
/**
+ * Cryptor response plugin enum.
+ */
+ CRYPTOR_RESPONSE(99, 0, "cryptor_response"),
+
+ /**
* Response plugin enum.
*/
RESPONSE(100, 0, "response");
diff --git a/shenyu-plugin/pom.xml b/shenyu-plugin/pom.xml
index b5c0205..925fc5d 100644
--- a/shenyu-plugin/pom.xml
+++ b/shenyu-plugin/pom.xml
@@ -57,5 +57,6 @@
<module>shenyu-plugin-jwt</module>
<module>shenyu-plugin-response</module>
<module>shenyu-plugin-param-mapping</module>
+ <module>shenyu-plugin-cryptor</module>
</modules>
</project>
diff --git a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResultEnum.java b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResultEnum.java
index 33d7c9c..1c427fc 100644
--- a/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResultEnum.java
+++ b/shenyu-plugin/shenyu-plugin-api/src/main/java/org/apache/shenyu/plugin/api/result/ShenyuResultEnum.java
@@ -174,6 +174,21 @@ public enum ShenyuResultEnum {
RESPONSE_ERROR(-113, "Response error, please check your configuration!"),
/**
+ * Cryptor request field error.
+ */
+ CRYPTOR_REQUEST_ERROR_CONFIGURATION(-114, "Please check Cryptor request plugin's "),
+
+ /**
+ * Cryptor response field error.
+ */
+ CRYPTOR_RESPONSE_ERROR_CONFIGURATION(-116, "Please check Cryptor response plugin's "),
+
+ /**
+ * Decryption error.
+ */
+ DECRYPTION_ERROR(-117, "Decryption failed, please check the parameters or key, or the data length is too long"),
+
+ /**
* Request Header Fields Too Large.
*/
REQUEST_HEADER_TOO_LARGE(431, "Request Header Fields Too Large"),
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/pom.xml b/shenyu-plugin/shenyu-plugin-cryptor/pom.xml
new file mode 100644
index 0000000..01666bc
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/pom.xml
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>shenyu-plugin</artifactId>
+ <groupId>org.apache.shenyu</groupId>
+ <version>2.4.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>shenyu-plugin-cryptor</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-plugin-base</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-metrics-facade</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>2.4</version>
+ </dependency>
+
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-web</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+
+ <dependency>
+ <groupId>io.netty</groupId>
+ <artifactId>netty-buffer</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>io.projectreactor</groupId>
+ <artifactId>reactor-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/CryptorChain.java
similarity index 54%
copy from shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
copy to shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/CryptorChain.java
index 58d08fb..07ef810 100644
--- a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/CryptorChain.java
@@ -15,38 +15,31 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.jwt.exception;
+package org.apache.shenyu.plugin.cryptor.common.chain;
+
+import org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy;
/**
- * Throwing wrap.
- * Handle exception, and return null.
- * Not all exceptions must be try catch, throw upwards, return null, you can use if to judge.
+ * Cryptor chain.
*/
-@FunctionalInterface
-public interface ThrowingFunction<T> {
+public interface CryptorChain {
/**
- * apply function.
- *
- * @return T
- * @throws Exception any error
+ * decrypt.
+ * @param cryptorStrategy strategy
+ * @param key key
+ * @param encryptData encryptData
+ * @return data.
*/
- T apply() throws Exception;
+ String decryptExecute(CryptorStrategy cryptorStrategy, String key, String encryptData);
/**
- * operation function.
- * If an exception occurs, it will directly return a null.
- *
- * @param function apply function.
- * @param <T> t
- * @return T
+ * encrypt.
+ * @param cryptorStrategy strategy
+ * @param key key
+ * @param data data
+ * @return encryptData.
*/
- static <T> T wrap(ThrowingFunction<T> function) {
- try {
- return function.apply();
- } catch (Exception e) {
- return null;
- }
- }
+ String encryptExecute(CryptorStrategy cryptorStrategy, String key, String data);
}
diff --git a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/Executor.java
similarity index 50%
copy from shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
copy to shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/Executor.java
index 58d08fb..7b96c2d 100644
--- a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/chain/Executor.java
@@ -15,38 +15,36 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.jwt.exception;
+package org.apache.shenyu.plugin.cryptor.common.chain;
+
+import org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
- * Throwing wrap.
- * Handle exception, and return null.
- * Not all exceptions must be try catch, throw upwards, return null, you can use if to judge.
+ * Executor.
*/
-@FunctionalInterface
-public interface ThrowingFunction<T> {
+public class Executor implements CryptorChain {
- /**
- * apply function.
- *
- * @return T
- * @throws Exception any error
- */
- T apply() throws Exception;
+ private static final Logger LOG = LoggerFactory.getLogger(Executor.class);
- /**
- * operation function.
- * If an exception occurs, it will directly return a null.
- *
- * @param function apply function.
- * @param <T> t
- * @return T
- */
- static <T> T wrap(ThrowingFunction<T> function) {
+ @Override
+ public String decryptExecute(final CryptorStrategy cryptorStrategy, final String key, final String encryptData) {
try {
- return function.apply();
+ return cryptorStrategy.decrypt(key, encryptData);
} catch (Exception e) {
+ LOG.error("decrypt data error: ", e);
return null;
}
}
+ @Override
+ public String encryptExecute(final CryptorStrategy cryptorStrategy, final String key, final String data) {
+ try {
+ return cryptorStrategy.encrypt(key, data);
+ } catch (Exception e) {
+ LOG.error("encrypt data error: ", e);
+ return null;
+ }
+ }
}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/RequestDecorator.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/RequestDecorator.java
new file mode 100644
index 0000000..af556dd
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/RequestDecorator.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.decorator;
+
+import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
+import org.apache.shenyu.plugin.cryptor.common.utils.HttpUtil;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Flux;
+import reactor.util.annotation.NonNull;
+
+/**
+ * Build and modify the request class.
+ */
+public class RequestDecorator extends ServerHttpRequestDecorator {
+
+ private final CachedBodyOutputMessage cachedBodyOutputMessage;
+
+ private final ServerWebExchange exchange;
+
+ public RequestDecorator(final ServerWebExchange exchange,
+ final CachedBodyOutputMessage cachedBodyOutputMessage) {
+ super(exchange.getRequest());
+ this.cachedBodyOutputMessage = cachedBodyOutputMessage;
+ this.exchange = exchange;
+ }
+
+ @Override
+ @NonNull
+ public Flux<DataBuffer> getBody() {
+ return cachedBodyOutputMessage.getBody();
+ }
+
+ @Override
+ @NonNull
+ public HttpHeaders getHeaders() {
+ HttpHeaders headers = new HttpHeaders();
+ headers.putAll(exchange.getRequest().getHeaders());
+ headers.remove(HttpHeaders.CONTENT_LENGTH);
+ return HttpUtil.modifiedContentLength(headers);
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/ResponseDecorator.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/ResponseDecorator.java
new file mode 100644
index 0000000..f73e328
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/decorator/ResponseDecorator.java
@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.decorator;
+
+import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
+import org.reactivestreams.Publisher;
+import org.springframework.core.io.buffer.DataBuffer;
+import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+import reactor.util.annotation.NonNull;
+
+/**
+ * Build and modify the response class.
+ */
+public class ResponseDecorator extends ServerHttpResponseDecorator {
+
+ private final CachedBodyOutputMessage cachedBodyOutputMessage;
+
+ public ResponseDecorator(final ServerWebExchange exchange,
+ final CachedBodyOutputMessage cachedBodyOutputMessage) {
+ super(exchange.getResponse());
+ this.cachedBodyOutputMessage = cachedBodyOutputMessage;
+ }
+
+ @Override
+ @NonNull
+ public Mono<Void> writeWith(@NonNull final Publisher<? extends DataBuffer> body) {
+ return getDelegate().writeWith(cachedBodyOutputMessage.getBody());
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorRequestPluginDataHandler.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorRequestPluginDataHandler.java
new file mode 100644
index 0000000..d0b89d6
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorRequestPluginDataHandler.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.handler;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.convert.rule.impl.CryptorRequestRuleHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.plugin.base.cache.RuleHandleCache;
+import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
+import org.apache.shenyu.plugin.base.utils.BeanHolder;
+import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+
+import java.util.Optional;
+import java.util.function.Supplier;
+
+/**
+ * The type cryptor request plugin plugin data subscriber.
+ */
+public class CryptorRequestPluginDataHandler implements PluginDataHandler {
+
+ public static final Supplier<RuleHandleCache<String, CryptorRequestRuleHandle>> CACHED_HANDLE = new BeanHolder(() -> new RuleHandleCache());
+
+ @Override
+ public void handlerRule(final RuleData ruleData) {
+ Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> {
+ CryptorRequestRuleHandle cryptorRequestRuleHandle = GsonUtils.getInstance().fromJson(s, CryptorRequestRuleHandle.class);
+ CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), cryptorRequestRuleHandle);
+ });
+ }
+
+ @Override
+ public void removeRule(final RuleData ruleData) {
+ Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> CACHED_HANDLE.get().removeHandle(CacheKeyUtils.INST.getKey(ruleData)));
+ }
+
+ @Override
+ public String pluginNamed() {
+ return PluginEnum.CRYPTOR_REQUEST.getName();
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorResponsePluginDataHandler.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorResponsePluginDataHandler.java
new file mode 100644
index 0000000..68da31e
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/handler/CryptorResponsePluginDataHandler.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.handler;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.convert.rule.impl.CryptorResponseRuleHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.GsonUtils;
+import org.apache.shenyu.plugin.base.cache.RuleHandleCache;
+import org.apache.shenyu.plugin.base.handler.PluginDataHandler;
+import org.apache.shenyu.plugin.base.utils.BeanHolder;
+import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+
+import java.util.Optional;
+import java.util.function.Supplier;
+
+/**
+ * The type cryptor response plugin data subscriber.
+ */
+public class CryptorResponsePluginDataHandler implements PluginDataHandler {
+
+ public static final Supplier<RuleHandleCache<String, CryptorResponseRuleHandle>> CACHED_HANDLE = new BeanHolder(() -> new RuleHandleCache());
+
+ @Override
+ public void handlerRule(final RuleData ruleData) {
+ Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> {
+ CryptorResponseRuleHandle cryptorResponseRuleHandle = GsonUtils.getInstance().fromJson(s, CryptorResponseRuleHandle.class);
+ CACHED_HANDLE.get().cachedHandle(CacheKeyUtils.INST.getKey(ruleData), cryptorResponseRuleHandle);
+ });
+ }
+
+ @Override
+ public void removeRule(final RuleData ruleData) {
+
+ Optional.ofNullable(ruleData.getHandle()).ifPresent(s -> CACHED_HANDLE.get().removeHandle(CacheKeyUtils.INST.getKey(ruleData)));
+ }
+
+ @Override
+ public String pluginNamed() {
+ return PluginEnum.CRYPTOR_RESPONSE.getName();
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/CryptorStrategy.java
similarity index 54%
copy from shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
copy to shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/CryptorStrategy.java
index 58d08fb..ff4b1a9 100644
--- a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/CryptorStrategy.java
@@ -15,38 +15,32 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.jwt.exception;
+package org.apache.shenyu.plugin.cryptor.common.strategies;
+
+import org.apache.shenyu.spi.SPI;
/**
- * Throwing wrap.
- * Handle exception, and return null.
- * Not all exceptions must be try catch, throw upwards, return null, you can use if to judge.
+ * strategy.
*/
-@FunctionalInterface
-public interface ThrowingFunction<T> {
+@SPI
+public interface CryptorStrategy {
/**
- * apply function.
- *
- * @return T
- * @throws Exception any error
+ * decrypt.
+ * @param key key
+ * @param encryptData encryptData
+ * @return data
+ * @throws Exception error
*/
- T apply() throws Exception;
+ String decrypt(String key, String encryptData) throws Exception;
/**
- * operation function.
- * If an exception occurs, it will directly return a null.
- *
- * @param function apply function.
- * @param <T> t
- * @return T
+ * encrypt.
+ * @param key key
+ * @param data data
+ * @return encryptData.
+ * @throws Exception error
*/
- static <T> T wrap(ThrowingFunction<T> function) {
- try {
- return function.apply();
- } catch (Exception e) {
- return null;
- }
- }
+ String encrypt(String key, String data) throws Exception;
}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/RsaStrategy.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/RsaStrategy.java
new file mode 100644
index 0000000..29282b8
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/strategies/RsaStrategy.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.strategies;
+
+import org.apache.shenyu.spi.Join;
+
+import javax.crypto.Cipher;
+import java.nio.charset.StandardCharsets;
+import java.security.KeyFactory;
+import java.security.PrivateKey;
+import java.security.interfaces.RSAPublicKey;
+import java.security.spec.PKCS8EncodedKeySpec;
+import java.security.spec.X509EncodedKeySpec;
+import java.util.Base64;
+
+/**
+ * rsa cryptor.
+ */
+@Join
+public class RsaStrategy implements CryptorStrategy {
+
+ private static final String RSA = "rsa";
+
+ @Override
+ public String decrypt(final String key, final String encryptData) throws Exception {
+ byte[] inputByte = Base64.getDecoder().decode(encryptData.getBytes(StandardCharsets.UTF_8));
+ byte[] decoded = Base64.getDecoder().decode(key);
+ PrivateKey priKey = KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decoded));
+ Cipher cipher = Cipher.getInstance(RSA);
+ cipher.init(Cipher.DECRYPT_MODE, priKey);
+ return new String(cipher.doFinal(inputByte));
+ }
+
+ @Override
+ public String encrypt(final String key, final String data) throws Exception {
+ byte[] decoded = Base64.getDecoder().decode(key);
+ RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance(RSA).generatePublic(new X509EncodedKeySpec(decoded));
+ Cipher cipher = Cipher.getInstance(RSA);
+ cipher.init(Cipher.ENCRYPT_MODE, pubKey);
+ return Base64.getEncoder().encodeToString(cipher.doFinal(data.getBytes(StandardCharsets.UTF_8)));
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/HttpUtil.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/HttpUtil.java
new file mode 100644
index 0000000..0025c70
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/HttpUtil.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.utils;
+
+import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
+import org.springframework.core.io.buffer.DataBufferUtils;
+import org.springframework.http.HttpHeaders;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+/**
+ * http util.
+ */
+public class HttpUtil {
+
+ private static final String CHUNKED = "chunked";
+
+ /**
+ * change header.
+ * @param headers headers
+ * @return HttpHeaders.
+ */
+ public static HttpHeaders modifiedContentLength(final HttpHeaders headers) {
+ long contentLength = headers.getContentLength();
+ HttpHeaders httpHeaders = new HttpHeaders();
+ httpHeaders.putAll(headers);
+ if (contentLength > 0) {
+ httpHeaders.setContentLength(contentLength);
+ } else {
+ httpHeaders.set(HttpHeaders.TRANSFER_ENCODING, CHUNKED);
+ }
+ return httpHeaders;
+ }
+
+ /**
+ * create CachedBodyOutputMessage.
+ * @param exchange ServerWebExchange
+ * @return CachedBodyOutputMessage.
+ */
+ public static CachedBodyOutputMessage newCachedBodyOutputMessage(final ServerWebExchange exchange) {
+ HttpHeaders headers = new HttpHeaders();
+ headers.putAll(exchange.getRequest().getHeaders());
+ headers.remove(HttpHeaders.CONTENT_LENGTH);
+ return new CachedBodyOutputMessage(exchange, headers);
+ }
+
+ /**
+ * release source.
+ * @param outputMessage CachedBodyOutputMessage
+ * @param throwable Throwable
+ * @return Mono.
+ */
+ public static Mono<Void> release(final CachedBodyOutputMessage outputMessage, final Throwable throwable) {
+ if (outputMessage.getCache()) {
+ return outputMessage.getBody().map(DataBufferUtils::release).then(Mono.error(throwable));
+ }
+ return Mono.error(throwable);
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/JsonUtil.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/JsonUtil.java
new file mode 100644
index 0000000..1719668
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/common/utils/JsonUtil.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.common.utils;
+
+import com.google.gson.JsonArray;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.common.utils.GsonUtils;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * json util.
+ */
+public class JsonUtil {
+
+ private static String errorCollector;
+
+ /**
+ * parser JSON.
+ * @param json json Map
+ * @param fieldNames params
+ * @return str.
+ */
+ public static String parser(final String json, final String fieldNames) {
+ Map<String, Object> map = GsonUtils.getInstance().toObjectMap(json);
+ String str = null;
+ if (fieldNames.contains(".")) {
+ String[] split = fieldNames.split("\\.");
+ JsonObject jsonObject = (JsonObject) map.get(split[0]);
+ for (int i = 1; i < split.length; i++) {
+ if (i == split.length - 1) {
+ str = jsonObject.getAsJsonPrimitive(split[i]).getAsString();
+ } else {
+ jsonObject = jsonObject.getAsJsonObject(split[i]);
+ }
+ }
+ } else {
+ return map.get(fieldNames) == null ? null : map.get(fieldNames).toString();
+ }
+ return str;
+ }
+
+ /**
+ * check param.
+ * @param json json
+ * @return is null
+ */
+ public static boolean checkParam(final String json) {
+ Map<String, String> map = GsonUtils.getInstance().toObjectMap(json, String.class);
+ for (Map.Entry<String, String> entry : map.entrySet()) {
+ if (StringUtils.isEmpty(map.get(entry.getKey()))) {
+ errorCollector = entry.getKey();
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * get error param.
+ * @return error param.
+ */
+ public static String getErrorCollector() {
+ return errorCollector;
+ }
+
+ /**
+ * operate json.
+ * @param jsonElement jsonElement
+ * @param initDeep default 0
+ * @param value The value that needs to be modified
+ * @param deepKey json link
+ * @return JsonElement
+ */
+ public static JsonElement replaceJsonNode(final JsonElement jsonElement,
+ final AtomicInteger initDeep,
+ final String value,
+ final List<String> deepKey) {
+ if (deepKey.size() == 0) {
+ return jsonElement;
+ }
+ if (jsonElement.isJsonPrimitive()) {
+ return jsonElement;
+ }
+ if (jsonElement.isJsonArray()) {
+ JsonArray jsonArray = jsonElement.getAsJsonArray();
+ JsonArray jsonArrayNew = new JsonArray();
+ for (JsonElement element : jsonArray) {
+ jsonArrayNew.add(replaceJsonNode(element, initDeep, value, deepKey));
+ }
+ return jsonArrayNew;
+ }
+
+ if (jsonElement.isJsonObject()) {
+ JsonObject object = jsonElement.getAsJsonObject();
+ JsonObject objectNew = new JsonObject();
+ for (Map.Entry<String, JsonElement> entry : object.entrySet()) {
+
+ if (deepKey.get(initDeep.get()).equals(entry.getKey())) {
+ initDeep.incrementAndGet();
+ }
+ String key = entry.getKey();
+ if (initDeep.get() == deepKey.size()) {
+ initDeep.set(deepKey.size() - 1);
+ object.addProperty(key, value);
+ }
+ JsonElement jsonEle = object.get(key);
+ JsonElement jsonElementNew = replaceJsonNode(jsonEle, initDeep, value, deepKey);
+ objectNew.add(key, jsonElementNew);
+ }
+ return objectNew;
+ }
+ return jsonElement;
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
new file mode 100644
index 0000000..634f1e0
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPlugin.java
@@ -0,0 +1,123 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.request;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.impl.CryptorRequestRuleHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
+import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
+import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
+import org.apache.shenyu.plugin.base.support.BodyInserterContext;
+import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
+import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+import org.apache.shenyu.plugin.cryptor.common.chain.Executor;
+import org.apache.shenyu.plugin.cryptor.common.decorator.RequestDecorator;
+import org.apache.shenyu.plugin.cryptor.common.handler.CryptorRequestPluginDataHandler;
+import org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy;
+import org.apache.shenyu.plugin.cryptor.common.utils.HttpUtil;
+import org.apache.shenyu.plugin.cryptor.common.utils.JsonUtil;
+import org.apache.shenyu.spi.ExtensionLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.ReactiveHttpOutputMessage;
+import org.springframework.http.codec.HttpMessageReader;
+import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
+import org.springframework.web.reactive.function.BodyInserter;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.server.HandlerStrategies;
+import org.springframework.web.reactive.function.server.ServerRequest;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+import java.util.Objects;
+import java.util.function.Function;
+
+/**
+ * Cryptor request plugin.
+ */
+public class CryptorRequestPlugin extends AbstractShenyuPlugin {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CryptorRequestPlugin.class);
+
+ private static final List<HttpMessageReader<?>> MESSAGE_READERS = HandlerStrategies.builder().build().messageReaders();
+
+ private final Executor executor = new Executor();
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
+ CryptorRequestRuleHandle ruleHandle = CryptorRequestPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
+ if (Objects.isNull(ruleHandle)) {
+ LOG.error("Cryptor request rule configuration is null :{}", rule.getId());
+ return chain.execute(exchange);
+ }
+ CachedBodyOutputMessage outputMessage = HttpUtil.newCachedBodyOutputMessage(exchange);
+ if (JsonUtil.checkParam(ruleHandle.toJson())) {
+ Object error = ShenyuResultWrap.error(ShenyuResultEnum.CRYPTOR_REQUEST_ERROR_CONFIGURATION.getCode(),
+ ShenyuResultEnum.CRYPTOR_REQUEST_ERROR_CONFIGURATION.getMsg()
+ + "[" + JsonUtil.getErrorCollector() + "]", null);
+ return WebFluxResultUtils.result(exchange, error);
+ }
+
+ ServerRequest serverRequest = ServerRequest.create(exchange, MESSAGE_READERS);
+ Mono<String> mono = serverRequest.bodyToMono(String.class)
+ .switchIfEmpty(Mono.defer(() -> Mono.just("")))
+ .flatMap(originalBody -> strategyMatch(ruleHandle, originalBody, exchange));
+
+ BodyInserter<Mono<String>, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromPublisher(mono, String.class);
+ return bodyInserter.insert(outputMessage, new BodyInserterContext())
+ .then(Mono.defer(() -> {
+ ServerHttpRequestDecorator decorator = new RequestDecorator(exchange, outputMessage);
+ return chain.execute(exchange.mutate().request(decorator).build());
+ })).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> HttpUtil.release(outputMessage, throwable));
+
+ }
+
+ @Override
+ public int getOrder() {
+ return PluginEnum.CRYPTOR_REQUEST.getCode();
+ }
+
+ @Override
+ public String named() {
+ return PluginEnum.CRYPTOR_REQUEST.getName();
+ }
+
+ private Mono strategyMatch(final CryptorRequestRuleHandle ruleHandle, final String originalBody, final ServerWebExchange exchange) {
+ CryptorStrategy strategy = ExtensionLoader.getExtensionLoader(CryptorStrategy.class).getJoin(ruleHandle.getStrategyName());
+ String parseBody = JsonUtil.parser(originalBody, ruleHandle.getFieldNames());
+ if (null == parseBody) {
+ Object error = ShenyuResultWrap.error(ShenyuResultEnum.CRYPTOR_REQUEST_ERROR_CONFIGURATION.getCode(),
+ ShenyuResultEnum.CRYPTOR_REQUEST_ERROR_CONFIGURATION.getMsg() + "[fieldNames]", null);
+ return WebFluxResultUtils.result(exchange, error);
+ }
+ String modifiedBody = executor.decryptExecute(strategy, ruleHandle.getKey(), parseBody);
+
+ if (modifiedBody == null) {
+ Object error = ShenyuResultWrap.error(ShenyuResultEnum.DECRYPTION_ERROR.getCode(), ShenyuResultEnum.DECRYPTION_ERROR.getMsg(), null);
+ return WebFluxResultUtils.result(exchange, error);
+ }
+ return Mono.just(modifiedBody);
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
new file mode 100644
index 0000000..59b2615
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePlugin.java
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.response;
+
+import com.google.gson.JsonElement;
+import com.google.gson.JsonParser;
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.dto.convert.rule.impl.CryptorResponseRuleHandle;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.api.result.ShenyuResultEnum;
+import org.apache.shenyu.plugin.api.result.ShenyuResultWrap;
+import org.apache.shenyu.plugin.api.utils.WebFluxResultUtils;
+import org.apache.shenyu.plugin.base.AbstractShenyuPlugin;
+import org.apache.shenyu.plugin.base.support.BodyInserterContext;
+import org.apache.shenyu.plugin.base.support.CachedBodyOutputMessage;
+import org.apache.shenyu.plugin.base.utils.CacheKeyUtils;
+import org.apache.shenyu.plugin.cryptor.common.chain.Executor;
+import org.apache.shenyu.plugin.cryptor.common.decorator.ResponseDecorator;
+import org.apache.shenyu.plugin.cryptor.common.handler.CryptorResponsePluginDataHandler;
+import org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy;
+import org.apache.shenyu.plugin.cryptor.common.utils.HttpUtil;
+import org.apache.shenyu.plugin.cryptor.common.utils.JsonUtil;
+import org.apache.shenyu.spi.ExtensionLoader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.ReactiveHttpOutputMessage;
+import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
+import org.springframework.web.reactive.function.BodyInserter;
+import org.springframework.web.reactive.function.BodyInserters;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+
+import java.util.Arrays;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Function;
+
+/**
+ * Cryptor response plugin.
+ */
+public class CryptorResponsePlugin extends AbstractShenyuPlugin {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CryptorResponsePlugin.class);
+
+ private final Executor executor = new Executor();
+
+ @Override
+ @SuppressWarnings("unchecked")
+ protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) {
+ CryptorResponseRuleHandle ruleHandle = CryptorResponsePluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule));
+ if (Objects.isNull(ruleHandle)) {
+ LOG.error("Cryptor response rule configuration is null :{}", rule.getId());
+ return chain.execute(exchange);
+ }
+ if (JsonUtil.checkParam(ruleHandle.toJson())) {
+ Object error = ShenyuResultWrap.error(ShenyuResultEnum.CRYPTOR_RESPONSE_ERROR_CONFIGURATION.getCode(),
+ ShenyuResultEnum.CRYPTOR_RESPONSE_ERROR_CONFIGURATION.getMsg() + "[" + JsonUtil.getErrorCollector() + "]", null);
+ return WebFluxResultUtils.result(exchange, error);
+ }
+
+ CachedBodyOutputMessage outputMessage = HttpUtil.newCachedBodyOutputMessage(exchange);
+ ClientResponse clientResponse = exchange.getAttribute(Constants.CLIENT_RESPONSE_ATTR);
+ if (clientResponse == null) {
+ return Mono.empty();
+ }
+ Mono<String> mono = clientResponse.bodyToMono(String.class)
+ .flatMap(originalBody ->
+ strategyMatch(originalBody, ruleHandle));
+ BodyInserter<Mono<String>, ReactiveHttpOutputMessage> bodyInserter = BodyInserters.fromPublisher(mono, String.class);
+ return bodyInserter.insert(outputMessage, new BodyInserterContext())
+ .then(Mono.defer(() -> {
+ ServerHttpResponseDecorator decorator = new ResponseDecorator(exchange, outputMessage);
+ return chain.execute(exchange.mutate().response(decorator).build());
+ })).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> HttpUtil.release(outputMessage, throwable));
+
+ }
+
+ @Override
+ public int getOrder() {
+ return PluginEnum.CRYPTOR_RESPONSE.getCode();
+ }
+
+ @Override
+ public String named() {
+ return PluginEnum.CRYPTOR_RESPONSE.getName();
+ }
+
+ private Mono strategyMatch(final String originalBody, final CryptorResponseRuleHandle ruleHandle) {
+ AtomicInteger initDeep = new AtomicInteger();
+ initDeep.set(0);
+ CryptorStrategy strategy = ExtensionLoader.getExtensionLoader(CryptorStrategy.class).getJoin(ruleHandle.getStrategyName());
+ String parseBody = JsonUtil.parser(originalBody, ruleHandle.getFieldNames());
+ if (parseBody == null) {
+ return Mono.just(originalBody);
+ }
+ String modifiedBody = executor.encryptExecute(strategy, ruleHandle.getKey(), parseBody);
+ JsonElement je = new JsonParser().parse(originalBody);
+ JsonElement resultJe = JsonUtil.replaceJsonNode(je,
+ initDeep,
+ modifiedBody,
+ Arrays.asList(ruleHandle.getFieldNames().split("\\.")));
+ return Mono.just(resultJe.toString());
+ }
+
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/main/resources/META-INF/shenyu/org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy b/shenyu-plugin/shenyu-plugin-cryptor/src/main/resources/META-INF/shenyu/org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy
new file mode 100644
index 0000000..aa25e26
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/main/resources/META-INF/shenyu/org.apache.shenyu.plugin.cryptor.common.strategies.CryptorStrategy
@@ -0,0 +1,17 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+rsa=org.apache.shenyu.plugin.cryptor.common.strategies.RsaStrategy
\ No newline at end of file
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPluginTest.java b/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPluginTest.java
new file mode 100644
index 0000000..fc7837c
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/request/CryptorRequestPluginTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.request;
+
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.cryptor.common.handler.CryptorRequestPluginDataHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.MediaType;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test case for {@link CryptorRequestPlugin}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class CryptorRequestPluginTest {
+
+ private RuleData ruleData;
+
+ @Mock
+ private ShenyuPluginChain chain;
+
+ private ServerWebExchange exchange;
+
+ private CryptorRequestPlugin cryptorRequestPlugin;
+
+ private CryptorRequestPluginDataHandler cryptorRequestPluginDataHandler;
+
+ @Before
+ public void setUp() {
+ this.ruleData = new RuleData();
+ this.ruleData.setSelectorId("test");
+ this.ruleData.setName("test-cryptor-request-plugin");
+ this.ruleData.setHandle("{\"strategyName\":\"rsa\","
+ + "\"fieldNames\":\"inputToken\","
+ + "\"key\":\"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAtrfolGUtLhZVSpd5L/oAXbGW9Rn54mD96Ny"
+ + "uWsxp/KCscDoeFScN7uSc3LwKk14wrC4X0+fSDxm0kMPTvgNBywIDAQABAkBFPvt4ycNOlQ4r364A3akn2PbR2s9V2NZBW"
+ + "ukE5jVAlOvgCn6L/+tsVDSQgeVtOPd6rwM2a24iASDsNEbnVrwBAiEA34DwAmsa1phE5aGKM1bPHJiGgM8yolIYDWBaBCu"
+ + "PTgECIQDRSOWA8rLJWP+Vijm/QB8C41Gw1V7WXC2Kuj07Jv5nywIgTDKCIODw8m5RNtRe8GfNDlu1p158TbidOJo7tiY/og"
+ + "ECIQCaj0tvP83qBWA8AClFpQVCDL936RxxEwJPQduWo+WeoQIhAN7HKEW0E97il2RvCsgeArdt83WjZh7OhMhW6MLPrMjs\""
+ + "}\n");
+ this.cryptorRequestPluginDataHandler = new CryptorRequestPluginDataHandler();
+ this.cryptorRequestPlugin = new CryptorRequestPlugin();
+ this.exchange = MockServerWebExchange.from(MockServerHttpRequest
+ .method(HttpMethod.POST, "/test")
+ .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
+ .body("{\"inputToken\": "
+ + "\"kYPZgOAR2pEipskl5WURW/r3CMxNQJwbs4jbTAOfZNV39L4WkaTOqAeolV+rlKCKiXKvhfHWaxQOTMm9hQBxLA==\""
+ + "}"));
+ }
+
+ @Test
+ public void doExecuteTest() {
+ SelectorData selectorData = mock(SelectorData.class);
+ when(this.chain.execute(any())).thenReturn(Mono.empty());
+ cryptorRequestPluginDataHandler.handlerRule(ruleData);
+ StepVerifier.create(cryptorRequestPlugin.doExecute(this.exchange, this.chain, selectorData, this.ruleData)).expectSubscription().verifyComplete();
+ }
+
+ @Test
+ public void namedTest() {
+ final String result = cryptorRequestPlugin.named();
+ assertEquals(PluginEnum.CRYPTOR_REQUEST.getName(), result);
+ }
+
+ @Test
+ public void getOrderTest() {
+ final int result = cryptorRequestPlugin.getOrder();
+ assertEquals(PluginEnum.CRYPTOR_REQUEST.getCode(), result);
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePluginTest.java b/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePluginTest.java
new file mode 100644
index 0000000..6b3d94d
--- /dev/null
+++ b/shenyu-plugin/shenyu-plugin-cryptor/src/test/java/org/apache/shenyu/plugin/cryptor/response/CryptorResponsePluginTest.java
@@ -0,0 +1,122 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.plugin.cryptor.response;
+
+import org.apache.shenyu.common.constant.Constants;
+import org.apache.shenyu.common.dto.RuleData;
+import org.apache.shenyu.common.dto.SelectorData;
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.api.ShenyuPluginChain;
+import org.apache.shenyu.plugin.cryptor.common.decorator.ResponseDecorator;
+import org.apache.shenyu.plugin.cryptor.common.handler.CryptorResponsePluginDataHandler;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.MediaType;
+import org.springframework.http.codec.ServerCodecConfigurer;
+import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
+import org.springframework.mock.web.server.MockServerWebExchange;
+import org.springframework.web.reactive.function.client.ClientResponse;
+import org.springframework.web.server.ServerWebExchange;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+import reactor.test.StepVerifier;
+
+import java.net.InetSocketAddress;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Test case for {@link CryptorResponsePlugin}.
+ */
+@RunWith(MockitoJUnitRunner.class)
+public class CryptorResponsePluginTest {
+
+ private RuleData ruleData;
+
+ private ShenyuPluginChain chain;
+
+ private ServerWebExchange exchange;
+
+ private CryptorResponsePlugin cryptorResponsePlugin;
+
+ private CryptorResponsePluginDataHandler cryptorResponsePluginDataHandler;
+
+ private SelectorData selectorData;
+
+ @Before
+ public void setUp() {
+ this.ruleData = new RuleData();
+ this.ruleData.setSelectorId("test");
+ this.ruleData.setName("test-cryptor-response-plugin");
+ this.ruleData.setHandle("{\"strategyName\":\"rsa\","
+ + "\"key\":\"MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBALa36JRlLS4WVUqXeS/6AF2xlvUZ+eJg/ejcrlrMafygrHA6HhUnDe"
+ + "7knNy8CpNeMKwuF9Pn0g8ZtJDD074DQcsCAwEAAQ\\u003d\\u003d\","
+ + "\"fieldNames\":\"data\"}");
+ this.cryptorResponsePluginDataHandler = new CryptorResponsePluginDataHandler();
+ this.chain = mock(ShenyuPluginChain.class);
+ this.selectorData = mock(SelectorData.class);
+ this.cryptorResponsePlugin = new CryptorResponsePlugin();
+ MockServerHttpRequest request = MockServerHttpRequest
+ .post("/test")
+ .remoteAddress(new InetSocketAddress(8090))
+ .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
+ .body("{\"code\":200,\"msg\":\"success\",\"data\":[\"test\"]}");
+ this.exchange = spy(MockServerWebExchange.from(request));
+ }
+
+ @Test
+ public void doExecuteTest() {
+ ServerWebExchange.Builder builder = mock(ServerWebExchange.Builder.class);
+ when(exchange.mutate()).thenReturn(builder);
+ when(builder.response(any(ResponseDecorator.class))).thenReturn(builder);
+ when(builder.build()).thenReturn(exchange);
+ when(chain.execute(any())).thenReturn(Mono.empty());
+ cryptorResponsePluginDataHandler.handlerRule(ruleData);
+ ServerWebExchange exchangeNormal = generateServerWebExchange();
+ Mono<Void> result = cryptorResponsePlugin.doExecute(exchangeNormal, chain, selectorData, ruleData);
+ StepVerifier.create(result).expectSubscription().verifyComplete();
+ }
+
+ @Test
+ public void namedTest() {
+ final String result = cryptorResponsePlugin.named();
+ assertEquals(PluginEnum.CRYPTOR_RESPONSE.getName(), result);
+ }
+
+ @Test
+ public void getOrderTest() {
+ final int result = cryptorResponsePlugin.getOrder();
+ assertEquals(PluginEnum.CRYPTOR_RESPONSE.getCode(), result);
+ }
+
+ private ServerWebExchange generateServerWebExchange() {
+ ClientResponse.Builder builder;
+ builder = ClientResponse.create(HttpStatus.OK, ServerCodecConfigurer.create().getReaders());
+ ClientResponse clientResponse = builder.body(Flux.from(exchange.getRequest().getBody())).build();
+ exchange.getAttributes().put(Constants.CLIENT_RESPONSE_ATTR, clientResponse);
+ return this.exchange;
+ }
+}
diff --git a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java b/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
index 58d08fb..821bd9e 100644
--- a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
+++ b/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
@@ -48,5 +48,4 @@ public interface ThrowingFunction<T> {
return null;
}
}
-
}
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/pom.xml b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/pom.xml
index 9b30539..f2953a9 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/pom.xml
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/pom.xml
@@ -55,5 +55,6 @@
<module>shenyu-spring-boot-starter-plugin-jwt</module>
<module>shenyu-spring-boot-starter-plugin-response</module>
<module>shenyu-spring-boot-starter-plugin-param-mapping</module>
+ <module>shenyu-spring-boot-starter-plugin-cryptor</module>
</modules>
</project>
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/pom.xml b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/pom.xml
new file mode 100644
index 0000000..9a8e6f7
--- /dev/null
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/pom.xml
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to You under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <artifactId>shenyu-spring-boot-starter-plugin</artifactId>
+ <groupId>org.apache.shenyu</groupId>
+ <version>2.4.1-SNAPSHOT</version>
+ </parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>shenyu-spring-boot-starter-plugin-cryptor</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shenyu</groupId>
+ <artifactId>shenyu-plugin-cryptor</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.assertj</groupId>
+ <artifactId>assertj-core</artifactId>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
diff --git a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfiguration.java
similarity index 52%
copy from shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
copy to shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfiguration.java
index 58d08fb..c6f4a6d 100644
--- a/shenyu-plugin/shenyu-plugin-jwt/src/main/java/org/apache/shenyu/plugin/jwt/exception/ThrowingFunction.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfiguration.java
@@ -15,38 +15,38 @@
* limitations under the License.
*/
-package org.apache.shenyu.plugin.jwt.exception;
+package org.apache.shenyu.springboot.starter.plugin.cryptor;
+
+import org.apache.shenyu.plugin.api.ShenyuPlugin;
+import org.apache.shenyu.plugin.cryptor.request.CryptorRequestPlugin;
+import org.apache.shenyu.plugin.cryptor.response.CryptorResponsePlugin;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
/**
- * Throwing wrap.
- * Handle exception, and return null.
- * Not all exceptions must be try catch, throw upwards, return null, you can use if to judge.
+ * Cryptor plugin.
*/
-@FunctionalInterface
-public interface ThrowingFunction<T> {
+@Configuration
+public class CryptorPluginConfiguration {
/**
- * apply function.
+ * Cryptor request plugin.
*
- * @return T
- * @throws Exception any error
+ * @return the soul plugin
*/
- T apply() throws Exception;
+ @Bean
+ public ShenyuPlugin cryptorRequestPlugin() {
+ return new CryptorRequestPlugin();
+ }
/**
- * operation function.
- * If an exception occurs, it will directly return a null.
+ * Cryptor response plugin.
*
- * @param function apply function.
- * @param <T> t
- * @return T
+ * @return the soul plugin
*/
- static <T> T wrap(ThrowingFunction<T> function) {
- try {
- return function.apply();
- } catch (Exception e) {
- return null;
- }
+ @Bean
+ public ShenyuPlugin cryptorResponsePlugin() {
+ return new CryptorResponsePlugin();
}
}
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/resources/META-INF/spring.factories b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/resources/META-INF/spring.factories
new file mode 100644
index 0000000..0d8d4e0
--- /dev/null
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,19 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+org.apache.shenyu.springboot.starter.plugin.cryptor.CryptorPluginConfiguration
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/test/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfigurationTest.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/test/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfigurationTest.java
new file mode 100644
index 0000000..69ccf96
--- /dev/null
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-plugin/shenyu-spring-boot-starter-plugin-cryptor/src/test/java/org/apache/shenyu/springboot/starter/plugin/cryptor/CryptorPluginConfigurationTest.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.springboot.starter.plugin.cryptor;
+
+import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.plugin.cryptor.request.CryptorRequestPlugin;
+import org.apache.shenyu.plugin.cryptor.response.CryptorResponsePlugin;
+import org.junit.Test;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * Test case for {@link CryptorPluginConfiguration}.
+ */
+public class CryptorPluginConfigurationTest {
+
+ @Test
+ public void testCryptorRequestPlugin() {
+ new ApplicationContextRunner()
+ .withConfiguration(AutoConfigurations.of(CryptorPluginConfiguration.class))
+ .run(context -> {
+ CryptorRequestPlugin plugin = context.getBean("cryptorRequestPlugin", CryptorRequestPlugin.class);
+ assertThat(plugin.named()).isEqualTo(PluginEnum.CRYPTOR_REQUEST.getName());
+ });
+ }
+
+ @Test
+ public void testCryptorResponsePlugin() {
+ new ApplicationContextRunner()
+ .withConfiguration(AutoConfigurations.of(CryptorPluginConfiguration.class))
+ .run(context -> {
+ CryptorResponsePlugin plugin = context.getBean("cryptorResponsePlugin", CryptorResponsePlugin.class);
+ assertThat(plugin.named()).isEqualTo(PluginEnum.CRYPTOR_RESPONSE.getName());
+ });
+ }
+}