eugenp / tutorials

Just Announced - "Learn Spring Security OAuth":

Home Page:http://bit.ly/github-lsso

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ISSUE] Reading HttpServletRequest Multiple Times in Spring Additional explanation of the limited situation needed.

IMWoo94 opened this issue · comments

Article and Module Links
Reading HttpServletRequest Multiple Times in Spring
CachedBodyHttpServletRequest

Describe the Issue
It creates a reusable request, but there is a problem if the data is provided in FORM format in the request body.
No parameters are defined for data in FORM format.

To Reproduce
Steps to reproduce the behavior:

  1. The first follow the guide for configuration. Reading HttpServletRequest Multiple Times in Spring
  2. Send an HTTP request. - contentType ( application/x-www-form-urlencoded )
  3. Check request parameters.

Expected Behavior
The "cachedBody" will contain the content of the HTTP request body.
However, information about the request parameters cannot be provided.

Because, in the process of creating a newly defined request, we call "Request getInputStream()" of the request.
This invocation alters the value of the "usingInputStream" field.
When calling the method to request information about the parameters, it proceeds to parse the parameters through the parseParameters() method.
The parseParameters() method determines whether to execute the parsing task based on the "usingInputStream" field.
As a result, a situation arises where the values for the parameters are not defined because they have already been read.

  1. Mentioning in the guide that normal operation may not occur in the case of FORM data format could be helpful.
  2. Adding code like " ContentCachingRequestWrapper " provided by Spring would be beneficial, as it allows handling of parameters for FORM data format separately.
  • The ContentCachingRequestWrapper class provided by Spring also invokes "super.getParameterMap()" to trigger the execution of the "parseParameters()" method.

The " fix CachedBodyHttpServletRequest " image below illustrates a simple approach where, upon receiving data in FORM format, the body is read to directly generate parameters.
Subsequently, when requesting parameters, both the existing request parameters and the generated FORM parameters are provided together.

I'm learning a lot and finding the tutorials provided very useful.
Updating the guide document could be beneficial and helpful to many people.

Screenshots

  • Custom Request Constructor
image
  • Request getInputStream() and usingInputStream
스크린샷 2024-04-24 오후 9 23 32
  • Request parseParameters()
image
  • ContentCachingRequestWrapper getParameters
image
  • fix CachedBodyHttpServletRequest
image image

Environment (please complete the following information):

  • OS: [Mac m2]
  • Develop Env
    • IntelliJ IDE
    • Spring boot 3.2.5
    • Spring Web