junit mockito 拦截器

拦截器代码

java 复制代码
/**
 * @Title:出参ASE加密拦截器
 * @Description: 指定的url拦截,指定返回参数加密
 * @Author: luohaihua
 * @Date: 2024/06/18 16:37
 * @Version: 1.0
 */
@Slf4j
public class ResponseAseEncryptInterceptor  extends HandlerInterceptorAdapter  {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return  true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
        //该参数由客户随机生产再通过rsa公钥加密
        String pwdkey= httpServletRequest.getParameter("pwdkey");
        if (!validateResult(salt,o)){
            return;
        }
        String aseKey = RsaUtils.decrypt(pwdkey);
        encryptResult(aseKey, ((JsonResponse) o).getInfo());
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}

mock代码

java 复制代码
public class TestResponseAseEncryptInterceptor extends MockBaseTest {
    @InjectMocks
    private ResponseAseEncryptInterceptor interceptor;

    @Mock
    private HttpServletRequest httpServletRequest;

    @Mock
    private HttpServletResponse httpServletResponse;;
    @Mock
    private ModelAndView modelAndView;;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
    }
    @Test
    public void testPostHandleWithPwdkey_result_success()  throws Exception{
        String pwdkey= "123321";
        JsonResponse jsonResponse = JsonResponse.success();
        CustInfoVo vo  = new CustInfoVo();
        vo.setName("张三");
        String rsaPwd = RsaEncryptUtil.encryptByPublicKey(pwdkey);
        jsonResponse.addInfo(vo);
        when(httpServletRequest.getParameter("pwdkey")).thenReturn(rsaPwd);
        interceptor.postHandle(httpServletRequest,httpServletResponse,jsonResponse,modelAndView);
        String resultName = AESUtils.encrypt("张三",pwdkey);
        Assert.assertEquals(resultName,vo.getName());
        Assert.assertEquals("张三",AESUtils.decrypt(vo.getName(),pwdkey));
    }
}
相关推荐
等一场春雨7 分钟前
Java设计模式 八 适配器模式 (Adapter Pattern)
java·设计模式·适配器模式
肖田变强不变秃25 分钟前
C++实现有限元计算 矩阵装配Assembly类
开发语言·c++·矩阵·有限元·ansys
一弓虽29 分钟前
java基础学习——jdbc基础知识详细介绍
java·学习·jdbc·连接池
王磊鑫29 分钟前
Java入门笔记(1)
java·开发语言·笔记
喜欢猪猪37 分钟前
分布式与微服务:构建现代应用的关键架构
开发语言·php
硬件人某某某1 小时前
Java基于SSM框架的社区团购系统小程序设计与实现(附源码,文档,部署)
java·开发语言·社区团购小程序·团购小程序·java社区团购小程序
程序员徐师兄1 小时前
Java 基于 SpringBoot 的校园外卖点餐平台微信小程序(附源码,部署,文档)
java·spring boot·微信小程序·校园外卖点餐·外卖点餐小程序·校园外卖点餐小程序
kucupung1 小时前
【C++基础】多线程并发场景下的同步方法
开发语言·c++
chengpei1471 小时前
chrome游览器JSON Formatter插件无效问题排查,FastJsonHttpMessageConverter导致Content-Type返回不正确
java·前端·chrome·spring boot·json
Quantum&Coder1 小时前
Objective-C语言的计算机基础
开发语言·后端·golang