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));
    }
}
相关推荐
Pacify_The_North几秒前
【C++进阶三】vector深度剖析(迭代器失效和深浅拷贝)
开发语言·c++·windows·visualstudio
一人の梅雨8 分钟前
化工网平台API接口开发实战:从接入到数据解析‌
java·开发语言·数据库
扫地的小何尚12 分钟前
NVIDIA工业设施数字孪生中的机器人模拟
android·java·c++·链表·语言模型·机器人·gpu
Zfox_15 分钟前
【C++项目】从零实现RPC框架「四」:业务层实现与项目使用
linux·开发语言·c++·rpc·项目
我想吃余18 分钟前
【C++篇】类与对象(上篇):从面向过程到面向对象的跨越
开发语言·c++
Niuguangshuo21 分钟前
Python设计模式:克隆模式
java·开发语言·python
双叶83628 分钟前
(C语言)单链表(1.0)(单链表教程)(数据结构,指针)
c语言·开发语言·数据结构·算法·游戏
想睡hhh30 分钟前
c++概念——入门基础概念
开发语言·c++
肖永威39 分钟前
python列表常用方法大全
开发语言·python
愚润求学44 分钟前
【C++】vector的模拟实现
开发语言·c++·stl·语法