接口加解密

/**
 * 这个就是一个标识是否加解密的注解
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CryptFlag {

}
@Slf4j
@ControllerAdvice
public class CryptResponseBodyAdvice implements ResponseBodyAdvice {


    @Qualifier(value = "initAes")
    @Autowired
    private InitAes initAes;

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        Method method = returnType.getMethod();
        String url = request.getURI().toASCIIString();
        CryptFlag cryptFlag = method.getAnnotation(CryptFlag.class);
        //有@CryptFlag注解的接口加密,没有就普通返回

        ObjectMapper mapper = new ObjectMapper();

        String o = null;

        try {

            o = mapper.writeValueAsString(body);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }

        if (null != cryptFlag) {
            log.info("{}.{},url={},加密数据为:{},原始数据为:{}", method.getDeclaringClass().getSimpleName(),
                    method.getName(), url, encrypt(o.toString(), initAes), o.toString());
            return encrypt(o.toString(), initAes);
        } else {
            log.info("{}.{},url={},result={}", method.getDeclaringClass().getSimpleName(), method.getName(), url, body);
            return body;
        }

    }


}
@Slf4j
@ControllerAdvice
public class RequestBodyBefore implements RequestBodyAdvice {

    @Qualifier(value = "initAes")
    @Autowired
    private InitAes initAes;


    @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 body;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {

                //这个里面进行处理
                //判断该接口上是否有注解
                Method method = parameter.getMethod();
                CryptFlag cryptFlag = method.getAnnotation(CryptFlag.class);
                String controllerName = parameter.getContainingClass().toString() + parameter.getMethod().getName();
                String json = "";
                //含有加解密的注解
                if (null != cryptFlag) {
                    //获得加密字符串
                    String enCryptStr = IOUtils.toString(inputMessage.getBody());
                    log.info("加密字符串为:{}", enCryptStr);
                    json = decrypt(enCryptStr, initAes);
                    log.info("解密过后的json字符串为:{}", json);
                } else {
                    json = IOUtils.toString(inputMessage.getBody());
                    log.info("json字符串为:{}", json);
                }
                log.info("方法签名为:{}", controllerName);
                return new ByteArrayInputStream(json.getBytes(inputMessage.getHeaders().getContentType().getCharset()));

            }

            @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;
    }


}

 

转载于:https://www.cnblogs.com/java-le/p/11634380.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中,可以使用RequestBodyAdvice和ResponseBodyAdvice来实现接口的加密和解密逻辑。这两个接口可以在方法中使用@RequestBody和@ResponseBody注解时生效。具体的实现步骤如下: 1. 创建一个自动配置类,比如AppConfig,用于配置加密和解密的相关参数。在该类中,可以使用@Bean注解创建一个AES对象,用于进行加密和解密操作。可以参考\[3\]中的示例代码。 2. 在自动配置类中,可以使用@Configuration注解标记该类为配置类,并使用@Resource注解注入CryptConfig对象,该对象用于获取加密和解密的相关配置参数。 3. 在加密和解密的逻辑中,可以使用AES对象进行加密和解密操作。可以根据具体的需求选择合适的加密模式、填充方式、密钥和向量等参数。 4. 在接口方法中,使用@RequestBody注解标记需要加密的请求数据,在方法中进行解密操作。然后处理相应的业务逻辑,并使用@ResponseBody注解标记需要加密的返回数据,在返回之前进行加密操作。 通过以上步骤,可以将加密和解密的逻辑提取出来,使接口方法只关注业务逻辑的处理。具体的实现可以参考\[2\]中的项目结构示例代码。 总结起来,使用Spring Boot的RequestBodyAdvice和ResponseBodyAdvice可以很方便地实现接口的加密和解密逻辑,提高代码的可维护性和安全性。 #### 引用[.reference_title] - *1* [springboot中如何优雅的对接口数据进行加密解密](https://blog.csdn.net/xinghui_liu/article/details/121208804)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot 接口加密解密,新姿势!](https://blog.csdn.net/qq_42914528/article/details/128168527)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值