Java ServletRequest Stream Closed Exception: Handling MultipartHttpServletRequest in Shiro AccessControlFilter
This exception occurs when you intercept the body content of a MultipartHttpServletRequest in a Shiro AccessControlFilter and then the character stream is closed. Typically, after intercepting the request body content in a filter, you need to create a new ServletRequest object and copy the contents of the original ServletRequest to the new one.
Here is an example code demonstrating how to correctly handle MultipartHttpServletRequest within a Shiro AccessControlFilter:
public class MyAccessControlFilter extends AccessControlFilter {
@Override
protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
// Get the original HttpServletRequest object
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
// Check if the request is a Multipart request
if (ServletFileUpload.isMultipartContent(httpServletRequest)) {
// Create a new MultipartHttpServletRequest object
MultipartHttpServletRequest multipartRequest = createMultipartHttpServletRequest(httpServletRequest);
// Copy the contents of the original ServletRequest object to the new MultipartHttpServletRequest object
copyRequestParameters(httpServletRequest, multipartRequest);
// Set the new MultipartHttpServletRequest object to Shiro's Subject
Subject subject = SecurityUtils.getSubject();
subject.getSession().setAttribute(DefaultWebSubjectContext.NATIVE_REQUEST, multipartRequest);
}
// Continue executing Shiro's filter chain
return true;
}
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
// Continue executing Shiro's filter chain
return true;
}
// Create a new MultipartHttpServletRequest object
private MultipartHttpServletRequest createMultipartHttpServletRequest(HttpServletRequest httpServletRequest) throws IOException {
MultipartHttpServletRequest multipartRequest = new DefaultMultipartHttpServletRequest(httpServletRequest);
return multipartRequest;
}
// Copy the contents of the original ServletRequest object to the new MultipartHttpServletRequest object
private void copyRequestParameters(HttpServletRequest httpServletRequest, MultipartHttpServletRequest multipartRequest) throws IOException {
// Get the original request parameter map
Map<String, String[]> parameterMap = httpServletRequest.getParameterMap();
// Copy the contents of the original request parameter map to the new MultipartHttpServletRequest object
for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
String name = entry.getKey();
String[] values = entry.getValue();
multipartRequest.getParameterMap().put(name, values);
}
// Copy the original request body content to the new MultipartHttpServletRequest object
ServletInputStream inputStream = httpServletRequest.getInputStream();
ServletOutputStream outputStream = multipartRequest.getOutputStream();
IOUtils.copy(inputStream, outputStream);
inputStream.close();
outputStream.close();
}
}
By using this code, you can correctly handle MultipartHttpServletRequest within your Shiro AccessControlFilter, thus preventing the 'Stream closed' exception.
AI 优化版
原文地址:https://www.cveoy.top/t/topic/qFjE 著作权归作者所有。请勿转载和采集!