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 13ae268 [ISSUE #1888] Optimize the password encryption (#2928)
13ae268 is described below
commit 13ae268dad7790f874f6145387d3919a97a5d811
Author: erdengk <[email protected]>
AuthorDate: Wed Feb 23 23:14:30 2022 +0800
[ISSUE #1888] Optimize the password encryption (#2928)
* fix issue #1888
* fix issue #1888
* fix issue #1888
* fix issue #1888
---
.../shenyu/admin/config/SecretConfiguration.java | 45 ---------
.../admin/controller/DashboardUserController.java | 10 +-
.../service/impl/DashboardUserServiceImpl.java | 10 +-
.../org/apache/shenyu/admin/utils/AesUtils.java | 56 -----------
.../org/apache/shenyu/admin/utils/CipherUtils.java | 105 ---------------------
.../org/apache/shenyu/admin/utils/ShaUtils.java | 58 ++++++++++++
shenyu-admin/src/main/resources/application.yml | 4 -
.../src/main/resources/sql-script/h2/schema.sql | 2 +-
.../src/main/resources/sql-script/mysql/schema.sql | 2 +-
.../src/main/resources/sql-script/pg/schema.sql | 2 +-
.../admin/config/SecretConfigurationTest.java | 56 -----------
.../admin/mapper/DashboardUserMapperTest.java | 6 +-
.../admin/service/DashboardUserServiceTest.java | 9 +-
.../apache/shenyu/admin/utils/CipherUtilsTest.java | 61 ------------
.../utils/{AesUtilsTest.java => ShaUtilsTest.java} | 30 ++----
15 files changed, 81 insertions(+), 375 deletions(-)
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/SecretConfiguration.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/SecretConfiguration.java
deleted file mode 100644
index b1a5af5..0000000
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/config/SecretConfiguration.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.admin.config;
-
-import org.apache.shenyu.admin.config.properties.SecretProperties;
-import org.springframework.beans.factory.annotation.Value;
-import
org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-/**
- * Aes Secret configuration.
- */
-@Configuration
-public class SecretConfiguration {
-
- /**
- * Register secretProperties for CipherUtils in spring ioc.
- *
- * @param key the key read from property file, default value is
2095132720951327
- * @return secretProperties
- */
- @Bean
- @ConditionalOnMissingBean(value = SecretProperties.class)
- public SecretProperties
secretProperties(@Value("${shenyu.aes.secret.key:2095132720951327}") final
String key) {
- SecretProperties secretProperties = new SecretProperties();
- secretProperties.setKey(key);
- return secretProperties;
- }
-}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
index 92a2f38..324da4b 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/controller/DashboardUserController.java
@@ -28,7 +28,7 @@ import org.apache.shenyu.admin.model.result.ShenyuAdminResult;
import org.apache.shenyu.admin.model.vo.DashboardUserEditVO;
import org.apache.shenyu.admin.model.vo.DashboardUserVO;
import org.apache.shenyu.admin.service.DashboardUserService;
-import org.apache.shenyu.admin.utils.AesUtils;
+import org.apache.shenyu.admin.utils.ShaUtils;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
@@ -111,10 +111,8 @@ public class DashboardUserController {
@RequiresPermissions("system:manager:add")
@PostMapping("")
public ShenyuAdminResult createDashboardUser(@Valid @RequestBody final
DashboardUserDTO dashboardUserDTO) {
- String key = secretProperties.getKey();
- String iv = secretProperties.getIv();
return Optional.ofNullable(dashboardUserDTO).map(item -> {
- item.setPassword(AesUtils.aesEncryption(item.getPassword(), key,
iv));
+ item.setPassword(ShaUtils.shaEncryption(item.getPassword()));
Integer createCount = dashboardUserService.createOrUpdate(item);
return
ShenyuAdminResult.success(ShenyuResultMessage.CREATE_SUCCESS, createCount);
}).orElseGet(() ->
ShenyuAdminResult.error(ShenyuResultMessage.DASHBOARD_CREATE_USER_ERROR));
@@ -132,9 +130,7 @@ public class DashboardUserController {
public ShenyuAdminResult updateDashboardUser(@PathVariable("id") final
String id, @Valid @RequestBody final DashboardUserDTO dashboardUserDTO) {
dashboardUserDTO.setId(id);
if (StringUtils.isNotBlank(dashboardUserDTO.getPassword())) {
- String key = secretProperties.getKey();
- String iv = secretProperties.getIv();
-
dashboardUserDTO.setPassword(AesUtils.aesEncryption(dashboardUserDTO.getPassword(),
key, iv));
+
dashboardUserDTO.setPassword(ShaUtils.shaEncryption(dashboardUserDTO.getPassword()));
}
Integer updateCount =
dashboardUserService.createOrUpdate(dashboardUserDTO);
return ShenyuAdminResult.success(ShenyuResultMessage.UPDATE_SUCCESS,
updateCount);
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
index 285eb51..5e3c071 100644
---
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
+++
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/DashboardUserServiceImpl.java
@@ -41,8 +41,8 @@ import org.apache.shenyu.admin.model.vo.LoginDashboardUserVO;
import org.apache.shenyu.admin.model.vo.RoleVO;
import org.apache.shenyu.admin.service.DashboardUserService;
import org.apache.shenyu.admin.transfer.DashboardUserTransfer;
-import org.apache.shenyu.admin.utils.AesUtils;
import org.apache.shenyu.admin.utils.JwtUtils;
+import org.apache.shenyu.admin.utils.ShaUtils;
import org.apache.shenyu.common.constant.AdminConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -251,8 +251,6 @@ public class DashboardUserServiceImpl implements
DashboardUserService {
}
private DashboardUserVO loginByLdap(final String userName, final String
password) {
- String key = secretProperties.getKey();
- String iv = secretProperties.getIv();
String searchBase = String.format("%s=%s,%s",
ldapProperties.getLoginField(), LdapEncoder.nameEncode(userName),
ldapProperties.getBaseDn());
String filter = String.format("(objectClass=%s)",
ldapProperties.getObjectClass());
try {
@@ -263,7 +261,7 @@ public class DashboardUserServiceImpl implements
DashboardUserService {
RoleDO role = roleMapper.findByRoleName("default");
DashboardUserDTO dashboardUserDTO =
DashboardUserDTO.builder()
.userName(userName)
- .password(AesUtils.aesEncryption(password, key,
iv))
+ .password(ShaUtils.shaEncryption(password))
.role(1)
.roles(Lists.newArrayList(role.getId()))
.enabled(true)
@@ -282,9 +280,7 @@ public class DashboardUserServiceImpl implements
DashboardUserService {
}
private DashboardUserVO loginByDatabase(final String userName, final
String password) {
- String key = secretProperties.getKey();
- String iv = secretProperties.getIv();
- return findByQuery(userName, AesUtils.aesEncryption(password, key,
iv));
+ return findByQuery(userName, ShaUtils.shaEncryption(password));
}
/**
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/AesUtils.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/AesUtils.java
deleted file mode 100644
index 9f5fc89..0000000
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/AesUtils.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.admin.utils;
-
-import org.apache.commons.lang3.StringUtils;
-
-/**
- * The type Aes utils.
- */
-public class AesUtils {
-
- /**
- * Aes Encryption string.
- *
- * @param src the src
- * @param aesKey key
- * @param iv iv
- * @return the string
- */
- public static String aesEncryption(final String src, final String aesKey,
final String iv) {
- if (StringUtils.isNotEmpty(src)) {
- return CipherUtils.encryptHex(src, aesKey, iv);
- }
- return null;
- }
-
- /**
- * Aes Decryption string.
- *
- * @param src the src
- * @param aesKey key
- * @param iv iv
- * @return the string
- */
- public static String aesDecryption(final String src, final String aesKey,
final String iv) {
- if (StringUtils.isNotEmpty(src)) {
- return CipherUtils.decryptStr(src, aesKey, iv);
- }
- return null;
- }
-}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CipherUtils.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CipherUtils.java
deleted file mode 100644
index 5b06c89..0000000
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/CipherUtils.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * 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.admin.utils;
-
-import java.nio.charset.StandardCharsets;
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Base64;
-import java.util.Optional;
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-import org.apache.shenyu.common.exception.ShenyuException;
-
-/**
- * Cipher Tools.
- */
-public class CipherUtils {
-
- public static final String AES_CBC_PKCS_5_PADDING = "AES/CBC/PKCS5Padding";
-
- /**
- * cipherTool.
- *
- * @param content source
- * @param mode encryption/decryption
- * @param aesKey key
- * @return the bytes
- */
- private static byte[] cipherTool(final byte[] content, final int mode,
final String aesKey,
- final String iv) {
- byte[] plainText;
- try {
- SecretKeySpec keySpec = new
SecretKeySpec(aesKey.getBytes(StandardCharsets.UTF_8),
- "AES");
- final Cipher cipher = Cipher.getInstance(AES_CBC_PKCS_5_PADDING);
- IvParameterSpec ivSpec = new
IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
- cipher.init(mode, keySpec, ivSpec);
- plainText = content;
- return cipher.doFinal(plainText);
- } catch (NoSuchAlgorithmException | InvalidKeyException |
NoSuchPaddingException | BadPaddingException
- | IllegalBlockSizeException | InvalidAlgorithmParameterException
e) {
- throw new ShenyuException(e);
- }
- }
-
- /**
- * Aes encryption.
- *
- * @param src source
- * @param aesKey key
- * @param iv iv
- * @return the string
- */
- public static String encryptHex(final String src, final String aesKey,
final String iv) {
- return Optional.ofNullable(src).map(item -> {
- try {
- byte[] byteContent = item.getBytes(StandardCharsets.UTF_8);
- byte[] result = cipherTool(byteContent, Cipher.ENCRYPT_MODE,
aesKey, iv);
- return Base64.getEncoder().encodeToString(result);
- } catch (Exception ex) {
- throw new ShenyuException(ex);
- }
- }).orElse(null);
- }
-
- /**
- * decryptStr.
- *
- * @param src source
- * @param aesKey key
- * @param iv iv
- * @return the string
- */
- public static String decryptStr(final String src, final String aesKey,
final String iv) {
- return Optional.ofNullable(src).map(item -> {
- try {
- byte[] byteContent = Base64.getDecoder().decode(item);
- byte[] result = cipherTool(byteContent, Cipher.DECRYPT_MODE,
aesKey, iv);
- return new String(result);
- } catch (Exception ex) {
- throw new ShenyuException(ex);
- }
- }).orElse(null);
- }
-}
diff --git
a/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/ShaUtils.java
b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/ShaUtils.java
new file mode 100644
index 0000000..f460da2
--- /dev/null
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/utils/ShaUtils.java
@@ -0,0 +1,58 @@
+/*
+ * 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.admin.utils;
+
+import java.security.MessageDigest;
+
+import java.util.Optional;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.common.exception.ShenyuException;
+
+/**
+ * The type SHA utils.
+ */
+public class ShaUtils {
+
+ /**
+ * sh512 Encryption string.
+ *
+ * @param src the src
+ * @return the string
+ */
+ public static String shaEncryption(final String src) {
+ return Optional.ofNullable(src).map(item -> {
+ if (StringUtils.isEmpty(src)) {
+ return null;
+ }
+ try {
+ MessageDigest messageDigest =
MessageDigest.getInstance("SHA-512");
+ messageDigest.update(item.getBytes());
+ byte[] byteBuffer = messageDigest.digest();
+ StringBuffer strHexString = new StringBuffer();
+ for (byte b:byteBuffer) {
+ String hex = Integer.toHexString(0xff & b);
+ strHexString.append(hex);
+ }
+ return strHexString.toString();
+ } catch (Exception e) {
+ throw new ShenyuException(e);
+ }
+ }).orElse(null);
+ }
+}
diff --git a/shenyu-admin/src/main/resources/application.yml
b/shenyu-admin/src/main/resources/application.yml
index de608d6..5db569a 100644
--- a/shenyu-admin/src/main/resources/application.yml
+++ b/shenyu-admin/src/main/resources/application.yml
@@ -67,10 +67,6 @@ shenyu:
# url: http://localhost:2379
# consul:
# url: http://localhost:8500
- aes:
- secret:
- key: 2095132720951327
- iv: 6075877187097700
ldap:
enabled: false
url: ldap://xxxx:xxx
diff --git a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
index eb6e421..2d75f2e 100644
--- a/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/h2/schema.sql
@@ -253,7 +253,7 @@ CREATE TABLE IF NOT EXISTS `data_permission` (
);;
/**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');;
+INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`,
`enabled`, `date_created`, `date_updated`) VALUES
('1','admin','ba3253876aed6bc22d4a6ff53d846c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413',
'1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');;
/** insert admin role */
INSERT IGNORE INTO `user_role` (`id`, `user_id`, `role_id`, `date_created`,
`date_updated`) VALUES ('1351007709096976384', '1', '1346358560427216896',
'2021-01-18 11:25:13', '2021-01-18 11:25:13');;
diff --git a/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
b/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
index 647bfbe..b850fa7 100644
--- a/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/mysql/schema.sql
@@ -267,7 +267,7 @@ CREATE TABLE IF NOT EXISTS `data_permission` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
ROW_FORMAT=DYNAMIC COMMENT='data permission table';;
/**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');;
+INSERT IGNORE INTO `dashboard_user` (`id`, `user_name`, `password`, `role`,
`enabled`, `date_created`, `date_updated`) VALUES
('1','admin','ba3253876aed6bc22d4a6ff53d846c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413',
'1', '1', '2018-06-23 15:12:22', '2018-06-23 15:12:23');;
/** insert admin role */
INSERT IGNORE INTO `user_role` (`id`, `user_id`, `role_id`, `date_created`,
`date_updated`) VALUES ('1351007709096976384', '1', '1346358560427216896',
'2021-01-18 11:25:13', '2021-01-18 11:25:13');;
diff --git a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
index 9aa15cc..0e941fc 100644
--- a/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
+++ b/shenyu-admin/src/main/resources/sql-script/pg/schema.sql
@@ -201,7 +201,7 @@ ELSE
PERFORM public.dblink_exec('init_conn', ' COMMENT ON COLUMN
"dashboard_user"."enabled" IS ''' || 'delete or not' || '''');
PERFORM public.dblink_exec('init_conn', ' COMMENT ON COLUMN
"dashboard_user"."date_created" IS ''' || 'create time' || '''');
PERFORM public.dblink_exec('init_conn', ' COMMENT ON COLUMN
"dashboard_user"."date_updated" IS ''' || 'update time' || '''');
- PERFORM public.dblink_exec('init_conn', 'INSERT INTO "dashboard_user"
VALUES (''' || '1' || ''', ''' || 'admin' || ''', ''' ||
'bbiB8zbUo3z3oA0VqEB/IA==' || ''', 1, 1, ''' || '2018-06-23 15:12:22' || ''',
''' || '2018-06-23 15:12:23' || ''');');
+ PERFORM public.dblink_exec('init_conn', 'INSERT INTO "dashboard_user"
VALUES (''' || '1' || ''', ''' || 'admin' || ''', ''' ||
'ba3253876aed6bc22d4a6ff53d846c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413'
|| ''', 1, 1, ''' || '2018-06-23 15:12:22' || ''', ''' || '2018-06-23
15:12:23' || ''');');
-- ----------------------------
-- Indexes structure for table dashboard_user
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/SecretConfigurationTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/SecretConfigurationTest.java
deleted file mode 100644
index ec290bc..0000000
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/config/SecretConfigurationTest.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.admin.config;
-
-import org.apache.shenyu.admin.AbstractConfigurationTest;
-import org.apache.shenyu.admin.config.properties.SecretProperties;
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.Assertions;
-
-/**
- * Test SecretConfiguration.
- */
-public class SecretConfigurationTest extends AbstractConfigurationTest {
-
- @Test
- public void testNormalSecretConfiguration() {
- load(SecretConfiguration.class,
"shenyu.aes.secret.key=1234567890123456");
- SecretProperties secretProperties =
getContext().getBean(SecretProperties.class);
- Assertions.assertNotNull(secretProperties);
- String key = secretProperties.getKey();
- Assertions.assertEquals(key, "1234567890123456");
- }
-
- @Test
- public void testDefaultSecretConfiguration() {
- load(SecretConfiguration.class);
- SecretProperties secretProperties =
getContext().getBean(SecretProperties.class);
- Assertions.assertNotNull(secretProperties);
- String key = secretProperties.getKey();
- Assertions.assertEquals(key, "2095132720951327");
- }
-
- @Test
- public void testAbnormalSecretConfiguration() {
- load(SecretConfiguration.class, "shenyu.aes.secret.key=");
- SecretProperties secretProperties =
getContext().getBean(SecretProperties.class);
- Assertions.assertNotNull(secretProperties);
- String key = secretProperties.getKey();
- Assertions.assertEquals("", key);
- }
-}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/DashboardUserMapperTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/DashboardUserMapperTest.java
index 2a8852b..ed2f6fc 100644
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/DashboardUserMapperTest.java
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/mapper/DashboardUserMapperTest.java
@@ -21,7 +21,7 @@ import org.apache.shenyu.admin.AbstractSpringIntegrationTest;
import org.apache.shenyu.admin.model.entity.DashboardUserDO;
import org.apache.shenyu.admin.model.page.PageParameter;
import org.apache.shenyu.admin.model.query.DashboardUserQuery;
-import org.apache.shenyu.admin.utils.AesUtils;
+import org.apache.shenyu.admin.utils.ShaUtils;
import org.apache.shenyu.common.utils.UUIDUtils;
import org.junit.jupiter.api.Test;
@@ -160,14 +160,12 @@ public final class DashboardUserMapperTest extends
AbstractSpringIntegrationTest
}
private DashboardUserDO buildDashboardUserDO() {
- String aseKey = "2095132720951327";
- String iv = "6075877187097700";
Timestamp now = new Timestamp(System.currentTimeMillis());
return DashboardUserDO.builder()
.id(UUIDUtils.getInstance().generateShortUuid())
.userName("adminTest")
- .password(AesUtils.aesEncryption("123456", aseKey, iv))
+ .password(ShaUtils.shaEncryption("123456"))
.enabled(true)
.role(1)
.dateCreated(now)
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java
index 740502f..a11da37 100644
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/service/DashboardUserServiceTest.java
@@ -34,7 +34,7 @@ import org.apache.shenyu.admin.model.query.DashboardUserQuery;
import org.apache.shenyu.admin.model.vo.DashboardUserVO;
import org.apache.shenyu.admin.model.vo.LoginDashboardUserVO;
import org.apache.shenyu.admin.service.impl.DashboardUserServiceImpl;
-import org.apache.shenyu.admin.utils.AesUtils;
+import org.apache.shenyu.admin.utils.ShaUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -180,10 +180,7 @@ public final class DashboardUserServiceTest {
ReflectionTestUtils.setField(dashboardUserService, "secretProperties",
secretProperties);
ReflectionTestUtils.setField(dashboardUserService, "jwtProperties",
jwtProperties);
DashboardUserDO dashboardUserDO = createDashboardUserDO();
- String key = "2095132720951327";
- String iv = "6075877187097700";
- when(secretProperties.getKey()).thenReturn(key, key);
- when(secretProperties.getIv()).thenReturn(iv, iv);
+
when(dashboardUserMapper.findByQuery(eq(TEST_USER_NAME),
anyString())).thenReturn(dashboardUserDO);
given(ldapTemplate.authenticate(anyString(), anyString(),
anyString())).willReturn(true);
given(roleMapper.findByRoleName("default")).willReturn(RoleDO.buildRoleDO(new
RoleDTO("1", "test", null, null)));
@@ -195,7 +192,7 @@ public final class DashboardUserServiceTest {
ReflectionTestUtils.setField(dashboardUserService, "ldapTemplate",
ldapTemplate);
LoginDashboardUserVO loginDashboardUserVO =
dashboardUserService.login(TEST_USER_NAME, TEST_PASSWORD);
assertEquals(TEST_USER_NAME, loginDashboardUserVO.getUserName());
- assertEquals(AesUtils.aesEncryption(TEST_PASSWORD,
secretProperties.getKey(), secretProperties.getIv()),
loginDashboardUserVO.getPassword());
+ assertEquals(ShaUtils.shaEncryption(TEST_PASSWORD),
loginDashboardUserVO.getPassword());
// test loginByDatabase
ReflectionTestUtils.setField(dashboardUserService, "ldapTemplate",
null);
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/CipherUtilsTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/CipherUtilsTest.java
deleted file mode 100644
index 9fc05c9..0000000
---
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/CipherUtilsTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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.admin.utils;
-
-import org.apache.shenyu.common.exception.ShenyuException;
-import org.junit.jupiter.api.Test;
-
-import static org.hamcrest.Matchers.is;
-import static org.hamcrest.Matchers.notNullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
-/**
- * Test cases for CipherUtils.
- */
-public final class CipherUtilsTest {
-
- private static final String AES_KEY = "2095132720951327";
-
- private static final String IV = "6075877187097700";
-
- @Test
- public void testEncryptHex() {
- assertThat(CipherUtils.encryptHex("123456", AES_KEY, IV),
is("bbiB8zbUo3z3oA0VqEB/IA=="));
- }
-
- @Test
- public void testDecryptStr() {
- assertThat(CipherUtils.decryptStr("bbiB8zbUo3z3oA0VqEB/IA==", AES_KEY,
IV), is("123456"));
- }
-
- @Test
- public void testDecryptStrForErrorStringThrowsException() {
- assertThrows(ShenyuException.class, () -> {
- assertThat(CipherUtils.decryptStr("bbiB8zbUo3z3oA0VqEB/IA=",
AES_KEY, IV), notNullValue());
- });
- }
-
- @Test
- public void testDecryptStrForNullThrowsException() {
- assertThrows(AssertionError.class, () -> {
- assertThat(CipherUtils.decryptStr(null, AES_KEY, IV),
notNullValue());
- });
- }
-}
-
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/AesUtilsTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/ShaUtilsTest.java
similarity index 55%
rename from
shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/AesUtilsTest.java
rename to
shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/ShaUtilsTest.java
index 440ee78..ed49c65 100644
--- a/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/AesUtilsTest.java
+++ b/shenyu-admin/src/test/java/org/apache/shenyu/admin/utils/ShaUtilsTest.java
@@ -24,36 +24,24 @@ import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
- * Test cases for AesUtils.
+ * Test cases for ShaUtils.
*/
-public final class AesUtilsTest {
-
- private static final String AES_KEY = "2095132720951327";
-
- private static final String IV = "6075877187097700";
-
- @Test
- public void testAesEncryption() {
- assertThat(AesUtils.aesEncryption("123456", AES_KEY, IV),
is("bbiB8zbUo3z3oA0VqEB/IA=="));
- }
+public final class ShaUtilsTest {
@Test
- public void testAesEncryptionForNull() {
- assertThat(AesUtils.aesEncryption(null, AES_KEY, IV), nullValue());
+ public void testShaEncryption() {
+ assertThat(ShaUtils.shaEncryption("123456"),
is("ba3253876aed6bc22d4a6ff53d846c6ad864195ed144ab5c87621b6c233b548baeae6956df346ec8c17f5ea10f35ee3cbc514797ed7ddd3145464e2a0bab413"));
}
@Test
- public void testAesDecryption() {
- assertThat(AesUtils.aesDecryption("bbiB8zbUo3z3oA0VqEB/IA==", AES_KEY,
IV), is("123456"));
+ public void testShaEncryptionForNull() {
+ assertThat(ShaUtils.shaEncryption(null), nullValue());
}
@Test
- public void testAesDecryptionForEmptyString() {
- assertThat(AesUtils.aesDecryption("", AES_KEY, IV), nullValue());
+ public void testShaDecryptionForEmptyString() {
+ assertThat(ShaUtils.shaEncryption(""), nullValue());
}
- @Test
- public void testAesDecryptionForNull() {
- assertThat(AesUtils.aesDecryption(null, AES_KEY, IV), nullValue());
- }
}
+