Ambassador-08-跨域
官方文档:https://www.getambassador.io/docs/latest/topics/using/cors/
Cross-Origin Resource Sharing-CORS
CORS的配置可以设置在ambassador的Module或者Mapping中,当CORS设置在Module或者Mapping中时,ambassador拦截OPTIONS请求,请响应CORS头。这意味着您将不需要在你的upstreams中实现任何逻辑来处理这些CORS选项请求。
设置cors属性
将以下域名设置到Access-Control-Allow-Origin头中,如果允许所有域名,可以设置成“*”
origins: - http://foo.example - http://bar.example
设置允许的方法到Access-Control-Allow-Methods
methods: - GET - POST - OPTIONS
可以设置数据格式headers: Content-Type,到Access-Control-Allow-Headers
headers:
- Content-Type
credentials 对应Access-Control-Allow-Credentials
exposed_headers 对应Access-Control-Expose-Headers
例子:
--- apiVersion: getambassador.io/v2 kind: Mapping metadata: name: cors spec: prefix: /cors/ service: cors-example cors: origins: http://foo.example,http://bar.example methods: POST, GET, OPTIONS headers: Content-Type credentials: true exposed_headers: X-Custom-Header max_age: "86400"
Spring Boot 2.0.1 中跨域
@EnableWebSecurity class SecurityConfig extends WebSecurityConfigurerAdapter { public void configure(final HttpSecurity http) throws Exception { http .cors().configurationSource(new PermissiveCorsConfigurationSource()).and() .csrf().disable() .authorizeRequests() .antMatchers("**").permitAll(); } private static class PermissiveCorsConfigurationSource implements CorsConfigurationSource { /** * Return a {@link CorsConfiguration} based on the incoming request. * * @param request * @return the associated {@link CorsConfiguration}, or {@code null} if none */ @Override public CorsConfiguration getCorsConfiguration(final HttpServletRequest request) { final CorsConfiguration configuration = new CorsConfiguration(); configuration.setAllowCredentials(true); configuration.setAllowedHeaders(Collections.singletonList("*")); configuration.setAllowedMethods(Collections.singletonList("*")); configuration.setAllowedOrigins(Collections.singletonList("*")); return configuration; } } }
不要小瞧女程序员
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具