Get the App
SLTechnology News&Howtos  ›  Development  › 

How to encrypt and decrypt MyBatis field through an annotation

Shulou Source: shulou.com Published: 2022-06-01 18:02:40 09月29日 Update

This article mainly explains "how to achieve MyBatis field encryption and decryption through an annotation", the content of the article is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "how to achieve MyBatis field encryption and decryption through an annotation" bar!

Brief introduction

Mybatis-crypto is a field encryption and decryption component based on mybatis plug-in mechanism, which can encrypt and decrypt sensitive data through an annotation. Custom Encryptor is supported, and Encryptor and key are specified separately for special fields to meet most usage scenarios.

Module

Mybatis-crypto consists of three modules:

Core functional modules of the mybatis-crypto-core plug-in

Mybatis-crypto-spring-boot-starter provides rapid integration of Spring boot

Mybatis-crypto-encryptors provides some IEncryptor implementations

Usage

Introduce dependency

Io.github.whitedg mybatis-crypto-spring-boot-starter ${latest.version}

Implement IEncryptor

Import io.github.whitedg.mybatis.crypto.IEncryptor;public class MyEncryptor implements IEncryptor {@ Override public String encrypt (Object val2bEncrypted, String key) throws Exception {/ / implement this method to return encrypted data return "encrypted string";} @ Override public String decrypt (Object val2bDecrypted, String key) throws Exception {/ / implement this method to return decrypted data return "decrypted string";}}

Or introduce mybatis-crypto-encryptors

Io.github.whitedg mybatis-crypto-encryptors ${latest.version}

Use the Encryptor provided by it:

Io.github.whitedg.mybatis.crypto.Base64Encryptor

Io.github.whitedg.mybatis.crypto.BasicTextEncryptor

Io.github.whitedg.mybatis.crypto.AES256Encryptor

Io.github.whitedg.mybatis.crypto.StrongTextEncryptor

Add configuration

Mybatis-crypto: # whether to enable plug-ins, default true enabled: true # Quick failure, default true fail-fast: false # Global default Encryptor default-encryptor: io.github.whitedg.mybatis.crypto.BasicTextEncryptor # Encryptor default key default-key: global-key # mybatis @ Param the parameter key prefix mapped-key-prefixes: et,encrypted under the annotation

Specify encryption field

Add @ EncryptedField to the fields that need to be encrypted and decrypted

Public class User {@ EncryptedField private String encryptedStr; @ EncryptedField (encryptor = YourEncryptor.class, key = "Your Key") private String customizedStr;}

Use the configured @ Param parameter key prefix

Import org.apache.ibatis.annotations.Param;interface YourEntityMapper {int insert (@ Param ("et") YourEntity entity); / / supports array int batchInsert (@ Param ("encrypted-entities") List entity); / / the return value also supports single object or array YourEntity selectOne (); List selectList ();}

Demo

Configuration item description configuration item indicates whether the default value mybatis-crypto.enabled enables mybatis-cryptotruemybatis-crypto.fail-fast fast failure, and whether an exception occurs during encryption and decryption is interrupted. True: throw an exception, false: print the prefix of the warn level log truemybatis-crypto.mapped-key-prefixes@Param parameter name using the original value, and the prefix matching will be encrypted. Empty mybatis-crypto.default-encryptor global default Encryptor empty mybatis-crypto.default-key global default Encryptor key empty thank you for reading, the above is the content of "how to encrypt and decrypt the MyBatis field through an annotation", after the study of this article I believe you have a deeper understanding of how to achieve MyBatis field encryption and decryption through an annotation, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Fields annotations prefixes configurations globals parameters plug-ins data methods modules encryption learning support content functions keys arrays processing primitive special Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Xiaomi Shulou Tech Info Huawei