首页 > 编程语言 >springboot aspect 对请求参数,返回数据加密

springboot aspect 对请求参数,返回数据加密

时间:2023-05-25 17:47:11浏览次数:55  
标签:返回 obj springboot Object aspect 加密 response String

 

     对客户端<--->服务端传输数据加密。

    上送文件流不加密,其他信息加密。   返回信息加密。

  切面的方式进行解密,不修改业务逻辑。

     找到对应的方法,对应的参数,进行解密。

  @Around("@annotation(com.jiayingsoft.scip.annotation.ScipSecureityMethodAtn)")
    public Object run(ProceedingJoinPoint pjp) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) (pjp.getSignature());
        Method method = methodSignature.getMethod();
        String secret = fetchAppsecret();

        Map<Parameter, Object> map = getParameterAndValue(pjp);
        for (Iterator<Map.Entry<Parameter, Object>> iterator = map.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry<Parameter, Object> row = iterator.next();
            if (row.getKey().isAnnotationPresent(ScipSecurityParameterAtn.class)) {
                Object obj = row.getValue();
                if (obj != null && obj.toString().trim() != "") {
                    String desStr = AESUtil.decrypt(obj.toString(), secret);
                    row.setValue(desStr);
                }
            }
        }
        Object obj = pjp.proceed(map.values().toArray());
//        if (obj instanceof String) {
//            return AESUtil.encrypt(obj.toString(), secret);
//        }
//        if (obj instanceof R) {
//            R r = ((R<?>) obj);
//            if (r.getData() != null) {
//                r.setData(AESUtil.encrypt(JSON.toJSONString(r.getData()), secret));
//            }
//        }
        return obj;

    }

在加密返回信息时,碰到问题。

加密后,信息是字符串,但是在controller层面不愿意写死返回类型,如果controller 返回返回的不是字符串,那么springboot 按照返回类型序列化,由于这里我把返回类型改成字符串,故springboot 序列化失败。

解决方法
1:controller--function 返回String 类型(写的太死了,拓展性不好)

2:核心数据加密,外面包一层,这样springboot 序列化时不会报错,但是前端解密稍复杂。

bing 上搜索后,发现可以继承 ResponseBodyAdvice 用 ControllerAdvice 在controller层做切面,在 beforeBodyWrite() 方法上做加密,当然controller 必须是@ResponseBody

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
String originString = JSON.toJSONString(body);
        String encryptBase64String = AESUtil.encrypt(originString, ScipSecureitAspect.fetchAppsecret());
return encryptBase64String;
    }

如上返回

前端解密失败,后发现返回的字符串  多了引号,服务端显示返回长度是100字符,前端收到102字符

解决方法:

 1:前端处理,先去掉引号,再解密 (太奇怪,为什么要这么做)

2 :覆盖MappingJackson2HttpMessageConverter  的 withInternal 方法 (可能会影响其他业务)、

后面stackoverflow.com发现这个方法,成功解决问题   链接

  @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
                                  Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
        String originString = JSON.toJSONString(body);
        String encryptBase64String = AESUtil.encrypt(originString, ScipSecureitAspect.fetchAppsecret());
        response.getHeaders().set("content-type", "text/plain;charset=UTF-8");
        try (OutputStream stream = response.getBody()) {
            stream.write(encryptBase64String.getBytes("utf-8"));
            stream.flush();
        } catch (IOException e) {
            log.error("failed to send message:"+e.getMessage());
            e.printStackTrace();
        }


        return null;

    }

把流直接写入response。

 

标签:返回,obj,springboot,Object,aspect,加密,response,String
From: https://www.cnblogs.com/come-here/p/17432282.html

相关文章

  • SpringBoot结合easyexcel处理Excel文件
    文/朱季谦假如有这样一个需求,每天需要读取以下表头的Excel文件,统计文件里击中黑名单的比例,该文件is_blacklist列的1表示击中了黑名单,0表示未击中黑名单。基于该需求,可以在定时任务通过easyexcel工具进行处理。一、首先需要在SpringBoot引入easyexcel的maven依赖<dependency>......
  • springboot 跳转到网页上的两种实现方式(转发与重定向详细对比)
    1.情景展示虽然现在流行的是前后端分离,后端开发与前端往往只进行数据交互,不需要参与对网页跳转的控制及网页内容的开发。但是,由服务器(后端)跳转到客户端(浏览器)或者从A服务器跳到B服务器是一项基本的能力。在项目开发中,真正遇到的时候,该如何实现?哪种实现方式更好?2.具体分析......
  • 仿射变换加密
    根据公式c=Ea,b(m) ☰a*m+b(mod26);如果已知a,b,加密非常简单,代码如下:#include<bits/stdc++.h>usingnamespacestd;inta,b;voidInput(){intp,val;charkey;charkey_2[1010];cout<<"请输入a,b的值(中间以空格分开)"<<endl;......
  • SpringBoot中使用@Scheduled实现定时任务通过读取配置文件动态开关
    场景SpringBoot中定时任务与异步定时任务的实现:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/117083609上面讲的通过@Scheduled注解实现简单定时任务的方式。如果定时任务有多个,不同业务场景下需要动态配置某个定时任务的开关。可以通过@ConditionalOnPropert......
  • springboot入门
    1. 介绍  7SpringBoot是Spring中的一个成员, 可以简化Spring,SpringMVC的使用。 他的核心还是IOC容器。1.1 特点:  7Create stand-alone Spring applications创建spring应用Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)内嵌的tomca......
  • mysql数学和日期和加密函数
    1. 数学相关函数  762rand()返回一个随机浮点值v,范围在0到1之间(即其范围为0≤v≤1.0)。若已指定一个整数参数N.则它被用作种子值,用来产生重复序列。1.1 练习代码在E:\java学习\初级\course156\db_math#演示数学相关函数762--ABS(num)绝对值SELECTABS(-10)FROMDUAL;#......
  • Springboot @Value注解
    配置文件test:name:123list:1,2,3aa:userInfoServiceImpl.merChantNoController:@Value("${test.name}")publicStringname1;//输出123@Value("#{'${test.list}'}")publicList<Object>list;//输出[1,2,3]......
  • SpringBoot 出现 Content type ‘application/x-www-form-urlencoded;charset=UTF-8‘
    问题点1:如果Content-Type设置为“application/x-www-form-urlencoded;charset=UTF-8”无论是POST请求还是GET请求都是可以通过这种方式成功获取参数,但是如果前端POST请求中的body是Json对象的话,会报上述错误。请求中传JSON时设置的Content-Type如果是application/json或者tex......
  • Mybatis中,SpringMVC中,SpringBoot项目中,出现There is no getter for property named 'x
    现象:Thereisnogetterforpropertynamed'xxxxxx'报错原因:其实说起原因有很多种,百度上都有很详细的说明,其中最重要也是经常发生的就是mapper.xml与是对应的实体类匹配不上导致错误发生,而我报错的原因是从mapper接口中向xml传入参数的时候,传入的是实体类对象,只有这一个参数,而在......
  • 【SpringBoot】SpringBoot常用注解
    一、前言首先这里说的SpringBoot常用注解是指在我们开发项目过程中,我们经常使用的注解,包含Spring、SpringBoot、SpringCloud、SpringMVC等这些框架中的注解,而不仅仅是SpringBoot中的注解。这里只是作一个注解列举,每个注解具体如何使用可以自行搜索查询哈。二、配置启动相关注解2.1......