转 Spring 接口数据加密—局部加密篇


Spring 接口数据加密—局部加密

  • 注解 RequestBody 解密 ResponseBody 加密 使用

注解

package com.base.project.commcon.annotation.des;

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DecryptRequestBody {

    boolean dencrypt() default true;
}

DecryptRequestBody注解

自定义解密注解,默认设置解密==true, 注解适用于方法上

package com.base.project.commcon.annotation.des;

import java.lang.annotation.*;

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EncryptResponBody {

    boolean encrypt() default true;
}

EncryptResponBody

自定义解密注解

RequestBody 解密

package com.base.project.advice;

import com.base.project.commcon.annotation.des.DecryptRequestBody;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;

/**
 * 这里只针对application/json的数据进行请求解密
 */
@ControllerAdvice
public class AppRequestBodyAdvice implements RequestBodyAdvice {
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {

        if(GsonHttpMessageConverter.class.isAssignableFrom(aClass)){

            return true;
        }
        return false;
    }

    @Override
    public Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {

        return o;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
        boolean dencrypt = false;
        HttpInputMessage returnInputMessage = null;

        if(methodParameter.getMethod().isAnnotationPresent(DecryptRequestBody.class)) {

            DecryptRequestBody body = methodParameter.getMethodAnnotation(DecryptRequestBody.class);
            dencrypt = body.dencrypt();
        }
        if(dencrypt) {

            InputStream is = httpInputMessage.getBody();

            //在此处对数据进行解密

            returnInputMessage = new DecryptHttpInputMessage(httpInputMessage.getHeaders(), this.toDencrypt(is));
            //returnInputMessage =
        }else {

            returnInputMessage = httpInputMessage;
        }

        return returnInputMessage;
    }

    @Override
    public Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {

        return o;
    }

    public InputStream toDencrypt(InputStream is) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[4096];
        int n = 0;
        while ((n = is.read(b)) > 0) {
            out.write(b, 0, n);
        }
        byte[] buf = out.toByteArray();

        //对buf进行解密

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/292529.html

(0)
上一篇 2022年11月3日
下一篇 2022年11月3日

相关推荐

发表回复

登录后才能评论