【发布时间】:2017-07-01 06:48:26
【问题描述】:
我在使用 Spring mvc 下载名称中包含空格的文件时遇到问题。 我要做的是使用 Spring MVC 内置的资源解析器从远程主机下载文件。
我将 Spring 安全性与 Spring MVC 一起使用,但用于测试而不对特定模式应用任何安全性
Spring 安全配置:
<http>
....
<intercept-url pattern="/test download/**" access="permitAll" requires-channel="https" />
...
</http>
然后请求到达配置资源解析器的 MVC 调度程序:
Spring MVC 配置:
<mvc:resources mapping="/test download/**" location="http://localhost:10089/test/" />
该配置适用于名称中没有空格的文件。在 Spring 模块上启用调试日志我看到两个请求(文件名中有或没有空格)都以相同的方式处理。 下面是解决两个调用时 Spring 调度程序 servlet 的日志。
第一个测试路径:/test download/file.txt
... after security filters ..
DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/portal/test%20download/file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/file.txt]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/test download/file.txt] are [/test download/**]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/test download/file.txt] are {}
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/test download/file.txt] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [http://localhost:10089/test/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@2a6eb843]]] and 1 interceptor
DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/portal/test%20download/file.txt] is: -1
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
结果:
Content of the file is shown in the browser as expected第二个测试路径:/test download/test file.txt
... after security filters ..
DEBUG org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 'mvc-dispatcher' processing GET request for [/portal/test%20download/test%20file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/test file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/test file.txt]
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /test download/test file.txt
DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/test download/test file.txt]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/test download/test file.txt] are [/test download/**]
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/test download/test file.txt] are {}
DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/test download/test file.txt] to HandlerExecutionChain with handler [ResourceHttpRequestHandler [locations=[URL [http://localhost:10089/test/]], resolvers=[org.springframework.web.servlet.resource.PathResourceResolver@2a6eb843]]] and 1 interceptor
DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/portal/test%20download/test%20file.txt] is: -1
DEBUG org.springframework.security.web.context.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
DEBUG org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'mvc-dispatcher': assuming HandlerAdapter completed request handling
DEBUG org.springframework.web.servlet.DispatcherServlet - Successfully completed request
DEBUG org.springframework.security.web.access.ExceptionTranslationFilter - Chain processed normally
DEBUG org.springframework.security.web.context.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
结果:
Http 404 is returned
这两个文件在主机的同一个文件夹中,我使用本地服务器测试文件和远程服务器,结果相同。
我试图仅使用 Spring 配置来解决我的需求,因为我需要的基本上类似于代理,但应用了安全规则。
Spring MVC 版本:4.1.6.RELEASE
Spring Security 版本:3.2.5.RELEASE
知道为什么会这样吗?
更新
在 Frederik 提示后,我将用于测试的 tomcat 配置为远程主机来管理 utf-8,我发现当它处理来自 Spring 的请求时,它返回 505。
从浏览器到远程主机的简单调用:
"GET /test/test%20file.txt HTTP/1.1" 200 15
通过 Spring 收到的电话:
“HEAD /test/test file.txt HTTP/1.1”505 -
在配置连接器之前,我确实尝试创建名称中包含 %20 的文件,但它没有工作,所以可能在编码工作之前。
【问题讨论】:
标签: spring spring-mvc