Spring和HTTP Options请求 [英] Spring and HTTP Options request
问题描述
作为对此问题的跟进,我很想知道如何在spring 3 mvc应用程序中处理OPTIONS请求。
我不想为我的spring代码中的每个端点编写一个选项处理方法。但是建议将选项处理程序映射到/ **只适用于没有处理程序的终端....
As a follow up on this question, i am wondering how to handle OPTIONS request in a spring 3 mvc application. I dont want to write an option-handling-method for every endpoint in my spring code. But the proposed mapping of an options-handler to "/**" works only for endpoints which dont have a handler already....
所以我想到使用mvc拦截器拦截OPTIONS请求以处理跨站点访问的东西。但我无法想象这是最好的方法。有没有其他选项,如在同一路径上具有不同请求方法的多个处理程序?我的感觉告诉我这实际上应该有效..(但它没有)!?
So i thought about using mvc interceptors to intercept OPTIONS request to handle cross-site-access stuff. but i cannot imagine that this is the best way to do this. are there any other options such as multiple handlers with different request-methods on the same path? My feeling tells me that this should actually work..(but it doesn't)!?
推荐答案
它看起来像原生春天为春季4(可能)设置了对此的支持。
It looks like native Spring support for this is set for Spring 4 (Maybe).
但与此同时,我实施了以下内容:
However in the meantime I implemented the following:
使用Maven(或手动)获取此依赖性:
Using Maven (or manually) pull in this dependancy:
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>1.3.2</version>
</dependency>
这有一个捕获所有入站OPTIONS请求的实现。进入web.xml文件添加以下配置:
This has an implementation to capture all the inbound OPTIONS requests. Into the web.xml file add the following config:
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Content-Type,Accept,Origin</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我用/ **方法看到的问题是一个更具体的控制器实现会覆盖这个。
The problem I've seen with the /** approach is a more specific Controller implementation will override this.
这篇关于Spring和HTTP Options请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!