您好,欢迎访问代理记账网站
移动应用 微信公众号 联系我们

咨询热线 -

电话 15988168888

联系客服
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

springboot+mybatis+shardingsphere对数据进行加密遇到了点问题。求解答

application.properties中关于shardingsphere的如下:

# 配置真实数据源
spring.shardingsphere.datasource.names=ds1
# 配置第 1 个数据源
spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver
spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&allowMultiQueries=true&useSSL=false
spring.shardingsphere.datasource.ds1.username=root
spring.shardingsphere.datasource.ds1.password=991022

# 该表是否使用加密列进行查询
#spring.shardingsphere.rules.encrypt.tables.sys_user.query-with-cipher-column=true
# 密文列名称
spring.shardingsphere.rules.encrypt.tables.sys_user.columns.password.cipher-column=pwd_cipher
# 逻辑列名称
spring.shardingsphere.rules.encrypt.tables.sys_user.columns.password.logicColumn=password
# 查询列名称
#spring.shardingsphere.rules.encrypt.tables.sys_user.columns.password.assisted-query-column=password
# 原文列名称
spring.shardingsphere.rules.encrypt.tables.sys_user.columns.password.plainColumn=password
# 加密算法名称
spring.shardingsphere.rules.encrypt.tables.sys_user.columns.password.encryptor-name=password-encryptor

# 加密算法配置
# 是否使用加密列进行查询。在有原文列的情况下,可以使用原文列进行查询
spring.shardingsphere.rules.encrypt.queryWithCipherColumn=true
# 加密算法类型
spring.shardingsphere.rules.encrypt.encryptors.password-encryptor.type=AES
# 加密算法属性配置
spring.shardingsphere.rules.encrypt.encryptors.password-encryptor.props.aes-key-value=123456abc

spring.shardingsphere.props.query-with-cipher-comlum=true
spring.shardingsphere.props.sql-show=true

DataSourceConfig:

@Configuration
@EnableTransactionManagement
@MapperScan(value = {"com.dao.ds1"}, sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class Ds1DataSourceConfig {
    private static final String TYPE_ALIASES_PACKAGE = "com.dao.ds1.*.entity";
    private static final String MAPPER_LOCATION = "classpath:mapper/ds1/*/*.xml";

    @Bean(name = "ds1DataSource")
    @Qualifier("ds1DataSource")
    @ConfigurationProperties(prefix = "spring.shardingsphere.datasource.ds1")
   //@Primary
    public DataSource db1DataSource() {
        return  DataSourceBuilder.create().type(HikariDataSource.class).build();
//        return new DruidDataSource();
    }

    @Bean(name = "ds1SqlSessionFactory")
   // @Primary
    public SqlSessionFactory db1SqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        // 加入sql语句拦截器
        //分页插件
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        PageInterceptor interceptor = new SqlStatementInterceptor();
        interceptor.setProperties(properties);
        bean.setPlugins(new PageInterceptor[]{interceptor});
        bean.setTypeAliasesPackage(TYPE_ALIASES_PACKAGE);
        bean.setMapperLocations(
                new PathMatchingResourcePatternResolver()
                        .getResources(MAPPER_LOCATION));
        return bean.getObject();
    }

    @Bean(name = "ds1TransactionManager")
   // @Primary
    public DataSourceTransactionManager db1TransactionManager(@Qualifier("ds1DataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "ds1SqlSessionTemplate")
   // @Primary
    public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("ds1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

测试类:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@EnableAutoConfiguration(exclude = RedisInit.class)
public class ShardingTest {
    @Autowired
    private Ds1SysUserMapper sysUserMapper;
    @Test
    public void test01(){
        SysUser sysUser = new SysUser();
        sysUser.setId(TextUtils.getUUID());
        sysUser.setPassword("123456");
        sysUser.setUsername("加密测试");
        sysUser.setNickName("加密测试");
        sysUser.setSex(1);
        sysUser.setPhoneOne("13263336666");
        sysUserMapper.insert(sysUser);
    }
}

我看文档以及一些文章都是配置了数据源和加密列这些之后,使用shardingsphere的数据源就可以了。但是我是一直都这样,插入到数据库的都是没有加密的。配置文件中去掉原文列只使用加密列也还是这样。debug到源码中是看到shardingsphere取到了配置的数据的。还是说需要配置其他什么吗。求大佬解答。万分感谢!


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进