相关疑难解决方法(0)

Angular:当credentials标志为true时,不能在'Access-Control-Allow-Origin'标头中使用通配符'*'

我知道有很多关于CORS的问题,但它们似乎没有回答我的问题.

所以我有一个用Angular编写的客户端应用程序,用于创建移动应用程序(使用Apache Cordova).将从移动设备加载html文件和JavaScript文件.当我模拟这个并且我向REST API服务器发送请求时,我首先得到"No'Access-Control-Allow-Origin'标头出现在所请求的资源上.http://localhost:82因此不允许来源".所以我添加了标题("Access-Control-Allow-Origin:*"); 在我的PHP REST API服务器中.我无法指定特定域,因为请求将来自移动设备.

现在,当凭证标志为真时,我不能在'Access-Control-Allow-Origin'标题中使用"通配符'*'."

我终于找到了一个解决方案,但我不确定这样做是否安全.

在我的PHP REST API服务器中,我添加了这个:

if (isset($_SERVER['HTTP_ORIGIN'])) {
   header("Access-Control-Allow-Credentials: true");
   header("Access-Control-Allow-Origin: " . $_SERVER['HTTP_ORIGIN']);
   header("Access-Control-Allow-Headers: *, X-Requested-With, Content-Type");
   header("Access-Control-Allow-Methods: GET, POST, DELETE, PUT");
}
Run Code Online (Sandbox Code Playgroud)

请告知这种工作方式.如果它不安全或根本没有好处,请告诉我如何解决这个问题?

非常感谢!

rest cors cordova ionic-framework angular

17
推荐指数
1
解决办法
6万
查看次数

NodeJs Passport isAuthenticated()即使在登录后也返回false

我是Angular.js的新手并尝试为网站构建本地身份验证.我已经浏览了各种来源,单页应用程序中的身份验证非常有用.当我尝试在我的localhost中构建相同的代码时,我的代码进入循环.

app.post('/login',.....)在响应返回用户,但之后,尽管加载它是检查用户是否通过调用记录在管理页面 app.get('/loggedin',... ),并req.isAuthenticated()正在恢复false,即使登录后,它进入一个循环.我不明白为什么会这样,请帮助我.

服务器端代码

var express = require('express');
var http = require('http');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

//==================================================================
// Define the strategy to be used by PassportJS
passport.use(new LocalStrategy(
  function(username, password, done) {
    if (username === "admin" && password === "admin") // stupid example
      return done(null, {name: "admin"});

    return done(null, false, { message: 'Incorrect username.' });
  }
));

// Serialized and deserialized methods when …
Run Code Online (Sandbox Code Playgroud)

authentication node.js angularjs passport.js

17
推荐指数
2
解决办法
9373
查看次数

如何在 FastAPI 中启用 CORS?

我试图在这个非常基本的 FastAPI 示例中启用 CORS,但它似乎不起作用。

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware


app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=['*']
)

@app.get('/')
def read_main():
    return {'message': 'Hello World!'}
Run Code Online (Sandbox Code Playgroud)

这是我得到的回应:

curl -v http://127.0.0.1:8000
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.1
> Accept: */*
>
< HTTP/1.1 200 OK
< date: Fri, 08 Jan 2021 19:27:37 GMT
< server: uvicorn
< content-length: 26
< content-type: application/json
< …
Run Code Online (Sandbox Code Playgroud)

fastapi

10
推荐指数
2
解决办法
9996
查看次数

带域域的CORS cookie仅在使用jQuery AJAX的Firefox中设置

domain使用跨站点请求添加提交时,我无法设置cookie .我试图通过jquery ajax调用请求来实现这一点.

是否有可能让它在除Firefox之外的其他浏览器中工作?

一些请求标题:

Accept:application/json, text/javascript, */*; q=0.01
Content-Length:55
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:53862
Origin:http://localhost:54265
Referer:http://localhost:54265/
Run Code Online (Sandbox Code Playgroud)

响应标题:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:x-requested-with, origin, content-type, accept, Proxy-Connection
Access-Control-Allow-Methods:GET,POST,PUT,OPTIONS, DELETE
Access-Control-Allow-Origin:http://localhost:54265
Set-Cookie:Auth=l_hash=123456&user=xyzl&remember_me=false; expires=Fri, 18 Jan 2013 13:42:10 GMT; domain=localhost; path=/
Run Code Online (Sandbox Code Playgroud)

码:

$.ajax({
    type: "PUT",
    url: apiHost + "api/account/login/",
    data: $("#loginBarForm").serialize(),
    dataType: "json",
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    crossDomain: true,

    xhrFields: {
        withCredentials: true
    },
});
Run Code Online (Sandbox Code Playgroud)

