Java Spring Boot 1.2升级中一致的URI/body解码破坏了我们返回的图像
我将我们的一个web服务从1.9更新到1.2.1,我们遇到了一个问题,图像不再按预期返回 它们的mime类型为Java Spring Boot 1.2升级中一致的URI/body解码破坏了我们返回的图像,java,spring,spring-mvc,spring-boot,Java,Spring,Spring Mvc,Spring Boot,我将我们的一个web服务从1.9更新到1.2.1,我们遇到了一个问题,图像不再按预期返回 它们的mime类型为text/html,响应很小,接近150字节。当然,浏览器会抱怨试图将文本解释为图像,而图像无法显示 奇怪的是,HttpServletResponse显然在控制器函数末尾的响应中包含真实数据 以下是控制器和函数,它通过一些更改将图像添加到响应中以隐藏公司标识: @RequestMapping(value = "/lot/{someId}/{anotherId}/{width}&
text/html
,响应很小,接近150字节。当然,浏览器会抱怨试图将文本解释为图像,而图像无法显示
奇怪的是,HttpServletResponse显然在控制器函数末尾的响应中包含真实数据
以下是控制器和函数,它通过一些更改将图像添加到响应中以隐藏公司标识:
@RequestMapping(value = "/lot/{someId}/{anotherId}/{width}", method = RequestMethod.GET)
public void getImage(@PathVariable Long someId, @PathVariable Integer anotherId, @PathVariable Integer width, HttpServletResponse response) throws IOException {
if(!validDimension(width)) { return; }
String key = someId+ "-" + anotherId+ "-" + width;
MyMap image = (MyMap) getFromCache(key);
if(image == null) {
image = someGetMapService.getMyMap(openHouseId, lotId);
MyMap processedImage = scaleMapImage(image, width);
if(processedImage != null) {
saveToCache(key, processedImage);
image = processedImage;//processedImage is valid so use that
}
}
processResponse(image.getMimeType(), image.getFile(), response);
}
processResponse代码:
private void processResponse(String mimeType, byte[] image, HttpServletResponse response) throws IOException {
ContentType contentType = ContentType.valueOfMimeType(mimeType, ContentType.findContentTypesByCategory(ContentTypeCategory.IMAGE));
//check for valid content type, and set it before streaming it out to avoid XSS vulnerabilities
//with things like svg - if not valid, don't stream out the image
if (contentType == null) {
LOG.log(Level.WARNING, MessageFormat.format("Unable to find matching content type for: {0}", mimeType));
response.flushBuffer();
return;
}
response.setContentType(contentType.getMimeType());
if (image != null) {
response.setContentLength(image.length);
response.getOutputStream().write(image);
}
response.flushBuffer();
}
设置时,所有问题都已解决:spring.http.encoding.enabled=false
这在1.2发行说明中有说明:
一致的HTTP URI/正文解码
CharacterEncodingFilter现在会自动注册,以进行一致的URI/正文解码。如果需要UTF-8以外的内容,则可以使用spring.http.encoding.charset属性;如果根本不希望注册CharacterEncodingFilter,则可以将spring.http.encoding.enabled设置为false
链接:
这是我的问题强>
为什么那个过滤器会阻挡我的图像