【问题标题】:XMLHttpRequest blocked by CORS policy annotation @CrossOriginXMLHttpRequest 被 CORS 策略注释 @CrossOrigin 阻止
【发布时间】:2021-09-25 20:35:20
【问题描述】:

从源“https://mysuite.ru”访问“http://localhost:8080/api/auth/signup”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:预检请求不允许重定向。

http.cors().and()
            .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
            .authorizeRequests()
            .antMatchers("/admin").hasRole("ADMIN")
            .antMatchers("/").permitAll()
            .antMatchers("/favicon.ico").permitAll()
            .antMatchers("/static/**").permitAll()
            .antMatchers("/manifest.json").permitAll()
            .antMatchers("/logo192.png").permitAll()
            .antMatchers("/api/auth/**").permitAll()
            .antMatchers("/api/test/**").permitAll()
            .anyRequest().authenticated();

    http.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);




@RestController
@RequestMapping("/api/auth")
@CrossOrigin(origins = "*", maxAge = 3600)
public class AuthController {

通过 nginx 重定向

server {
    listen       443 ssl;
    server_name   is my address.ru;

    ssl_certificate      C:/ssl/ip.crt;
    ssl_certificate_key  C:/ssl/ip.key;

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

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

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-NginX-Proxy true;
      proxy_pass        http://localhost:8080;
      proxy_set_header  Host $http_host;
      proxy_redirect off;
    }
}

更新

请求标头:

Accept: application/json, text/plain, */*
Access-Control-Allow-Headers: DNT,User-Agent,X-Requested-With,If- 
Modified-Since,Cache-Control,Content-Type,Range
Access-Control-Allow-Methods: GET, POST, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: Content-Length,Content-Range
Content-Type: application/json;charset=UTF-8
Referer
sec-ch-ua: "Chromium";v="92", " Not A;Brand";v="99", "Yandex";v="21"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 
(KHTML, like Gecko) Chrome/92.0.4515.159 YaBrowser/21.8.3.614 Yowser/2.5 
Safari/537.36

在 nginx 中添加:

location / {
    
    if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
     #
     # Custom headers and headers various browsers *should* be OK with but aren't
     #
     add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
     #
     # Tell client that this pre-flight info is valid for 20 days
     #
     add_header 'Access-Control-Max-Age' 1728000;
     add_header 'Content-Type' 'text/plain; charset=utf-8';
     add_header 'Content-Length' 0;
     return 204;
     }
     if ($request_method = 'POST') {
     add_header 'Access-Control-Allow-Origin' '*' always;
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
     add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
     add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
     }
     if ($request_method = 'GET') {
     add_header 'Access-Control-Allow-Origin' '*' always;
     add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
     add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always;
     add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }
    
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://localhost:8080;
    }

还没有结果

【问题讨论】:

    标签: spring spring-boot nginx


    【解决方案1】:

    尝试将methods 属性添加到@CrossOrigin。用于注释方法时,支持的方法与方法映射到的方法相同,但由于您在类级别使用它,因此可能需要按如下方式指定它们:

    @CrossOrigin(origins = "*", maxAge = 3600, methods = {RequestMethod.GET, RequestMethod.POST} ) // Just an example
    

    【讨论】:

    • 它应该是什么样子? .....方法=“GET”,“POST”); ?
    • 我已经编辑了我的答案;)
    • 不幸的是,由于某种原因,这对我没有帮助(
    • 您能否尝试在您的请求中添加Origin 标头以及请求本身的原始域?此外,您是否尝试在后端进行一些重定向?
    • 是的,目前我正在使用 nginx,我将监听从域和端口 80 重定向到 443,然后从那里重定向到 spring 应用程序本身 localhost:8080
    【解决方案2】:

    尝试将 @CrossOrigin 注释替换为

     @CrossOrigin(value = "https://mysuite.ru", allowCredentials = "true")
    

    不要使用“*”作为来源,而是使用“https://mysuite.ru”。

    【讨论】:

    • 由于某种原因这对我没有帮助
    【解决方案3】:

    问题是我错误地在前端向 localhost 发出了 post 请求

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 2021-09-05
      • 2021-10-27
      • 1970-01-01
      • 2021-04-24
      • 2019-12-18
      • 2019-10-31
      • 2019-04-28
      • 1970-01-01
      相关资源
      最近更新 更多