在Firefox中一切都很好.Chrome未设置Cookie.仅当域字段被删除时,所有浏览器都在运行.我可以看到在下一个请求中(在设置cookie之后)cookie出现在标题中.响应设置cookie后的firefox请求示例(当响应有域字段时):

Cookie: Auth=l_hash=123456&user=xyz&remember_me=false
Run Code Online (Sandbox Code Playgroud)

cookies ajax jquery cors

9
推荐指数
2
解决办法
5993
查看次数

AngularJS/Restangular会话

我是否需要对会话/身份验证做一些特别的事情?我通过restangular post方法登录后,我的服务器无法看到我的会话.登录成功,但之后服务器上没有看到任何会话.如果我在正常的帖子外面登录,然后尝试请求页面,它工作正常.我还注意到chrome正在标题中发送以下内容:

Cookie:_rexbro_session=g2wAAAABaAJkAAxjdXJyZW50X3VzZXJhAmo=--E0687A67DBE48DA40B6957CBCF6E83FB0E612660
Run Code Online (Sandbox Code Playgroud)

这不是通过restangular请求发送的.

我还要提一下,当我通过restangular登录时,服务器正在响应以下标头:

set-cookie:_rexbro_session=g2wAAAACaAJkAAxjdXJyZW50X3VzZXJhAmgCZAAEdXNlcmgIZAASRWxpeGlyLlVzZXIuRW50aXR5ZAALRWxpeGlyLlVzZXJhAm0AAAASYXZvaWRpYW1AZ21haWwuY29tbQAAAA1KYXlzb24gQmFpbGV5bQAAADwkMmEkMTAkNHNsQVFFRGRDcXd1R3JNNVYwOXBTLkx3MHpVeElNLy5tbVM2eTVmc3BFa3NUbHhuTUtiMzJoB2QAFEVsaXhpci5FY3RvLkRhdGVUaW1lYgAAB91hBGEcYRFhD2EUaAdkABRFbGl4aXIuRWN0by5EYXRlVGltZWIAAAfdYQVhA2EVYR5hFmo=--D3F8D9A8355421B8571DB691ED5C082AD183B034; path=/; HttpOnly
Run Code Online (Sandbox Code Playgroud)

但我没有看到任何事情.

提前致谢.

编辑:我应该说我的服务器存储会话,api服务器,是与服务前端的服务器分开.

angularjs restangular

5
推荐指数
1
解决办法
2040
查看次数

vertx 应用程序中的 CORS 问题不起作用

我的 Vertx 服务器驻留在服务器 A 中,客户端驻留在服务器 B 中。当我尝试访问 vertx 服务器时,出现 CORS 错误。我添加了一些服务器端代码来处理 CORS 问题,但它不起作用。我们是否需要在客户端添加一些标头。我在这里错过了什么?谁能帮忙

顶点服务器端:

Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
router.route().handler(io.vertx.rxjava.ext.web.handler.CorsHandler.create("*")
.allowedMethod(io.vertx.core.http.HttpMethod.GET)
.allowedMethod(io.vertx.core.http.HttpMethod.POST)
.allowedMethod(io.vertx.core.http.HttpMethod.OPTIONS)
.allowedHeader("Access-Control-Request-Method")
.allowedHeader("Access-Control-Allow-Credentials")
.allowedHeader("Access-Control-Allow-Origin")
.allowedHeader("Access-Control-Allow-Headers")
.allowedHeader("Content-Type"));
Run Code Online (Sandbox Code Playgroud)

客户端实现:

    function(url, user) {
    eventBus = new EventBus(url);
    eventBus.onopen = function() {
            //Do Something
    }
 }
Run Code Online (Sandbox Code Playgroud)

更新:

我删除了标题中的 withCredential 属性。现在我的代码看起来像

if (ar.succeeded()) {
routingContext.response().setStatusCode(200).setStatusMessage("OK")
.putHeader("content-type", "application/json")
.putHeader("Access-Control-Allow-Origin", "*")
.putHeader("Access-Control-Allow-Methods","GET, POST, OPTIONS")
.end(
Json.encodePrettily(ar.result().body())
//(String) ar.result().body()
);
routingContext.response().close(); 
Run Code Online (Sandbox Code Playgroud)

但仍然弹出以下错误。你能帮我吗?

XMLHttpRequest cannot load https://192.168.1.20:7070/Notify/571/rn4nh0r4/xhr_send?t=1471592391921. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header …
Run Code Online (Sandbox Code Playgroud)

javascript java cors vert.x

5
推荐指数
3
解决办法
1万
查看次数

如何在asp.net core 3.1中为每种类型的请求启用Cors

如何为asp.net core 3.1中的每种类型的请求启用Cors?

我正在关注MS Docs,但它们似乎不起作用。

注意:我可以为特定域实现 cors,但不能为每个域实现。例如,我已将以下配置应用于我的项目:

  1. ConfigureServices();我添加的方法中:

    services.AddCors();

  2. Configure()我添加的方法中:

    app.UseCors(builder => { builder .WithOrigins() .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); });

