renren-fast 跨域限制-同源策略

 

http://localhost:88/api/captcha.jpg?uuid=bce06470-b6f4-4a36-8276-053aa883af51

88默认发给网关,api前缀满足

- id: admin_route
  uri: lb://renren-fast
  predicates:
    - Path=/api/**

会转到renren-fast, 先去注册中心找到renren-fast的地址http://renren-fast:8080/api/captcha.jpg

 默认是http://renren-fast:8080/renren-fast/captcha.jpg

使用网关带的路径重写

filters:
  - RewritePath=/api/(?<segment>.*),/renren-fast/$\{segment}

CORS策略:是指协议、域名、端口都要相同,其中有一个不同都会产生跨域

浏览器为了安全会默认阻止跨域请求

http://localhost:8001/#/login   http://localhost:88/api/sys/login

同一域名,不同端口 不允许通信

不是简单请求的会发送预检请求,options是个打头兵,先找服务器问一下,服务器是否允许浏览器访问,如果服务器响应允许,浏览器才会发送真正的请求过去,拿到响应的请求数据。

如何让服务器允许?

1.使用nginx部署为同一域

2.配置当次请求允许跨域

(1)添加响应头

只需在网关里同一配置跨域。

package com.atguigu.gulinall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;



@Configuration
public class GulinallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //1、配置跨域
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}

 预检请求完成

注释掉 renren中配置的跨域 

 

 成功进入

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kiki,

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值