forked from DataLinkDC/dinky
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: database config and fragment variable encrypt (DataLinkDC#2178)
* feat: database config and fragment variable encrypt * Update db-h2.sql dinky_database table password column
- Loading branch information
Showing
17 changed files
with
426 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
dinky-admin/src/main/java/org/dinky/crypto/CryptoComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.dinky.crypto; | ||
|
||
import org.jasypt.util.text.AES256TextEncryptor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class CryptoComponent { | ||
|
||
private final boolean enabled; | ||
private final AES256TextEncryptor textEncryptor; | ||
|
||
public CryptoComponent(CryptoProperties cryptoProperties) { | ||
this.enabled = cryptoProperties.getEnabled(); | ||
this.textEncryptor = new AES256TextEncryptor(); | ||
if (cryptoProperties.getEncryptionPassword() != null) { | ||
this.textEncryptor.setPassword(cryptoProperties.getEncryptionPassword()); | ||
} | ||
} | ||
|
||
public String encryptText(String result) { | ||
if (!enabled) { | ||
return result; | ||
} | ||
|
||
return textEncryptor.encrypt(result); | ||
} | ||
|
||
public String decryptText(String result) { | ||
if (!enabled) { | ||
return result; | ||
} | ||
|
||
if (result == null) { | ||
return null; | ||
} | ||
return textEncryptor.decrypt(result); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
dinky-admin/src/main/java/org/dinky/crypto/CryptoProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* | ||
* 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.dinky.crypto; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
@Setter | ||
@Getter | ||
@ConfigurationProperties(prefix = "crypto") | ||
@Component | ||
public class CryptoProperties { | ||
/** 是否启用加解密配置 */ | ||
private Boolean enabled = false; | ||
/** 加密密钥 */ | ||
private String encryptionPassword; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
dinky-admin/src/main/java/org/dinky/mybatis/crypto/CryptoTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* | ||
* 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.dinky.mybatis.crypto; | ||
|
||
import org.dinky.context.SpringContextUtils; | ||
import org.dinky.crypto.CryptoComponent; | ||
|
||
import org.apache.ibatis.type.BaseTypeHandler; | ||
import org.apache.ibatis.type.JdbcType; | ||
|
||
import java.sql.CallableStatement; | ||
import java.sql.PreparedStatement; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class CryptoTypeHandler extends BaseTypeHandler<String> { | ||
|
||
private final CryptoComponent cryptoComponent; | ||
|
||
public CryptoTypeHandler() { | ||
cryptoComponent = SpringContextUtils.getBeanByClass(CryptoComponent.class); | ||
} | ||
|
||
@Override | ||
public void setNonNullParameter( | ||
PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { | ||
ps.setString(i, encryptText(parameter)); | ||
} | ||
|
||
@Override | ||
public String getNullableResult(ResultSet rs, String columnName) throws SQLException { | ||
return decryptText(rs.getString(columnName)); | ||
} | ||
|
||
@Override | ||
public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException { | ||
return decryptText(rs.getString(columnIndex)); | ||
} | ||
|
||
@Override | ||
public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { | ||
return decryptText(cs.getString(columnIndex)); | ||
} | ||
|
||
private String encryptText(String result) { | ||
return cryptoComponent.encryptText(result); | ||
} | ||
|
||
private String decryptText(String result) { | ||
return cryptoComponent.decryptText(result); | ||
} | ||
} |
109 changes: 109 additions & 0 deletions
109
dinky-admin/src/main/java/org/dinky/mybatis/crypto/MybatisPlusCustomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* | ||
* 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.dinky.mybatis.crypto; | ||
|
||
import org.apache.ibatis.builder.MapperBuilderAssistant; | ||
import org.apache.ibatis.mapping.MappedStatement; | ||
import org.apache.ibatis.mapping.ResultMap; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.ReflectionUtils; | ||
|
||
import com.baomidou.mybatisplus.annotation.TableName; | ||
import com.baomidou.mybatisplus.autoconfigure.SqlSessionFactoryBeanCustomizer; | ||
import com.baomidou.mybatisplus.core.MybatisConfiguration; | ||
import com.baomidou.mybatisplus.core.metadata.TableInfo; | ||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; | ||
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean; | ||
|
||
@Component | ||
public class MybatisPlusCustomizer implements SqlSessionFactoryBeanCustomizer { | ||
|
||
@Override | ||
public void customize(MybatisSqlSessionFactoryBean factoryBean) { | ||
factoryBean.setConfiguration( | ||
new MybatisConfiguration() { | ||
@Override | ||
public void addMappedStatement(MappedStatement ms) { | ||
final String ns = ms.getId().replaceAll("(.*Mapper)\\.\\w+$", "$1"); | ||
final MapperBuilderAssistant builderAssistant = | ||
new MapperBuilderAssistant(ms.getConfiguration(), ""); | ||
builderAssistant.setCurrentNamespace(ns); | ||
boolean[] changes = new boolean[] {false}; | ||
final List<ResultMap> collect = | ||
ms.getResultMaps().stream() | ||
.map( | ||
m -> { | ||
// 仅ResultType为Table且包含TypeHandler时,替换ResultMap为MybatisPlus生成的 | ||
TableName table = | ||
m.getType() | ||
.getAnnotation(TableName.class); | ||
if (table != null) { | ||
TableInfo tableInfo = | ||
TableInfoHelper.getTableInfo( | ||
m.getType()); | ||
if (tableInfo == null) { | ||
tableInfo = | ||
TableInfoHelper.initTableInfo( | ||
builderAssistant, | ||
m.getType()); | ||
} | ||
if (tableInfo != null | ||
&& tableInfo.getResultMap() != null | ||
&& tableInfo.getFieldList().stream() | ||
.anyMatch( | ||
f -> | ||
f | ||
.getTypeHandler() | ||
!= null)) { | ||
ResultMap resultMap = | ||
ms.getConfiguration() | ||
.getResultMap( | ||
tableInfo | ||
.getResultMap()); | ||
if (resultMap != null | ||
&& resultMap != m) { | ||
changes[0] = true; | ||
return resultMap; | ||
} | ||
} | ||
} | ||
return m; | ||
}) | ||
.collect(Collectors.toList()); | ||
if (changes[0]) { | ||
final Field field = | ||
ReflectionUtils.findField(ms.getClass(), "resultMaps"); | ||
if (field != null) { | ||
ReflectionUtils.makeAccessible(field); | ||
ReflectionUtils.setField( | ||
field, ms, Collections.unmodifiableList(collect)); | ||
} | ||
} | ||
super.addMappedStatement(ms); | ||
} | ||
}); | ||
} | ||
} |
Oops, something went wrong.