generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Spring's request wrapper does not support reading the input stream multiple times with `getInputStream()` or `getReader()`, hence we need a custom wrapper to cache to body.
- Loading branch information
1 parent
ba2e764
commit 3136362
Showing
4 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
src/main/java/de/bund/digitalservice/a2j/config/SecurityConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/de/bund/digitalservice/a2j/security/CachedBodyServletInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package de.bund.digitalservice.a2j.security; | ||
|
||
import jakarta.servlet.ReadListener; | ||
import jakarta.servlet.ServletInputStream; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
public class CachedBodyServletInputStream extends ServletInputStream { | ||
|
||
private final InputStream cachedBodyInputStream; | ||
|
||
public CachedBodyServletInputStream(byte[] cachedBody) { | ||
this.cachedBodyInputStream = new ByteArrayInputStream(cachedBody); | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
return cachedBodyInputStream.read(); | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
try { | ||
return cachedBodyInputStream.available() == 0; | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void setReadListener(ReadListener readListener) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/de/bund/digitalservice/a2j/security/CachedBodyServletRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package de.bund.digitalservice.a2j.security; | ||
|
||
import jakarta.servlet.ServletInputStream; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import org.springframework.util.StreamUtils; | ||
|
||
public class CachedBodyServletRequest extends HttpServletRequestWrapper { | ||
|
||
private byte[] cachedBody; | ||
|
||
public CachedBodyServletRequest(HttpServletRequest request) throws IOException { | ||
super(request); | ||
InputStream requestInputStream = request.getInputStream(); | ||
this.cachedBody = StreamUtils.copyToByteArray(requestInputStream); | ||
} | ||
|
||
@Override | ||
public ServletInputStream getInputStream() throws IOException { | ||
return new CachedBodyServletInputStream(this.cachedBody); | ||
} | ||
|
||
@Override | ||
public BufferedReader getReader() throws IOException { | ||
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(this.cachedBody); | ||
return new BufferedReader(new InputStreamReader(byteArrayInputStream)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters