package com.persagy.adm.steward.basic.config;

import com.alibaba.fastjson.JSONObject;
import com.persagy.adm.steward.basic.utils.RSAUtils;
import com.persagy.dmp.common.helper.SpringHelper;
import feign.Request;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
/**
 * 加密拦截器
 * @author linhuili
 * @date 2022-01-22
 */
@Configuration
@Slf4j
public class StewardEncodeInterceptor implements RequestInterceptor {

    private static final String ENCRYPTION_RSA="encryption-rsa";

    @Override
    public void apply(RequestTemplate template) {
        if (!template.headers().containsKey(ENCRYPTION_RSA)){
            return;
        }
        // 更改请求体中的content的值
        Request.Body body = template.requestBody();
        JSONObject temp = JSONObject.parseObject(body.asString());
        template.body(RSAUtils.decode(body.asString(), SpringHelper.getString("adm.rsa.privateKey")));
    }
}