【问题标题】:CORS - how to ignore authentication for OPTIONS preflight request in Apache's httpd.conf?CORS - 如何在 Apache 的 httpd.conf 中忽略 OPTIONS 预检请求的身份验证?
【发布时间】:2021-05-07 07:09:45
【问题描述】:

我是 CORS 的新手,并且了解到浏览器发送的 OPTIONS 预检请求不包括用户凭据。 如何让过滤器(在 httpd.conf 中)以不同方式响应 OPTIONS 请求,即绕过身份验证?

这是我目前的配置:

<LocationMatch /api>
SetEnvIfNoCase Origin "https://(www\.)?(domain1\.com|domain2\.com)(:\d+)?$" AccessControlAllowOrigin=$0
Header set Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header set Access-Control-Allow-Credentials "true"
Header set Access-Control-Allow-Methods "GET,POST,DELETE,OPTIONS"
Header set Access-Control-Allow-Headers "Accept, Authorization, Origin, Content-Type"
AuthFormProvider ldap
AuthLDAPURL "ldap://localhost:10889/ou=Users,dc=work,dc=com?uid"
AuthLDAPGroupAttribute member
AuthLDAPGroupAttributeIsDN on
Require valid-user
ErrorDocument 401 /login.html
ErrorDocument 500 /error.html
AuthType form
AuthName realm
Session On
SessionMaxAge 1800
SessionDBDCookieName session path=/
ProxyPass  http://localhost:8080 timeout=31536000
AuthFormFakeBasicAuth On
</LocationMatch>

以及发出请求的javascript:

$.ajax({
        type : "DELETE",
        url : "https://www.domain1.com/api",
        xhrFields: {
            withCredentials: true,
        },
        success : function(data){

        },
});

我已经尝试了以下方法,但没有运气:

(a)

RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L,E=HTTP_ORIGIN:%{HTTP:ORIGIN}]

(b)

<Limit OPTIONS>
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Credentials "false"
Header always set Access-Control-Allow-Headers "Accept, Authorization, Origin, Content-Type"
Header always set Access-Control-Allow-Methods "GET,POST,DELETE,OPTIONS,PUT"
</Limit>

(c)

<Limit OPTIONS>
Allow for all
</Limit>

(d)

SetEnvIfNoCase Request_Method OPTIONS allowed

有什么想法吗?请帮忙!

【问题讨论】:

  • 哪里首先需要身份验证的配置部分?
  • Allow for all 应改为 FROM,并且您必须将其与 SATISFY ANY 结合使用,以便 ALLOW 指令或 REQUIRE 申请OPTIONS 请求。
  • 谢谢,但它仍然返回 401 Unauthorized。

标签: ajax apache .htaccess cors


【解决方案1】:

我今天在this question 的帮助下解决了同样的问题。基本上你的选择c。

我的 conf 结构是:

conf/httpd.conf <- normal stuff   
conf.d/ssl.conf <- set up ssl stuff  
conf.d/api.conf <- set specific stuff to api like Auth  
/var/www/.htaccess <- set specific stuff to api again   

这允许限制除 OPTIONS 之外的所有内容

/conf.d/api.conf文件:

<Directory "/var/www/api">
  AllowOverride All
  Options FollowSymLinks

  <LimitExcept OPTIONS>
    Auth stuff here
    Mainly your Require statements
  </LimitExcept>
</Directory>

然后在我的.htaccess 文件中设置标题。

The Apache manual 在 require 指令中声明“以这种方式应用的访问控制对所有方法都有效。这是通常需要的。如果您希望仅对特定方法应用访问控制,而其他方法不受保护,然后将 Require 语句放入 &lt;Limit&gt; [或 &lt;LimitExcept&gt;] 部分。”

我必须确保我的应用程序可以处理 OPTIONS,因为此设置不会自动返回。 Herehere 可以看到如何重定向可能会起作用,而不是让应用程序中的某些东西处理它。

【讨论】:

    猜你喜欢
    • 2016-12-16
    • 2011-01-23
    • 2014-01-15
    • 2013-03-21
    • 2020-08-11
    • 2016-10-12
    • 2018-09-02
    相关资源
    最近更新 更多