本帖最后由 wuqingvika 于 2023-4-21 11:42 编辑
大佬们 有没有想法?
我的场景:
将请求来的密文数据请求拦截其中的text/plain报文体 取出对应的密文 变成明文 再丢给controller处理。我这里是不是哪里写错了。。。jsonStr就是转换好的明文Str。
拦截器
这是Controller
controller
报错【org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing: public java.lang.String brick.gcgs.controller.ywbdapController.handleTextPlain(java.lang.String)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.readWithMessageConverters(RequestResponseBodyMethodProcessor.java:163)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:133)
at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:122)
at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:179)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:146)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
】
==================================已解决===========================================
后来改成了filter过滤器且 @Component
public class MyFilter extends OncePerRequestFilter
[Java] 纯文本查看 复制代码 @Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
HttpServletResponse httpServletResponse = (HttpServletResponse) response;
String contentType = httpServletRequest.getHeader("Content-Type");
if (contentType != null && contentType.contains("text/plain")) { // 只处理text/plain请求数据
IDCTextPlainServletRequestWrapper myRequestWrapper = new IDCTextPlainServletRequestWrapper(httpServletRequest);
// TODO: 在这里对请求数据进行修改、验证等操作
String requestBodyStr = myRequestWrapper.getRequestBodyAsString();
//String requestBody = IOUtils.toString(httpServletRequest.getInputStream(), StandardCharsets.UTF_8);
//FIXME --用私钥解密text/plain文本数据---->拿到json明文数据
String jsonStr = null;
try {
jsonStr = RSAUtils.decrypt(requestBodyStr, “你自己的密钥”);
} catch (Exception e) {
e.printStackTrace();
}
//放行:对第4步的Json明文直接丢到接口中(这样接口中不需要验证这块繁琐的操作)
myRequestWrapper.setRequestBody(jsonStr.getBytes(StandardCharsets.UTF_8));
httpServletRequest = myRequestWrapper; // 使用自定义的HttpServletRequestWrapper对象来替换原始请求对象
}
filterChain.doFilter(httpServletRequest, httpServletResponse);
}
再配置
[Java] 纯文本查看 复制代码 @Bean
public FilterRegistrationBean myFilter ()
{
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new MyFilter());
//String[] urlPatterns = {"/api/*"};
registration.addUrlPatterns("/sync/**");
registration.setName("myFilter");
registration.setOrder(FilterRegistrationBean.LOWEST_PRECEDENCE);
return registration;
} |