我使用backbone.js的模型.当我保存模型时,它会在firefox上向服务器端发送HTTP OPTIONS方法,但是使用safari发送HTTP POST方法.
我知道这不是关于backbone.js的问题,而是关于CORS.我将检查服务器端的方法,GET,POST,PUT和DELETE,我不会使用HTTP OPTIONS方法.
我要求的网址是我的api:api.foo.com和api要求:bar.com
那么,我如何控制所有浏览器请求我的api.foo.com使用HTTP POST而非OPTIONS?以及如何与来自任何其他域的所有请求共享api.foo.com的内容?
注意:我已经将响应的标头从服务器端更改为:Access-Control-Allow-Origin:*
我有CORS使用以下内容:
[System.Web.Http.HttpPut]
[System.Web.Http.AcceptVerbs("OPTIONS")]
[System.Web.Http.Route("api/exercise")]
public HttpResponseMessage UpdateExercise(Exercise exercise) {
try {
_adminService.UpdateExercise(exercise);
return Request.CreateResponse(HttpStatusCode.OK, "Success");
} catch (Exception e) {
return Request.CreateResponse(HttpStatusCode.InternalServerError, e);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的global.asax:
protected void Application_BeginRequest() {
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS") {
Response.Flush();
}
}
Run Code Online (Sandbox Code Playgroud)
但是有些奇怪的事情正在发生 - 如果我在控制器中设置断点,OPTIONS请求会通过null练习进入内部.为什么会这样?我希望Flush()能防止这种情况发生.
就目前而言,我需要为所有对CORS敏感的端点(PUT,DELETE)添加null检查.这看起来不太优雅......我应该能够阻止OPTIONS请求命中控制器逻辑,而只是直接响应所需的标题吗?
我正在拨打 CORS 电话。现在,每个 api 调用都有一个 OPTIONS 预检调用。是否可以缓存 OPTIONS 预检调用?
我看到该Cache-Control标头可用于缓存实际的 GET 响应。https://www.fastly.com/blog/caching-cors
但是如何缓存 OPTIONS 调用的响应?
HTTPOPTIONS请求是否适合确定用户的授权?
我见过 HTTPOPTIONS请求用于预检以检查请求是否有效,但是可以使用它来确定用户对特定资源的访问权限吗?
资源显示在页面上。有一个编辑表单用于PUT更新资源。如果请求显示OPTIONS /resource/1它接受PUT,则会显示编辑按钮。
请求是否OPTIONS适合返回特定用户有权访问的动词以确定授权/权限?
请求标头信息是否OPTIONS应该在前端代码中使用(或者只是预检验证)?
是否有任何标准用于通过 REST API 确定经过身份验证的用户的权限?
permissions rest authorization api-design http-options-method
我正在使用Sencha Touch 2.1.0.我正在进行HTTP GET调用.这是一个CORS请求.因此它正在按预期发送Pre-flight HTTP OPTIONS命令.
我在我的服务器上安装了CORS过滤器并进行了配置.来自我的代码的电话一直很顺利,直到昨天.突然间它今天停止加载数据.当我在Chrome中查看网络电话时,我发现OPTIONS方法显示为"已取消加载"
方法:选项
状态文本:"加载取消"
类型:待定
启动器:Connection.js:319
我第一次配置CORS过滤器时遇到了类似的问题.当我清除浏览器缓存时,它开始工作.这一次,我不确定它为什么突然停止工作.即使我在浏览器中清除缓存和历史记录,也无法修复.
如果我在Firefox中使用HTTPRequestor进行同样的调用,那么它的效果非常好.这是电话.由于保密原因,我屏蔽了网址.
OPTIONS http://myurl/rest/items?_dc=1358304888220&page=1&start=0&limit=25 HTTP/1.1
Access-Control-Request-Method: GET
Origin: http://localhost:8080
Access-Control-Request-Headers: accept, origin, x-requested-with
Run Code Online (Sandbox Code Playgroud)
同样的确切请求给了我一个非常好的HTTPRequestor响应.结果如下:
OPTIONS http://myurl/rest/items?_dc=1358304888220&page=1&start=0&limit=25
Access-Control-Request-Headers: accept, origin, x-requested-with
Access-Control-Request-Method: GET
Origin: http://localhost:8080
-- response --
200 OK
Date: Wed, 16 Jan 2013 03:19:27 GMT
Server: Apache-Coyote/1.1
Access-Control-Allow-Origin: http://localhost:8080
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: HEAD, GET, POST, OPTIONS
Access-Control-Allow-Headers: X-Requested-With, Origin, Accept, Content-Type
Content-Length: 0
Strict-Transport-Security: max-age=15768000, includeSubDomains
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Run Code Online (Sandbox Code Playgroud)
在商店中使用Sencha代码进行此调用:
proxy: {
type: 'ajax',
method: 'GET', …Run Code Online (Sandbox Code Playgroud) 当我RequestMapping在Spring MVC中配置我的s时,我想Allow在使用该OPTIONS方法时自动生成正确的头.
例如,使用此控制器:
@Controller
@RequestMapping("/test")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
ResponseEntity<String> getTest() {
return new ResponseEntity<>("test", HttpStatus.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我OPTIONS对该URL 发出请求,我会得到405,方法不允许.相反,我希望它能自动回复
Allow: GET, OPTIONS 和 204 - No content
我有一个想法添加拦截器,如下所示:
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new HandlerInterceptor() {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if("OPTIONS".equalsIgnoreCase(request.getMethod())){
response.setHeader("Allow", "GET, OPTIONS");
response.setStatus(204);
//TODO figure out the @Controller and what possible methods exist
return false;
}
return …Run Code Online (Sandbox Code Playgroud) 我试图在Spray服务器上实现CORS支持(版本1.1-20131011,其中已经支持cors头).
目前,服务器代码如下所示:
trait DefaultCORSDirectives { this: Directives =>
def defaultCORSHeaders = respondWithHeaders(
`Access-Control-Allow-Origin`(AllOrigins),
`Access-Control-Allow-Methods`(HttpMethods.GET, HttpMethods.POST, HttpMethods.OPTIONS, HttpMethods.DELETE,
HttpMethods.CONNECT, HttpMethods.DELETE, HttpMethods.HEAD, HttpMethods.PATCH, HttpMethods.PUT, HttpMethods.TRACE),
`Access-Control-Allow-Headers`("Origin, X-Requested-With, Content-Type, Accept, Accept-Encoding, Accept-Language, Host," +
" Referer, User-Agent, Overwrite, Destination, Depth, X-Token, X-File-Size, If-Modified-Since, X-File-Name, Cache-Control"),
`Access-Control-Allow-Credentials`(true),
`Access-Control-Max-Age`(3600)
)
}
Run Code Online (Sandbox Code Playgroud)
并像这样使用
defaultCORSHeaders {
options {
complete {
StatusCodes.OK
}
} ~
post {
path("path") {
//response
}
}
Run Code Online (Sandbox Code Playgroud)
使用curl时,POST和OPTIONS方法的响应都是预期的.但是,从浏览器,我得到Access不允许使用Access-Control-Allow-Origin(Chrome)或错误415不支持的媒体类型(Firefox),似乎根本不发送POST请求.
请求的jQuery代码如下:
$(document).ready(function () {
$.post(url,
{
'params': "params",
},
function (data) {
//handle response
} …Run Code Online (Sandbox Code Playgroud) 我的前端代码:
<form action="" onSubmit={this.search}>
<input type="search" ref={(input) => { this.searchInput = input; }}/>
<button type="submit">??</button>
</form>
// search method:
const baseUrl = 'http://localhost:8000/'; // where the Express server runs
search(e) {
e.preventDefault();
let keyword = this.searchInput.value;
if (keyword !== this.state.lastKeyword) {
this.setState({
lastKeyword: keyword
});
fetch(`${baseUrl}search`, {
method: 'POST',
// mode: 'no-cors',
headers: new Headers({
'Content-Type': 'application/json'
}),
// credentials: 'include',
body: JSON.stringify({keyword})
})
}
}
Run Code Online (Sandbox Code Playgroud)
和我的Express.js服务器代码:
app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS');
res.header('Access-Control-Allow-Headers', …Run Code Online (Sandbox Code Playgroud) 由于我不明白的原因,Safari 无法(间歇性但持续地)连接到我们 QA 基础设施上的 Rails 应用程序(静态前端和 API 后端)。(我将就此写另一个问题。)
目前,让我感到困惑的一件事是,Safari(一般来说)似乎从未显示其他浏览器(Chrome 和 Firefox)在其 XHR 网络列表(在开发人员工具中)中明确显示的选项(预检)请求。我只看到 POST 和 GET 请求等,从未看到任何 OPTION 请求。
Safari 是否制作了它们,只是不展示它们?Safari 似乎对我们的 DEMO 和 PROD 基础设施(其设置略有不同)表现良好,因此我很想假设这只是他们的开发人员工具的差异,而不是表明出现了问题。
safari cors http-options-method preflight safari-web-inspector
cors ×8
http ×4
preflight ×3
api-design ×1
cross-domain ×1
http-post ×1
java ×1
jquery ×1
mobile ×1
node.js ×1
permissions ×1
post ×1
rest ×1
safari ×1
scala ×1
spray ×1
spring ×1
spring-mvc ×1