提交 50940157 编写于 作者: T tuohai666

update encrypt-example

上级 bceeb5ba
......@@ -27,6 +27,8 @@ public class User implements Serializable {
private String userName;
private String userNamePlain;
private String pwd;
private String assistedQueryPwd;
......@@ -35,7 +37,7 @@ public class User implements Serializable {
return userId;
}
public void setUserId(int userId) {
public void setUserId(final int userId) {
this.userId = userId;
}
......@@ -43,15 +45,23 @@ public class User implements Serializable {
return userName;
}
public void setUserName(String userName) {
public void setUserName(final String userName) {
this.userName = userName;
}
public String getUserNamePlain() {
return userNamePlain;
}
public void setUserNamePlain(final String userNamePlain) {
this.userNamePlain = userNamePlain;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
public void setPwd(final String pwd) {
this.pwd = pwd;
}
......@@ -59,7 +69,12 @@ public class User implements Serializable {
return assistedQueryPwd;
}
public void setAssistedQueryPwd(String assistedQueryPwd) {
public void setAssistedQueryPwd(final String assistedQueryPwd) {
this.assistedQueryPwd = assistedQueryPwd;
}
@Override
public String toString() {
return String.format("user_id: %d, user_name: %s, user_name_plain: %s, pwd: %s, assisted_query_pwd: %s", userId, userName, userNamePlain, pwd, assistedQueryPwd);
}
}
......@@ -39,7 +39,7 @@ public final class UserRepositoryImpl implements UserRepository {
@Override
public void createTableIfNotExists() {
String sql = "CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), pwd VARCHAR(200), assisted_query_pwd VARCHAR(200), PRIMARY KEY (user_id))";
String sql = "CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), user_name_plain VARCHAR(200), pwd VARCHAR(200), assisted_query_pwd VARCHAR(200), PRIMARY KEY (user_id))";
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
......
......@@ -41,6 +41,12 @@ public final class UserEntity extends User {
return super.getUserName();
}
@Column(name = "user_name_plain")
@Override
public String getUserNamePlain() {
return super.getUserNamePlain();
}
@Column(name = "pwd")
@Override
public String getPwd() {
......
......@@ -9,7 +9,7 @@
</resultMap>
<update id="createTableIfNotExists">
CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), pwd VARCHAR(200), assisted_query_pwd VARCHAR(200), PRIMARY KEY (user_id));
CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL AUTO_INCREMENT, user_name VARCHAR(200), user_name_plain VARCHAR(200), pwd VARCHAR(200), assisted_query_pwd VARCHAR(200), PRIMARY KEY (user_id));
</update>
<update id="truncateTable">
......
......@@ -17,26 +17,46 @@
package org.apache.shardingsphere.example.encrypt.table.raw.jdbc.config;
import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encryptor.EncryptorRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;
import org.apache.shardingsphere.example.common.DataSourceUtil;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class EncryptDatabasesConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() {
EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration();
Properties properties = new Properties();
properties.setProperty("aes.key.value", "123456");
EncryptorRuleConfiguration aesRuleConfiguration = new EncryptorRuleConfiguration("AES", "t_user.user_name", properties);
EncryptorRuleConfiguration testRuleConfiguration = new EncryptorRuleConfiguration("assistedTest", "t_user.pwd", "t_user.assisted_query_pwd", properties);
encryptRuleConfiguration.getEncryptorRuleConfigs().put("name_encryptor", aesRuleConfiguration);
encryptRuleConfiguration.getEncryptorRuleConfigs().put("pwd_encryptor", testRuleConfiguration);
return EncryptDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), encryptRuleConfiguration);
properties.setProperty("query.with.cipher.column", "true");
EncryptorRuleConfiguration aesRuleConfiguration = new EncryptorRuleConfiguration("aes", properties);
EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("user_name_plain", "user_name", "", "name_encryptor");
Map<String, EncryptColumnRuleConfiguration> columns = new HashMap<>();
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columns);
columns.put("user_name", columnConfigAes);
encryptRuleConfiguration.getEncryptors().put("name_encryptor", aesRuleConfiguration);
encryptRuleConfiguration.getTables().put("t_user", tableConfig);
EncryptorRuleConfiguration testRuleConfiguration = new EncryptorRuleConfiguration("assistedTest", properties);
EncryptColumnRuleConfiguration columnConfigTest = new EncryptColumnRuleConfiguration("", "pwd", "assisted_query_pwd", "pwd_encryptor");
columns.put("pwd", columnConfigTest);
tableConfig.getColumns().putAll(columns);
encryptRuleConfiguration.getEncryptors().put("pwd_encryptor", testRuleConfiguration);
try {
return EncryptDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), encryptRuleConfiguration, properties);
} catch (final SQLException ex) {
ex.printStackTrace();
return null;
}
}
}
......@@ -8,10 +8,21 @@ encryptRule:
encryptors:
name_encryptror:
type: aes
qualifiedColumns: t_user.name
props:
aes.key.value: 123456
pwd_encryptror:
type: assistedTest
qualifiedColumns: t_user.pwd
assistedQueryColumns: t_user.assisted_query_pwd
tables:
t_user:
columns:
user_name:
plainColumn: user_name_plain
cipherColumn: user_name
encryptor: name_encryptror
pwd:
cipherColumn: pwd
encryptor: pwd_encryptror
assistedQueryColumn: assisted_query_pwd
props:
query.with.cipher.column: true
\ No newline at end of file
......@@ -7,10 +7,15 @@ spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=
spring.shardingsphere.encrypt.encryptors.name_encryptor.type=aes
spring.shardingsphere.encrypt.encryptors.name_encryptor.qualifiedColumns=t_user.user_name
spring.shardingsphere.encrypt.encryptors.name_encryptor.props.aes.key.value=123456
spring.shardingsphere.encrypt.encryptors.pwd_encryptor.type=assistedTest
spring.shardingsphere.encrypt.encryptors.pwd_encryptor.qualifiedColumns=t_user.pwd
spring.shardingsphere.encrypt.encryptors.pwd_encryptor.assistedQueryColumns=t_user.assisted_query_pwd
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.plainColumn=user_name_plain
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.cipherColumn=user_name
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.encryptor=name_encryptor
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.cipherColumn=pwd
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.assistedQueryColumn=assisted_query_pwd
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.encryptor=pwd_encryptor
spring.shardingsphere.props.query.with.cipher.comlum=true
......@@ -28,8 +28,21 @@
</bean:properties>
<encrypt:data-source id="encryptDataSource" data-source-name="ds" >
<encrypt:encryptor-rule id="pwd_encryptor" type="assistedTest" qualified-columns="t_user.pwd" assisted-query-columns="t_user.assisted_query_pwd" />
<encrypt:encryptor-rule id="name_encryptor" type="AES" qualified-columns="t_user.user_name" props-ref="props" />
<encrypt:encrypt-rule>
<encrypt:tables>
<encrypt:table name="t_user">
<encrypt:column logic-column="user_name" plain-column="user_name_plain" cipher-column="user_name" encryptor-ref="name_encryptor" />
<encrypt:column logic-column="pwd" cipher-column="pwd" assisted-query-column="assisted_query_pwd" encryptor-ref="pwd_encryptor"/>
</encrypt:table>
</encrypt:tables>
<encrypt:encryptors>
<encrypt:encryptor id="name_encryptor" type="AES" props-ref="props"/>
<encrypt:encryptor id="pwd_encryptor" type="assistedTest" />
</encrypt:encryptors>
</encrypt:encrypt-rule>
<encrypt:props>
<prop key="query.with.cipher.column">true</prop>
</encrypt:props>
</encrypt:data-source>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册