Encryption

Background

Spring Namespace’s data encryption configuration applies to the traditional Spring projects. Sharding rules and attributes are configured through the XML configuration file of the namespace. Spring creates and manages the ShardingSphereDataSource object, reducing unnecessary coding.

Parameters

Namespace: http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt-5.2.1.xsd

<encrypt:rule />

NameTypeDescriptionDefault Value
idAttributeSpring Bean Id
queryWithCipherColumn (?)AttributeWhether query with cipher column for data encrypt. User you can use plaintext to query if havetrue
table (+)TagEncrypt table configuration

<encrypt:table />

NameTypeDescription
nameAttributeEncrypt table name
column (+)TagEncrypt column configuration
query-with-cipher-column(?) (?)AttributeWhether the table query with cipher column for data encrypt. User you can use plaintext to query if have

<encrypt:column />

NameTypeDescription
logic-columnAttributeColumn logic name
cipher-columnAttributeCipher column name
assisted-query-column (?)AttributeAssisted query column name
plain-column (?)AttributePlain column name
encrypt-algorithm-refAttributeEncrypt algorithm name

<encrypt:encrypt-algorithm />

NameTypeDescription
idAttributeEncrypt algorithm name
typeAttributeEncrypt algorithm type
props (?)TagEncrypt algorithm properties

Please refer to Built-in Encrypt Algorithm List for more details about type of algorithm.

Procedure

  1. Configure data encryption rules in the Spring namespace configuration file, including data sources, encryption rules, and global attributes.
  2. Start the Spring program, and it will automatically load the configuration and initialize the ShardingSphereDataSource.

Sample

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:shardingsphere="http://shardingsphere.apache.org/schema/shardingsphere/datasource"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans.xsd
  9. http://www.springframework.org/schema/tx
  10. http://www.springframework.org/schema/tx/spring-tx.xsd
  11. http://www.springframework.org/schema/context
  12. http://www.springframework.org/schema/context/spring-context.xsd
  13. http://shardingsphere.apache.org/schema/shardingsphere/datasource
  14. http://shardingsphere.apache.org/schema/shardingsphere/datasource/datasource.xsd
  15. http://shardingsphere.apache.org/schema/shardingsphere/encrypt
  16. http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
  17. ">
  18. <context:component-scan base-package="org.apache.shardingsphere.example.core.mybatis" />
  19. <bean id="ds" class="com.zaxxer.hikari.HikariDataSource" destroy-method="close">
  20. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  21. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&amp;useSSL=false&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
  22. <property name="username" value="root"/>
  23. <property name="password" value=""/>
  24. </bean>
  25. <encrypt:encrypt-algorithm id="name_encryptor" type="AES">
  26. <props>
  27. <prop key="aes-key-value">123456</prop>
  28. </props>
  29. </encrypt:encrypt-algorithm>
  30. <encrypt:encrypt-algorithm id="pwd_encryptor" type="assistedTest" />
  31. <encrypt:rule id="encryptRule">
  32. <encrypt:table name="t_user">
  33. <encrypt:column logic-column="username" cipher-column="username" plain-column="username_plain" encrypt-algorithm-ref="name_encryptor" />
  34. <encrypt:column logic-column="pwd" cipher-column="pwd" assisted-query-column="assisted_query_pwd" encrypt-algorithm-ref="pwd_encryptor" />
  35. </encrypt:table>
  36. </encrypt:rule>
  37. <shardingsphere:data-source id="encryptDataSource" data-source-names="ds" rule-refs="encryptRule" />
  38. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  39. <property name="dataSource" ref="encryptDataSource" />
  40. </bean>
  41. <tx:annotation-driven />
  42. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  43. <property name="dataSource" ref="encryptDataSource"/>
  44. <property name="mapperLocations" value="classpath*:META-INF/mappers/*.xml"/>
  45. </bean>
  46. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  47. <property name="basePackage" value="org.apache.shardingsphere.example.core.mybatis.repository"/>
  48. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  49. </bean>
  50. </beans>