我们的后端使用Spring Boot。Spring Boot跨域非常简单,只需书写以下代码即可。

  1. @Configuration
  2. public class CustomCORSConfiguration {
  3. private CorsConfiguration buildConfig() {
  4. CorsConfiguration corsConfiguration = new CorsConfiguration();
  5. corsConfiguration.addAllowedOrigin("*");
  6. corsConfiguration.addAllowedHeader("*");
  7. corsConfiguration.addAllowedMethod("*");
  8. return corsConfiguration;
  9. }
  10. @Bean
  11. public CorsFilter corsFilter() {
  12. UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
  13. source.registerCorsConfiguration("/**", buildConfig());
  14. return new CorsFilter(source);
  15. }
  16. }

某天,我们将Spring Boot应用用Nginx反向代理。而前端跨域请求的需求不减,于是乎。

Nginx跨域也比较简单,只需添加以下配置即可。

  1. location / {
  2. proxy_pass http://localhost:8080;
  3. if ($request_method = 'OPTIONS') {
  4. add_header 'Access-Control-Allow-Origin' '*';
  5. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  6. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  7. add_header 'Access-Control-Max-Age' 1728000;
  8. add_header 'Content-Type' 'text/plain; charset=utf-8';
  9. add_header 'Content-Length' 0;
  10. return 204;
  11. }
  12. if ($request_method = 'POST') {
  13. add_header 'Access-Control-Allow-Origin' '*';
  14. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  15. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  16. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  17. }
  18. if ($request_method = 'GET') {
  19. add_header 'Access-Control-Allow-Origin' '*';
  20. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  21. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  22. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range,Token';
  23. }
  24. }

其中: add_header 'Access-Control-Expose-Headers' 务必加上你请求时所带的header。例如本例中的“Token”,其实是前端传给后端过来的。如果记不得也没有关系,浏览器的调试器会有详细说明。

参考文档:https://enable-cors.org/server_nginx.html

B.T.W,阿里云中文档描述到Nginx也可通过crossdomain.xml配置文件跨域:https://helpcdn.aliyun.com/knowledge_detail/41123.html ,不过笔者并未采用这种方式。

The following Nginx configuration enables CORS, with support for preflight requests.

  1. #
  2. # Wide-open CORS config for nginx
  3. #
  4. location / {
  5. if ($request_method = 'OPTIONS') {
  6. add_header 'Access-Control-Allow-Origin' '*';
  7. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  8. #
  9. # Custom headers and headers various browsers *should* be OK with but aren't
  10. #
  11. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  12. #
  13. # Tell client that this pre-flight info is valid for 20 days
  14. #
  15. add_header 'Access-Control-Max-Age' 1728000;
  16. add_header 'Content-Type' 'text/plain; charset=utf-8';
  17. add_header 'Content-Length' 0;
  18. return 204;
  19. }
  20. if ($request_method = 'POST') {
  21. add_header 'Access-Control-Allow-Origin' '*';
  22. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  23. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  24. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  25. }
  26. if ($request_method = 'GET') {
  27. add_header 'Access-Control-Allow-Origin' '*';
  28. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  29. add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  30. add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
  31. }
  32. }

Chrome、Firefox本身是可以通过配置支持跨域请求的。

  • Chrome跨域:参考文档:http://www.cnblogs.com/laden666666/p/5544572.html
  • Firefox跨域:参考文档:https://segmentfault.com/q/1010000002532581/a-1020000002533699

版权声明:本文为alterem原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/alterem/p/11227200.html