diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/passwordEncoder/SM3PasswordEncoder.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/passwordEncoder/SM3PasswordEncoder.java new file mode 100644 index 00000000..c2bfc5bd --- /dev/null +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/passwordEncoder/SM3PasswordEncoder.java @@ -0,0 +1,36 @@ +package com.central.common.passwordEncoder; + +import cn.hutool.crypto.SmUtil; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.crypto.password.PasswordEncoder; + +/** + * SM3 + * + * @author zlt + * @version 1.0 + * @date 2022/12/29 + *

+ * Blog: https://zlt2000.gitee.io + * Github: https://github.com/zlt2000 + */ +@Slf4j +public class SM3PasswordEncoder implements PasswordEncoder { + @Override + public String encode(CharSequence rawPassword) { + return SmUtil.sm3(rawPassword.toString()); + } + + @Override + public boolean matches(CharSequence rawPassword, String encodedPassword) { + if (rawPassword == null) { + throw new IllegalArgumentException("rawPassword cannot be null"); + } + if (encodedPassword == null || encodedPassword.length() == 0) { + log.warn("Empty encoded password"); + return false; + } + String rawPasswordEncoded = this.encode(rawPassword.toString()); + return rawPasswordEncoded.equals(encodedPassword); + } +} diff --git a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/PwdEncoderUtil.java b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/PwdEncoderUtil.java index dfe0b18c..343a8070 100644 --- a/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/PwdEncoderUtil.java +++ b/zlt-commons/zlt-common-core/src/main/java/com/central/common/utils/PwdEncoderUtil.java @@ -1,5 +1,6 @@ package com.central.common.utils; +import com.central.common.passwordEncoder.SM3PasswordEncoder; import org.springframework.security.crypto.argon2.Argon2PasswordEncoder; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.crypto.password.DelegatingPasswordEncoder; @@ -35,6 +36,7 @@ public class PwdEncoderUtil { encoders.put("SHA-256", new org.springframework.security.crypto.password.MessageDigestPasswordEncoder("SHA-256")); encoders.put("sha256", new org.springframework.security.crypto.password.StandardPasswordEncoder()); encoders.put("argon2", new Argon2PasswordEncoder()); + encoders.put("SM3", new SM3PasswordEncoder()); Assert.isTrue(encoders.containsKey(encodingId), encodingId + " is not found in idToPasswordEncoder"); diff --git a/zlt-doc/sql/oauth-center.sql b/zlt-doc/sql/oauth-center.sql index 6405352c..bdf168fc 100644 --- a/zlt-doc/sql/oauth-center.sql +++ b/zlt-doc/sql/oauth-center.sql @@ -30,6 +30,6 @@ CREATE TABLE `oauth_client_details` ( -- ---------------------------- -- Records of oauth_client_details -- ---------------------------- -INSERT INTO `oauth_client_details` VALUES (1, 'webApp', NULL, '$2a$10$06msMGYRH8nrm4iVnKFNKOoddB8wOwymVhbUzw/d3ZixD7Nq8ot72', 'webApp', 'app', 'authorization_code,password,refresh_token,client_credentials,implicit,password_code,openId,mobile_password', NULL, NULL, 3600, NULL, '{"LOGOUT_NOTIFY_URL_LIST":"http://127.0.0.1:8082/logoutNotify"}', 'true', NULL, NULL, 'pc端', 1, 60); +INSERT INTO `oauth_client_details` VALUES (1, 'webApp', NULL, '$2a$10$06msMGYRH8nrm4iVnKFNKOoddB8wOwymVhbUzw/d3ZixD7Nq8ot72', 'webApp', 'app', 'authorization_code,password,refresh_token,client_credentials,implicit,password_code,openId,mobile_password', 'http://127.0.0.1:8082/callback.html', NULL, 3600, NULL, '{"LOGOUT_NOTIFY_URL_LIST":"http://127.0.0.1:8082/logoutNotify"}', 'true', NULL, NULL, 'pc端', 1, 60); INSERT INTO `oauth_client_details` VALUES (2, 'app', NULL, '$2a$10$i3F515wEDiB4Gvj9ym9Prui0dasRttEUQ9ink4Wpgb4zEDCAlV8zO', 'app', 'app', 'authorization_code,password,refresh_token', 'http://127.0.0.1:8081/callback.html', NULL, 3600, NULL, '{"LOGOUT_NOTIFY_URL_LIST":"http://127.0.0.1:8081/logoutNotify"}', 'true', NULL, NULL, '移动端', 1, 60); INSERT INTO `oauth_client_details` VALUES (3, 'zlt', NULL, '$2a$10$/o.wuORzVcXaezmYVzwYMuoY7qeWXBALwQmkskXD/7C6rqfCyPrna', 'zlt', 'all', 'authorization_code,password,refresh_token,client_credentials', 'http://127.0.0.1:8080/singleLogin', NULL, 3600, 28800, '{}', 'true', '2018-12-27 00:50:30', '2018-12-27 00:50:30', '第三方应用', 1, 60); \ No newline at end of file