Java教程

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

本文主要是介绍转 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进行解密
这篇关于转 Spring 接口数据加密---局部加密篇的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!