nginx配置跨域

1.前后端分离,前端vue,后端springboot,本机测试存在cookie跨域问题

2.不需要cookie跨域

配置WebMvcConfig

package ;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.FormContentFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebMvc配置, 拦截器、资源映射等都在此配置
 */
@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

//    /**
//     * 支持跨域访问
//     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {

        registry.addMapping("/**")//项目中的所有接口都支持跨域
                .allowedOrigins("*")
                .allowedHeaders("Access-Control-Allow-Headers","Content-Type","Content-Length", "Authorization", "Accept","X-Requested-With")
                .allowedMethods("PUT","POST","GET","DELETE","OPTIONS")//"GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS"
                .maxAge(3600);// 跨域允许时间
    }


}

3.需要cookie跨域

需要cookie跨域的话,后端要设置allowCredentials为true,并且allowOrign不能是*,如果前后端分开开发的话,origin是变的,不能设置唯一值,最后通过nginx解决。
nginx配置内容如下:

server {
    listen       443 ssl;
    server_name  localhost;
    ssl_certificate      D://application//nginx-1.24.0//ssl//mysite.crt;
    ssl_certificate_key  D://application//nginx-1.24.0//ssl//mysite.key.unsecure;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;
	
         
        location / {
	proxy_pass       http://127.0.0.1:8081;
	proxy_set_header host $host;
            proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
            proxy_set_header X-real-ip $remote_addr;
	proxy_cookie_path / "/; httponly; secure; SameSite=None";

	add_header 'Access-Control-Allow-Credentials' 'true' always;
               add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS' always;
               add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
	add_header Access-Control-Allow-Origin  $http_origin always;
	#add_header Set-Cookie  'JSESSIONID=XXX;SameSite=None;Secure' always;


            if ($request_method = 'OPTIONS') {
	add_header 'Access-Control-Max-Age' 1728000 always;
	add_header 'Access-Control-Allow-Credentials' 'true' always;
               add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS' always;
               add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type' always;
	add_header Access-Control-Allow-Origin  $http_origin always;
	add_header 'Content-Length' 0 always;
               return 204;
           }
        }

之所以使用https是因为存在SameSite验证的问题,nginx配置https见我的另一篇博文http://t.csdnimg.cn/jyels。

此外,前端axios需要配置axios.defaults.withCredentials = true;

之后就可以跨域测试了。

本机开发的话,把origin写死也行。
跨域成功后响应标头应该试这样的:
在这里插入图片描述
请求中应该可以看到相应的cookie:
这个是登录请求
这个是后续的其他请求
最后,代码和nginx的跨域设置不要重复,否则nginx的add_header之后会出现相同的header

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值