后台跨域, nginx跨域,nginx跨域无效
nginx跨域配置
location /api { # 允许跨域请求的域,* 代表所有。如果是特定的域名,应该将 * 替换为确切的域名。 add_header 'Access-Control-Allow-Origin' '*' always; # 允许带上 cookie 请求 add_header 'Access-Control-Allow-Credentials' 'true' always; # 允许请求的方法,比如 GET/POST/PUT/DELETE,* 表示允许所有方法 add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; # 允许请求的 header,* 表示允许所有头信息,也可以根据需要指定允许的头信息 add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, Origin, X-Requested-With, Accept' always; # 如果是预检请求(OPTIONS),直接返回 200,不再执行 proxy_pass if ($request_method = 'OPTIONS') { add_header 'Content-Length' 0; add_header 'Content-Type' 'text/plain charset=UTF-8'; add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain charset=UTF-8'; return 204; } # 代理到后端服务 proxy_pass http://111:8080/api; }
当nginx配置无效请修改后台跨域配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 CorsConfig { @Bean public CorsWebFilter corsFilter() { UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); // 配置跨域 CorsConfiguration corsConfiguration = new CorsConfiguration(); // 允许哪个请求头 corsConfiguration.addAllowedHeader( "*" ); // 允许哪个方法进行跨域 corsConfiguration.addAllowedMethod( "*" ); // 允许哪个请求来源进行跨域 // corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedOriginPattern( "*" ); // 是否允许携带cookie进行跨域 corsConfiguration.setAllowCredentials( true ); source.registerCorsConfiguration( "/**" ,corsConfiguration); return new CorsWebFilter(source); } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!