【发布时间】:2019-11-09 09:19:54
【问题描述】:
我有以下架构。
我有一个处理身份验证、授权和路由的 spring 云网关服务器。
我们可以shell叫他服务器A。
另一个服务器是身份验证服务器B,它使用 Kerberos 对用户进行身份验证。要使用它,A 将A/a/route 形式的请求重定向到B/a/route 形式的请求,状态为307。
然后服务器 B 对用户进行身份验证并使用 JWT 添加 cookie,并以重定向 A/a/route 响应状态为 307。
当服务器 A 使用 JWT cookie 获取重定向请求时,他将其代理到 API 服务器。
在这一切过程中,我们得到以下错误
无法加载
A/a/route:从A/a/route重定向到B/a/route已被 CORS 策略阻止:“Access-Control-Allow-Origin”标头包含多个值“*、http://localhost:4200”,但仅一个是允许的。 Origin 'http://localhost:4200' 因此不允许访问。
我们将所有Access-Control-Allow-* 标头设置为允许A 和B 两台服务器上的所有请求,方式如下
在spring网关服务器中
@Bean
public WebFilter corsFilter() {
return (ServerWebExchange exchange, WebFilterChain chain) -> {
ServerHttpRequest request = exchange.getRequest();
if (CorsUtils.isCorsRequest(request)) {
ServerHttpResponse response = exchange.getResponse();
HttpHeaders headers = response.getHeaders();
headers.add("Access-Control-Allow-Method", "POST, GET, OPTIONS, PUT");
headers.add("Access-Control-Allow-Origin", "*");
headers.add("Access-Control-Allow-Headers", /* HEADRS */);
headers.add("Access-Control-Allow-Credentials", "true");
if (HttpMethod.OPTIONS.equals(request.getMethod())) {
response.setStatusCode(HttpStatus.OK);
return Mono.empty();
}
}
return chain.filter(exchange);
}
}
在 apache-httpd Kerberos 服务器中
<VirtualHost *>
LuaRoot /etc/httpd/lua
LuaHookFixups authz.lua check_authz_cookie
ProxyPreserveHost On
SetEnvIf Origin "(.*)" origin=$0
Header always set Access-Control-Allow-Origin "%{origin}e"
Header always set Access-Control-Allow-Method "POST, GET, OPTIONS, PUT"
Header always set Access-Control-Allow-Credentials "true"
Header always set Access-Control-Allow-Headers # HEADERS...
RewriteEngine On
RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]
ProxyPass /whoami !
ProxyPass / A
ProxyPassReverse / A
</VirtualHost>
我们希望重定向能够正常工作,但 CORS 问题发生了。
【问题讨论】:
标签: spring apache cors spring-cloud