但是当我尝试从另一个 url 使用 jQuery 访问 API 时,我得到:

从源“ https://localhost:44361 ”访问“ https://localhost: 44314/Reservation”处的 XMLHttpRequest已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:无“访问控制” -Allow-Origin'标头存在于请求的资源上。

现在,如果我将 URL 放入 WithOrigins() 方法中,我就可以访问 API:

app.UseCors(builder =>
{
    builder
    .WithOrigins("https://localhost:44361")
    .AllowAnyMethod()
    .AllowAnyHeader()
    .AllowCredentials();
});
Run Code Online (Sandbox Code Playgroud)

我的问题是如何不受限制地向每个 URL(即域、子域等)授予对 API 的访问权限。应用 * 不起作用。

请帮忙。

c# cors asp.net-core asp.net-core-3.0 asp.net-core-3.1

5
推荐指数
1
解决办法
2万
查看次数

CORS:简单请求的目的是什么?

从 Mozilla 的文档来看,CORS 的场景有以下三种:

  1. 简单的请求
  2. 预检请求
  3. 带有凭据的请求

Simple Requests有一些缺点,例如,当客户端声明时withCredentials,即使服务器拒绝,带有cookie的HTTP请求已经发送,这可能是一种攻击。

Preflighted request安全,可以覆盖各种场景。Simple Requests即使预检请求可以满足所有要求,为什么人们还要发明?

参考

  1. Access-Control-Allow-Credentials 标头到底有什么作用?

http cors

4
推荐指数
1
解决办法
938
查看次数

ASP .NET Core使用多个CORS策略

我正在尝试设置2个CORS策略.一个作为api默认值,另一个Controllers作为我需要它们使用.我想这样做的原因是因为我有一个端点,它接收带有电子邮件信息的对象并发送电子邮件(与我的网页上的"联系我"框一起使用)并让它只接受来自我的域的请求.

我的startup.cs文件片段:

public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy("Example",
                    builder => builder.WithOrigins("http://www.example.com"));
                options.AddPolicy("AllowAll",
                    builder => builder.AllowAnyOrigin());
            });

            services.AddMvc();
            //other configure stuff
        }


 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        app.UseCors(builder =>
        {
            builder.AllowAnyHeader();
            builder.AllowAnyMethod();
            builder.WithOrigins("AllowAll");
        });

        app.UseMvcWithDefaultRoute();
    }
Run Code Online (Sandbox Code Playgroud)

我的emailcontroller.cs档案:

using System.Threading.Tasks;
using MyAPI.Models;
using MyAPI.Services;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;

namespace MyAPI.Controllers
{
    [Produces("application/json")]
    [Route("api/Email")]
    [EnableCors("Example")]
    public class EmailController : Controller
    {
        private readonly IEmailSender _emailSender;

        public EmailController(IEmailSender emailSender)
        {
            _emailSender …
Run Code Online (Sandbox Code Playgroud)

.net javascript c# cors asp.net-core

3
推荐指数
2
解决办法
3168
查看次数

Laravel cors.php 文件中的supports_credential 是什么意思?

在 config/cors.php 文件中我有键值对

'supports_credentials' => false,

“supports_credentials”对于 true 有何作用,对于 false 有何作用?

laravel

3
推荐指数
1
解决办法
3039
查看次数

Azure Functions Core 工具 - 无法访问已处置的对象。对象名称:“IServiceProvider”

当我尝试在本地环境中运行 Azure 函数时,出现以下错误:

在此输入图像描述

如果我尝试使用 v2,我会得到同样的错误。

我已经检查了所有这些问题:

Azure/azure-functions-host#4521 Azure/azure-functions-host#4520

另外,我尝试重新安装 Visual Studio 并更新到最新版本的 Azure Function Core Tools。

如果我通过 Visual Studio 2019 或使用 func host start 从命令行运行函数应用,则会出现此问题。我也尝试过设置不同的端口号。

有人有解决这个问题的方法吗?

更新

我发现 VS 启动 x64 版本,它可能会导致问题。当我下载 func-cli-3.0.2358-x86.msi 并为项目设置调试时,它可以工作。

如何设置 VS 运行 x86 cli?

azure-functions azure-functions-core-tools

1
推荐指数
1
解决办法
2401
查看次数