Commit 50940157 authored by tuohai666's avatar tuohai666
Browse files

update encrypt-example

parent bceeb5ba
Loading
Loading
Loading
Loading
+19 −4
Original line number Diff line number Diff line
@@ -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);
    }
}
+1 −1
Original line number Diff line number Diff line
@@ -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);
+6 −0
Original line number Diff line number Diff line
@@ -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() {
+1 −1
Original line number Diff line number Diff line
@@ -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">
+27 −7
Original line number Diff line number Diff line
@@ -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;
        }
    }
}
Loading