Spring IE图像未加载X-Content-Type-Options:nosniff
简介 我有一个SpringMVC应用程序,我正在从控制器加载映像。为了安全起见,我在Spring应用程序中添加了Spring IE图像未加载X-Content-Type-Options:nosniff,spring,internet-explorer,Spring,Internet Explorer,简介 我有一个SpringMVC应用程序,我正在从控制器加载映像。为了安全起见,我在Spring应用程序中添加了X-Content-Type-Options:nosniff 通过在springConfig xml中设置以下内容 问题:在此之后,IE没有加载控制器响应的图像。我怀疑响应中没有设置内容类型。因为另一个站点正在响应X-Content-Type-Options:nosniff和内容类型:image/png工作正常 TRY1 我试图将控制器更改为设置内容类型。但事实并非如此 @Reques
X-Content-Type-Options:nosniff
通过在springConfig xml中设置以下内容
问题:在此之后,IE没有加载控制器响应的图像。我怀疑响应中没有设置内容类型。因为另一个站点正在响应X-Content-Type-Options:nosniff
和内容类型:image/png代码>工作正常
TRY1
我试图将控制器更改为设置内容类型。但事实并非如此
@RequestMapping(value = "/getUserImage" , produces = org.springframework.http.MediaType.IMAGE_PNG_VALUE)
public @ResponseBody
void getUserImage(
@RequestParam(value = "userId", required = false) int userId,
HttpServletRequest request, HttpServletResponse response) {
try {
//Get file and add it to response
IOUtils.copy(inputStream, response.getOutputStream());
response.getOutputStream().flush();
response.setContentType(org.springframework.http.MediaType.IMAGE_PNG_VALUE);
response.setHeader("Content-Type","image/png");
response.flushBuffer();
inputStream.close();
} catch (Exception e){
}
}
TRY2
我试图像在方法拦截器中一样添加响应头,但仍然没有成功
但在Chrome和Firefox中也有同样的功能。试试这个:
@RequestMapping(value = "/image/{personId}")
@ResponseBody
public HttpEntity<byte[]> getPhoto(@PathVariable int personId) {
Person person = this.personService.getPersonById(personId);
if (person != null && person.getProfileThumbnail() != null) {
try {
byte[] image;
try {
image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(msg + "/" + person.getUsername() + "/" + personId + ".png"));
} catch (FileNotFoundException e) {
image = org.apache.commons.io.FileUtils.readFileToByteArray(new File(defaultProfilePath));
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
headers.setContentLength(image.length);
return new HttpEntity<>(image, headers);
} catch (IOException ignored) {
}
}
}
@RequestMapping(value=“/image/{personId}”)
@应答器
公共HttpEntity getPhoto(@PathVariable int personId){
Person=this.personService.getPersonById(personId);
if(person!=null&&person.getProfileThumbnail()!=null){
试一试{
字节[]图像;
试一试{
image=org.apache.commons.io.FileUtils.readFileToByteArray(新文件(msg++“/”+person.getUsername()++“/”+personId++.png”);
}catch(filenotfounde异常){
image=org.apache.commons.io.FileUtils.readFileToByteArray(新文件(defaultProfilePath));
}
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.IMAGE\u PNG);
headers.setContentLength(image.length);
返回新的HttpEntity(图像、标题);
}捕获(忽略IOException){
}
}
}
我所做的基本工作是检查文件系统中是否有供用户使用的图像,如果没有,则加载默认图像。现在它可以在所有浏览器上运行,所以即使personid为0,我也会返回默认图像,还有其他原因,我并没有在这里发布 内容类型已设置,但IE仍不加载图像。谁造的这个?世界上最令人沮丧的网络浏览器。你正在返回void,你认为它会如何工作?或者这是你的尝试?不,博格,让我说清楚。它在铬和FF中工作。没有内容类型。我试着添加你的代码,你的代码正在设置内容类型,但是IE仍然没有加载图像,但是Chrome和FF正在加载。更改你的返回类型。先生,我已经按照你提到的更改了。我说,我试过你的密码。这是伟大的工作,即设置内容类型,但IE没有加载图像。如何在不更改返回类型的情况下使用您的代码。它将抛出编译错误。