zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
  • 相关阅读:
    关于两次指针(struct型)传参数的问题
    git学习基础教程
    程序员恶性循环- 有感
    基于Tomcat 的WEB Project存在的安全漏洞总结
    使用Maven自动部署Java Web项目到Tomcat问题小记
    MyEclipse中Maven的配置
    mybatis处理集合、循环、数组和in查询等语句的使用
    JBOSS的启动和停止
    myeclipse越来越卡了怎么回事啊?
    linux shell 模拟post请求
  • 原文地址:https://www.cnblogs.com/shix0909/p/12960854.html
Copyright © 2011-2022 走看看