Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to encrypt and decrypt MyBatis field through an annotation

2025-01-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report