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));
    }
}
相关推荐
1 9 J几秒前
Java 上机实践4(类与对象)
java·开发语言·算法
Code apprenticeship1 分钟前
Java面试题(2)
java·开发语言
J不A秃V头A4 分钟前
Python爬虫:获取国家货币编码、货币名称
开发语言·爬虫·python
憨子周1 小时前
2M的带宽怎么怎么设置tcp滑动窗口以及连接池
java·网络·网络协议·tcp/ip
霖雨3 小时前
使用Visual Studio Code 快速新建Net项目
java·ide·windows·vscode·编辑器
SRY122404193 小时前
javaSE面试题
java·开发语言·面试
Fiercezm3 小时前
JUC学习
java
无尽的大道3 小时前
Java 泛型详解:参数化类型的强大之处
java·开发语言
ZIM学编程3 小时前
Java基础Day-Sixteen
java·开发语言·windows
我不是星海3 小时前
1.集合体系补充(1)
java·数据结构