当前位置:   article > 正文

前后端分别解决跨域问题 CORS错误_cors 错误

cors 错误

一、前端:配置nginx

  1. # 跨域配置
  2. location ^~ /api/ {
  3. proxy_pass http://127.0.0.1:8080/api/;
  4. add_header 'Access-Control-Allow-Origin' $http_origin;
  5. add_header 'Access-Control-Allow-Credentials' 'true';
  6. add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
  7. add_header Access-Control-Allow-Headers '*';
  8. if ($request_method = 'OPTIONS') {
  9. add_header 'Access-Control-Allow-Credentials' 'true';
  10. add_header 'Access-Control-Allow-Origin' $http_origin;
  11. add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
  12. add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
  13. add_header 'Access-Control-Max-Age' 1728000;
  14. add_header 'Content-Type' 'text/plain; charset=utf-8';
  15. add_header 'Content-Length' 0;
  16. return 204;
  17. }
  18. }

二、后端处理的看这

方式①:添加 web 全局请求拦截器

  1. @Configuration
  2. public class WebMvcConfg implements WebMvcConfigurer {
  3. @Override
  4. public void addCorsMappings(CorsRegistry registry) {
  5. //设置允许跨域的路径
  6. registry.addMapping("/**")
  7. //设置允许跨域请求的域名
  8. //当**Credentials为true时,**Origin不能为星号,需为具体的ip地址【如果接口不带cookie,ip无需设成具体ip】
  9. .allowedOrigins("http://localhost:9527", "http://127.0.0.1:9527", "http://127.0.0.1:8082", "http://127.0.0.1:8083")
  10. //是否允许证书 不再默认开启
  11. .allowCredentials(true)
  12. //设置允许的方法
  13. .allowedMethods("*")
  14. //跨域允许时间
  15. .maxAge(3600);
  16. }
  17. }

方式②:配置 @CrossOrigin 注解

  1. package com.arhorchin.securitit.webannotations;
  2. import java.util.Map;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.CrossOrigin;
  7. import org.springframework.web.bind.annotation.RequestHeader;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.RequestMethod;
  10. import org.springframework.web.bind.annotation.ResponseBody;
  11. /**
  12. * @author Securitit.
  13. * @note 演示@CrossOrigin注解使用方法.
  14. */
  15. @Controller
  16. @RequestMapping("/WebAnnotations")
  17. public class CrossOriginController {
  18. /**
  19. * logger.
  20. */
  21. private Logger logger = LoggerFactory.getLogger(CrossOriginController.class);
  22. /**
  23. * 未使用@CrossOrigin.
  24. */
  25. @ResponseBody
  26. @RequestMapping(
  27. value = "/UnCrossOrigin.do",
  28. method = RequestMethod.GET)
  29. public String unCrossOrigin(@RequestHeader Map<String, String> requestHeaderMap) throws Exception {
  30. logger.info("@CrossOrigin use default value.");
  31. return "@CrossOrigin use default value.";
  32. }
  33. /**
  34. * 使用默认值的@CrossOrigin.
  35. */
  36. @ResponseBody
  37. @RequestMapping(
  38. value = "/CrossOrigin.do",
  39. method = RequestMethod.GET)
  40. @CrossOrigin
  41. public String crossOrigin(@RequestHeader Map<String, String> requestHeaderMap) throws Exception {
  42. logger.info("@CrossOrigin use default value.");
  43. return "@CrossOrigin use default value.";
  44. }
  45. /**
  46. * 指定origins属性的@CrossOrigin.
  47. */
  48. @ResponseBody
  49. @RequestMapping(
  50. value = "/CrossOriginOrigins.do",
  51. method = RequestMethod.GET)
  52. @CrossOrigin(origins="http://localhost:9299")
  53. public String crossOriginOrigins(@RequestHeader Map<String, String> requestHeaderMap) throws Exception {
  54. logger.info("@CrossOrigin with origins.");
  55. return "@CrossOrigin with origins.";
  56. }
  57. /**
  58. * 指定origins、allowedHeaders、exposedHeaders、allowCredentials属性的@CrossOrigin.
  59. */
  60. @ResponseBody
  61. @RequestMapping(
  62. value = "/CrossOriginOriginsAllowCredentials.do",
  63. method = RequestMethod.GET)
  64. @CrossOrigin(origins="http://localhost:9299", allowCredentials="true")
  65. public String crossOriginAllowedHeadersExposedHeadersAllowCredentials(@RequestHeader Map<String, String> requestHeaderMap) throws Exception {
  66. logger.info("@CrossOrigin with origins、allowedHeaders、exposedHeaders、allowCredentials.");
  67. return "@CrossOrigin with origins、allowedHeaders、exposedHeaders、allowCredentials.";
  68. }
  69. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/660711
推荐阅读
  

闽ICP备14008679号