Coder Social home page Coder Social logo

mybatis-crypt's Introduction

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>

mybatis-crypt's People

Contributors

miaoxinwei avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.