拦截器代码
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));
}
}