【发布时间】: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