可以在 HandlerInterceptorAdapter 中实现 postHandle 方法,在该方法中对返回结果进行加密操作。具体步骤如下:

  1. 创建 SM2 加密工具类,实现加密和解密方法。

  2. 在 postHandle 方法中获取返回结果,并对结果进行加密操作。

  3. 将加密后的结果设置到 ModelAndView 对象中,以便后续使用。

示例代码如下:

public class MyInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private SM2Utils sm2Utils;

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        if (modelAndView != null && modelAndView.getViewName() != null && !modelAndView.getViewName().startsWith('redirect')) {
            // 获取返回结果
            Object result = modelAndView.getModel().get('result');

            // 对结果进行加密操作
            String encryptedResult = sm2Utils.encrypt(result.toString());

            // 将加密后的结果设置到 ModelAndView 对象中
            modelAndView.addObject('encryptedResult', encryptedResult);
        }
    }
}

这样,在 Controller 中返回的结果将会被拦截器加密后返回给客户端。客户端可以使用相同的 SM2 加密工具类进行解密操作。

SpringBoot 使用 HandlerInterceptorAdapter 返回 SM2 加密数据

原文地址: https://www.cveoy.top/t/topic/n8s1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录