All Projects → miaoxinwei → mybatis-crypt

miaoxinwei / mybatis-crypt

Licence: MIT License
mybatis 拦截器--实现数据库数据脱敏

Programming Languages

java
68154 projects - #9 most used programming language
shell
77523 projects

Labels

Projects that are alternatives of or similar to mybatis-crypt

mybatis-typehandlers-postgis
MyBatis Type Handlers for PostGIS
Stars: ✭ 58 (-25.64%)
Mutual labels:  mybatis3
mybatis-helper
Mybatis plugins package
Stars: ✭ 13 (-83.33%)
Mutual labels:  mybatis3
mybatis-mapper
generate SQL statements from the MyBatis3 Mapper XML file in Node.js
Stars: ✭ 64 (-17.95%)
Mutual labels:  mybatis3
anyangdp-frame
基于mybatis,springboot,tk.mybatis等框架二次开发,实现crud,controller,service,dao。
Stars: ✭ 16 (-79.49%)
Mutual labels:  mybatis3
lightbatis
Lightbatis 增强 MyBatis 版Java 数据库持久层,更简洁并易用
Stars: ✭ 52 (-33.33%)
Mutual labels:  mybatis3
sqlt
SqlT golang实现的类似MyBaits的Sql 工具
Stars: ✭ 28 (-64.1%)
Mutual labels:  mybatis3

mybatis-crypt

mybatis 拦截器--实现数据库数据脱敏

介绍

注解类 @CryptField — 可作用于类成员变量、方法、方法参数

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
public @interface CryptField {

    String value() default "";

    boolean encrypt() default true;

    boolean decrypt() default true;
}

使用方法

  1. 引入jar包

  2. 配置mybatis插件

    • xml

      <!-- 配置SqlSessionFactory -->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <!-- 数据源 -->
              <property name="dataSource" ref="dataSource"/>
              <property name="mapperLocations">
                  <array>
                      <value>classpath:mapper/*.xml</value>
                  </array>
              </property>
              <property name="plugins">
                  <array>
                      <bean id="cryptInterceptor" class="org.apache.ibatis.plugin.CryptInterceptor">
                      </bean>
                  </array>
              </property>
          </bean>
    • java config

      @Bean(name = "sqlSessionFactory")
      public SqlSessionFactory sqlSessionFactory() throws Exception {
      	SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
      	PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
              factoryBean.setMapperLocations(resolver.getResources("classpath:/mapper/*.xml"));
      	factoryBean.setDataSource(dataSource);
        
              List<Interceptor> interceptors = Lists.newArrayList();
      
      	CryptInterceptor cryptInterceptor = new CryptInterceptor();
      	interceptors.add(cryptInterceptor);
      
      	factoryBean.setPlugins(interceptors.toArray(new Interceptor[interceptors.size()]));
      
      	return factoryBean.getObject();
      }
  3. 注解使用场景

  • 方法

    表示返回值需要解密,适用对象包括String、List<String>、JavaBean、List<JavaBean>

    @CryptField
    List<AppInfo> select(String aaa);
  • 类成员变量

    表示类成员需要加解密,具体看使用场景、适用对象包括String、List<String>

  • 方法参数

    表示方法参数需要加密,适用对象包括String、List<String>、JavaBean、List<JavaBean>

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].