RequestBodyAdvice spring全局參數(shù)解密

前言
由于公司業(yè)務(wù)需求需要對(duì)傳來(lái)的密碼解密,每個(gè)都解密一次太過于麻煩,借助spring RequestBodyAdvice 特性來(lái)做統(tǒng)一解密處理

1、自定義注解

package cn.oq.dz.finance.common.annotation;

/**
 * @Description: j
 * @Author: hongwang.zhang
 * @CreateDate: 2019/10/12 15:07
 * @Version: 1.0
 */

import java.lang.annotation.*;

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

    /**
     * 解密方式
     * @return
     */
    String value() default "";
}

package cn.oq.dz.finance.web.advice;

import cn.oq.dz.finance.business.ElectronicAccountBusiness;
import cn.oq.dz.finance.common.annotation.Decrypt;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

/**
 * @Description: spring mvc 全局入?yún)⒔饷? *   需要在 controller  添加 @Decrypt 注解  和需要解密的字段加上 @Decrypt 注解
 * @Author: hongwang.zhang
 * @CreateDate: 2019/10/12 14:54
 * @Version: 1.0
 */
@Slf4j
@ControllerAdvice
public class RequestControllerAdvice  implements RequestBodyAdvice {

    @Autowired
    private ElectronicAccountBusiness electronicAccountBusiness;

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return true;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return null;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        if(parameter.getMethodAnnotation(Decrypt.class) == null){
            return inputMessage;
        }
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {
                String bodyStr = IOUtils.toString(inputMessage.getBody(),"utf-8");
                JSONObject jsonObject = JSONObject.parseObject(bodyStr);
                Class clazz = null;
                try {
                    clazz = Class.forName(targetType.getTypeName());
                } catch (ClassNotFoundException e) {
                    log.info("RequestControllerAdvice:",e);
                }
                if(clazz == null ){
                  return (InputStream) inputMessage;
              }
                for (Field field : clazz.getDeclaredFields()) {
                    Decrypt decrypt = field.getAnnotation(Decrypt.class);
                    if(decrypt != null){
                        jsonObject.put(field.getName(),electronicAccountBusiness.decryptionPassword(jsonObject.getString(field.getName())));
                    }
                }
                return  IOUtils.toInputStream(jsonObject.toJSONString(),"utf-8");
            }
            @Override
            public HttpHeaders getHeaders() {
                return inputMessage.getHeaders();
            }
        };
    }
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
        return body;
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 本文我們來(lái)梳理一下Spring的那些注解,如下圖所示,大概從幾方面列出了Spring的一些注解: 如果此圖看不清楚...
    Java自閉師閱讀 768評(píng)論 0 1
  • 本來(lái)是準(zhǔn)備看一看Spring源碼的。然后在知乎上看到來(lái)一個(gè)帖子,說有一群**自己連Spring官方文檔都沒有完全讀...
    此魚不得水閱讀 7,036評(píng)論 4 21
  • Spring目前的趨勢(shì)是使用注解結(jié)合Java代碼而不是配置來(lái)定義行為、屬性、功能、規(guī)則和擴(kuò)展點(diǎn),因此梳理注解也是梳...
    墨雨軒夏閱讀 1,247評(píng)論 0 30
  • Version 5.0.7.RELEASE 1.Kotlin Kotlin是一門運(yùn)行于JVM(或其他平臺(tái))之上的靜...
    hlwz5735閱讀 3,966評(píng)論 0 3
  • 艾艾貼一一五臟六腑問題一一膽結(jié)石 【艾灸前癥狀】劇痛到滿地打滾,做檢查結(jié)果,膽結(jié)石,開始天天吃藥控制痛的日子,幾天...
    聽雨心依閱讀 651評(píng)論 0 0

友情鏈接更多精彩內(nèi)容