From da51bb777c1908598fbe8683778121dc714c81aa Mon Sep 17 00:00:00 2001 From: CorvusYe Date: Thu, 23 May 2024 09:41:27 +0800 Subject: [PATCH] feat: support the use of ciphertext passwords in yml. --- CHANGELOG.md | 7 +++-- .../demo/config/Base64PasswordDecoder.java | 27 +++++++++++++++++++ .../src/main/resources/application.yml | 2 +- .../NgbatisBeanFactoryPostProcessor.java | 10 +++++++ .../contrib/ngbatis/PasswordDecoder.java | 18 +++++++++++++ .../ngbatis/config/NebulaJdbcProperties.java | 15 ++++++++++- .../contrib/ngbatis/proxy/NebulaDaoBasic.java | 2 +- 7 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 ngbatis-demo/src/main/java/ye/weicheng/ngbatis/demo/config/Base64PasswordDecoder.java create mode 100644 src/main/java/org/nebula/contrib/ngbatis/PasswordDecoder.java diff --git a/CHANGELOG.md b/CHANGELOG.md index add9ac03..ec80b957 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,9 @@ This source code is licensed under Apache 2.0 License. ## Feature +- feat: support the use of ciphertext passwords in yml. +- feat: expanding the `insertSelectiveBatch` interface in `NebulaDaoBasic`. + # 1.2.2 ## Bugfix @@ -73,8 +76,8 @@ This source code is licensed under Apache 2.0 License. - feat: support `` include query pieces. ([#212](https://github.com/nebula-contrib/ngbatis/pull/212), via [dieyi](https://github.com/1244453393)) - feat: extending `NgPath`, when 'with prop' is used in nGQL, edge attributes can be obtained from NgPath. ([#228](https://github.com/nebula-contrib/ngbatis/pull/228), via [dieyi](https://github.com/1244453393)) - feat: expanding the `insertEdgeBatch` interface in `NebulaDaoBasic`. ([#244](https://github.com/nebula-contrib/ngbatis/pull/244), via [Sunhb](https://github.com/shbone)) -- feat: expanding the `deleteByIdBatch` interface in `NebulaDaoBasic`. ([#247](https://github.com/nebula-contrib/ngbatis/pull/244), via [Sunhb](https://github.com/shbone)) -- feat: expanding the `listEndNodes` interface in `NebulaDaoBasic`. ([#247](https://github.com/nebula-contrib/ngbatis/pull/272), via [knqiufan](https://github.com/knqiufan)) +- feat: expanding the `deleteByIdBatch` interface in `NebulaDaoBasic`. ([#247](https://github.com/nebula-contrib/ngbatis/pull/247), via [Sunhb](https://github.com/shbone)) +- feat: expanding the `listEndNodes` interface in `NebulaDaoBasic`. ([#272](https://github.com/nebula-contrib/ngbatis/pull/272), via [knqiufan](https://github.com/knqiufan)) - feat: support specify space by param ## Bugfix diff --git a/ngbatis-demo/src/main/java/ye/weicheng/ngbatis/demo/config/Base64PasswordDecoder.java b/ngbatis-demo/src/main/java/ye/weicheng/ngbatis/demo/config/Base64PasswordDecoder.java new file mode 100644 index 00000000..0de738f2 --- /dev/null +++ b/ngbatis-demo/src/main/java/ye/weicheng/ngbatis/demo/config/Base64PasswordDecoder.java @@ -0,0 +1,27 @@ +package ye.weicheng.ngbatis.demo.config; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +import java.util.Base64; +import org.nebula.contrib.ngbatis.PasswordDecoder; +import org.springframework.stereotype.Component; + +/** + * yml 明码解密器的组建示例,使用 Base64 的方式 + * 如 yml 使用的是明文密码,则不需要这个 bean + * + * @author yeweicheng + * @since 2024-05-23 7:39 + *
Now is history! + */ +@Component +public class Base64PasswordDecoder implements PasswordDecoder { + + @Override + public String decode(String password) { + return new String(Base64.getDecoder().decode(password)); + } + +} diff --git a/ngbatis-demo/src/main/resources/application.yml b/ngbatis-demo/src/main/resources/application.yml index 9780fd9c..c2c2f2d9 100644 --- a/ngbatis-demo/src/main/resources/application.yml +++ b/ngbatis-demo/src/main/resources/application.yml @@ -14,7 +14,7 @@ nebula: use-session-pool: false hosts: 127.0.0.1:19669 username: root - password: nebula + password: bmVidWxh space: test pool-config: min-conns-size: 0 diff --git a/src/main/java/org/nebula/contrib/ngbatis/NgbatisBeanFactoryPostProcessor.java b/src/main/java/org/nebula/contrib/ngbatis/NgbatisBeanFactoryPostProcessor.java index 379973b8..10a4cf79 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/NgbatisBeanFactoryPostProcessor.java +++ b/src/main/java/org/nebula/contrib/ngbatis/NgbatisBeanFactoryPostProcessor.java @@ -24,6 +24,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -59,10 +60,19 @@ public NgbatisBeanFactoryPostProcessor(NebulaJdbcProperties nebulaJdbcProperties @Override public void postProcessBeanFactory( ConfigurableListableBeanFactory configurableListableBeanFactory) throws BeansException { + setBeans(configurableListableBeanFactory); NebulaPool nebulaPool = nebulaPool(); mapperContext(nebulaPool); } + private void setBeans(ConfigurableListableBeanFactory beanFactory) { + ObjectProvider passwordDecoders = + beanFactory.getBeanProvider(PasswordDecoder.class); + + PasswordDecoder passwordDecoder = passwordDecoders.getIfAvailable(); + nebulaJdbcProperties.setPasswordDecoder(passwordDecoder); + } + public MapperContext mapperContext(NebulaPool nebulaPool) { DaoResourceLoader daoBasicResourceLoader = new DaoResourceLoader(parseCfgProps); MapperContext context = MapperContext.newInstance(); diff --git a/src/main/java/org/nebula/contrib/ngbatis/PasswordDecoder.java b/src/main/java/org/nebula/contrib/ngbatis/PasswordDecoder.java new file mode 100644 index 00000000..dca55653 --- /dev/null +++ b/src/main/java/org/nebula/contrib/ngbatis/PasswordDecoder.java @@ -0,0 +1,18 @@ +package org.nebula.contrib.ngbatis; + +// Copyright (c) 2024 All project authors. All rights reserved. +// +// This source code is licensed under Apache 2.0 License. + +/** + * 用于:从配置中得到的密码,可以解密获得明文密码 + * + * @author yeweicheng + * @since 2024-05-23 7:31 + *
Now is history! + */ +public interface PasswordDecoder { + + String decode(String password); + +} diff --git a/src/main/java/org/nebula/contrib/ngbatis/config/NebulaJdbcProperties.java b/src/main/java/org/nebula/contrib/ngbatis/config/NebulaJdbcProperties.java index fc894213..3ec29aa7 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/config/NebulaJdbcProperties.java +++ b/src/main/java/org/nebula/contrib/ngbatis/config/NebulaJdbcProperties.java @@ -8,6 +8,8 @@ import com.vesoft.nebula.client.graph.data.HostAddress; import java.util.ArrayList; import java.util.List; +import org.nebula.contrib.ngbatis.PasswordDecoder; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @@ -53,6 +55,9 @@ public class NebulaJdbcProperties { */ private String space; + @Autowired(required = false) + private PasswordDecoder passwordDecoder; + public NebulaJdbcProperties() { } @@ -101,7 +106,7 @@ public NebulaJdbcProperties setUsername(String username) { } public String getPassword() { - return password; + return passwordDecoder == null ? password : passwordDecoder.decode(password); } public NebulaJdbcProperties setPassword(String password) { @@ -127,5 +132,13 @@ public NebulaJdbcProperties setNgbatis(NgbatisConfig ngbatis) { return this; } + + public PasswordDecoder getPasswordDecoder() { + return passwordDecoder; + } + + public void setPasswordDecoder(PasswordDecoder passwordDecoder) { + this.passwordDecoder = passwordDecoder; + } } diff --git a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java index 4d89c3b2..3a1946cf 100644 --- a/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java +++ b/src/main/java/org/nebula/contrib/ngbatis/proxy/NebulaDaoBasic.java @@ -224,7 +224,7 @@ default void insertBatch(List ts) { * 批量插入非空字段 * @param ts 当前Tag下多个顶点 */ - default void insertSelectiveBatch(List ts){ + default void insertSelectiveBatch(List ts) { MethodModel methodModel = getMethodModel(); ClassModel classModel = getClassModel(this.getClass()); MapperProxy.invoke(classModel,methodModel,ts);