欢迎来到徐庆高(Tea)的个人博客网站
磨难很爱我,一度将我连根拔起。从惊慌失措到心力交瘁,我孤身一人,但并不孤独无依。依赖那些依赖我的人,信任那些信任我的人,帮助那些给予我帮助的人。如果我愿意,可以分裂成无数面镜子,让他们看见我,就像看见自己。察言观色和模仿学习是我的领域。像每个深受创伤的人那样,最终,我学会了随遇而安。
当前位置: 日志文章 > 详细内容

mybatis拦截器自动加密解密教程

2025年08月08日 Java
mybatis拦截器自动加密解密来看实例import kai8.system.annotation.encryptdecryptfield;import lombok.extern.slf4j.slf

mybatis拦截器自动加密解密

来看实例

import kai8.system.annotation.encryptdecryptfield;
import lombok.extern.slf4j.slf4j;
import org.apache.ibatis.executor.parameter.parameterhandler;
import org.apache.ibatis.plugin.interceptor;
import org.apache.ibatis.plugin.intercepts;
import org.apache.ibatis.plugin.invocation;
import org.apache.ibatis.plugin.signature;
import org.springframework.stereotype.component;

import java.lang.reflect.field;
import java.lang.reflect.method;
import java.sql.preparedstatement;
import java.util.objects;

@slf4j
@component
@intercepts({
        @signature(type = parameterhandler.class, method = "setparameters", args = preparedstatement.class)
})
public class encryptioninterceptor implements interceptor {

    @override
    public object intercept(invocation invocation) throws throwable {
        parameterhandler item = (parameterhandler) invocation.gettarget();
        field parameterobject = item.getclass().getdeclaredfield("parameterobject");
        parameterobject.setaccessible(true);
        object o = parameterobject.get(item);
        if (!objects.isnull(o)) {
            class<?> aclass = o.getclass();
            for (; aclass != object.class; aclass = aclass.getsuperclass()) {//向上循环  遍历父类
                field[] declaredfields = aclass.getdeclaredfields();
                for (field field : declaredfields) {
                    // 如果属性带有encryptfield注解放到要加解密的集合中
                    if (field.isannotationpresent(encryptdecryptfield.class)) {
                        field.setaccessible(true);
                        try {
                            // 假设你有某种方法获取加密值(根据你的逻辑)
                            string encryptedvalue = (string) field.get(o);
                            if (encryptedvalue != null && !"".equals(encryptedvalue)) {
                                encryptdecryptfield annotation = field.getannotation(encryptdecryptfield.class);
                                class<?> value = annotation.value();
                                method method = value.getmethod(annotation.encrypt(), string.class);
                                object invoke = method.invoke(value, encryptedvalue);
                                field.set(o, invoke);
                            } else {
                                field.set(o, null);
                            }
                        } catch (illegalaccessexception e) {
                            // 处理异常
                            log.error(e.getmessage(), e);
                        }
                    }
                }
            }
        }
        return invocation.proceed();

    }
}
import kai8.system.annotation.encryptdecryptfield;
import lombok.extern.slf4j.slf4j;
import org.apache.ibatis.executor.parameter.parameterhandler;
import org.apache.ibatis.executor.resultset.resultsethandler;
import org.apache.ibatis.plugin.interceptor;
import org.apache.ibatis.plugin.intercepts;
import org.apache.ibatis.plugin.invocation;
import org.apache.ibatis.plugin.signature;

import java.lang.reflect.field;
import java.lang.reflect.method;
import java.sql.statement;
import java.util.objects;

@slf4j
@intercepts({@signature(type = resultsethandler.class, method = "handleresultsets", args = {statement.class})})
public class decryptinterceptor implements interceptor {

    @override
    public object intercept(invocation invocation) throws throwable {
        parameterhandler item = (parameterhandler) invocation.gettarget();
        field parameterobject = item.getclass().getdeclaredfield("parameterobject");
        parameterobject.setaccessible(true);
        object o = parameterobject.get(item);
        if (!objects.isnull(o)) {
            class<?> aclass = o.getclass();
            for (; aclass != object.class; aclass = aclass.getsuperclass()) {//向上循环  遍历父类
                field[] declaredfields = aclass.getdeclaredfields();
                for (field field : declaredfields) {
                    // 如果属性带有encryptfield注解放到要加解密的集合中
                    if (field.isannotationpresent(encryptdecryptfield.class)) {
                        field.setaccessible(true);
                        try {
                            // 假设你有某种方法获取加密值(根据你的逻辑)
                            string encryptedvalue = (string) field.get(o);
                            if (encryptedvalue != null) {
                                encryptdecryptfield annotation = field.getannotation(encryptdecryptfield.class);
                                class<?> value = annotation.value();
                                method method = value.getmethod(annotation.decrypt(), string.class);
                                object invoke = method.invoke(value, encryptedvalue);
                                // string decryptedvalue = arithmeticapi.base64decod(encryptedvalue);
                                field.set(o, invoke);
                            } else {
                                field.set(o, null);
                            }
                        } catch (illegalaccessexception e) {
                            // 处理异常
                            log.error(e.getmessage(), e);
                        }
                    }
                }
            }
        }
        return invocation.proceed();
    }
}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。