Debugging Tips: HttpServletRequest 中获取所有参数
起因
业务需要跟第三方厂商进行 OAuth 对接,在开发者平台有个授权页面,授权之后如果成功就会拿到 accessToken,问题就出在获取的时候失败了。
过程
获取 Token 报错,首先检查入参是否有问题,OAuth 工程是用的 spring security,记录日志用了 AOP 去实现的,但是切入有点靠后,记录的都是成功的完整参数,所以查看日志就没用了。
通过查看源码找到了原始入参在这里,先在本地构造个参数缺失请求,自己打印下 request 内容找找看,通过 IDEA 可以查看到内容。
package org.springframework.security.oauth2.provider.client;
public Authentication attemptAuthentication(HttpServletRequest request, HttpServletResponse response)
throws AuthenticationException, IOException, ServletException {
继续上生产环境把 Arthas 挂上,开始抓入参,启动 Arthas 用启动 JVM 同一个用户才可以。
获取 HttpServletRequest 所有参数用到的方法:
request.getParameterMap();
request.getParameterNames();
前置操作都准备好了,上生产抓参数命令:
watch org.springframework.security.oauth2.provider.client.ClientCredentialsTokenEndpointFilter attemptAuthentication 'params[0].getParameterMap()' -x 3
再次触发请求,抓到入参如下,发现是缺少 client_secret 参数,到这为止就清楚问题所在了。
总结
- IDEA 中 debug 查看 request 的 parameter 姿势
request->request->request->request->request->coyoteRequest->parameters->paramHashValues request->request->request->request->request->inputStream->ib->coyoteRequest->parameters->paramHashValues
- AOP 打印日志不能太靠后,这样导致失败请求都无法记录,无法快速重现定位问题。
- HttpServletRequest 中每个方法的注释需要去过一遍,留点记忆。