Spring boot - 预检响应没有 HTTP 正常状态

2023-11-23

我正在使用 Angular 5 制作网络,每次尝试执行此操作时都会收到此错误GET要求。我在这里阅读了大量的答案,但没有一个对我有用。

正如我所读到的,这是因为我正在向此请求添加自定义标头,这是需要完成的,因为我正在使用 Spring Security,我认为这是导致问题的原因。这是我当前的 Spring Security 配置,我通过阅读问题进行了配置,但仍然无法正常工作,我不知道我是否做错了什么:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import java.util.Arrays;

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                .authorizeRequests().anyRequest().permitAll()
                .and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable();
    }

    @Override
    public void configure(WebSecurity web ) throws Exception
    {
        web.ignoring().antMatchers( HttpMethod.OPTIONS, "/**" );
    }

    @Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"));
        configuration.setAllowedHeaders(Arrays.asList("authorization", "content-type", "x-auth-token"));
        configuration.setExposedHeaders(Arrays.asList("x-auth-token"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

希望你能帮忙解决这个问题,因为我已经为此苦苦挣扎了几天。我认为显然这里的问题是CORS, my GET请求被转换为OPTIONS一方面是因为习俗headers and Spring Security.

另外我想提一下,我正在使用带有 Jersey 的 Spring Boot。

Thanks.


我能够像这样解决这个问题:

我的 WebMvc 配置:

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**");
    }

}

我的安全配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
      http.cors().disable()
          .authorizeRequests()
          .antMatchers(HttpMethod.OPTIONS, "/**").permitAll()
          .anyRequest()
          .fullyAuthenticated()
          .and()
          .httpBasic()
          .and()
          .csrf().disable();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring boot - 预检响应没有 HTTP 正常状态 的相关文章

随机推荐

  • 许多 Pandas 数据框的箱线图

    我有三个数据框 包含 17 组数据 其中 A B 和 C 组 A 如以下代码片段所示 import pandas as pd import numpy as np data1 pd DataFrame np random rand 17 3
  • 使用 RsaProtectedConfigurationProvider 对 app.config 部分进行加密/解密

    在安装程序期间 我们运行此方法来加密 app config 的部分 Get the application configuration file Configuration config ConfigurationManager OpenE
  • 如何在 Entity Framework Code First 中分离对象?

    没有Detach object entity on the DbContext 我是否能够首先分离 EF 代码上的对象 这是一个选项 dbContext Entry entity State EntityState Detached
  • 用 PHP 编写合并排序

    我尝试在 PHP 中编写一个涉及小数组的基本合并排序 但问题是它需要大约一分钟左右的时间来执行 并返回 致命错误 允许的内存大小 536870912 字节已耗尽 已尝试 分配 35 个字节 在 Users web www merge php
  • 范围 ElasticSearch 聚合

    我需要在 ElasticSearch 中计算管道聚合 但我不知道如何表达它 每个文档都有一个电子邮件地址和金额 我需要输出金额计数的范围桶 并按唯一的电子邮件分组 0 99 300 100 400 100 基本上是预期的输出 密钥将在我的应
  • npm install eslint 缺少参数错误

    我正在尝试在项目文件夹中本地安装 eslint 但是按照上面提到的安装https eslint org npm install eslint save dev 返回以下错误 npm ERR Linux 4 15 0 70 generic n
  • 查看文件所有者无法使用的插座

    创建新的视图控制器时我收到以下消息 一切都在 Xcode 中编译正常 没有错误 但应用程序在从 RootViewController 加载新视图后立即退出 Terminating app due to uncaught exception
  • 窗口更新破坏了 MVC 应用程序

    我昨天运行了 Windows Update 当我尝试发布 ASP NET MVC 4 项目的新版本时 它引入了一些问题 该应用程序在本地编译并运行正常 但是当我将版本推送到网络服务器上的测试站点时 它失败并显示错误消息 System Web
  • 如何制作QheaderView多层次?

    我正在创建一个保存对象发送服务数据的应用程序 我用它创建了Qt 类型模型QStandardItemModel我想用它来显示QtableView But QtableView显示左侧的线路电平 如果可能的话 我想删除它或隐藏它 我也有一个标题
  • 无法连接到 Jenkins 服务器 (Amazon Linux AMI)

    当我在 Amazon Linux AMI 上安装 Jenkins 时 遵循中提到的步骤http bhargavamin com how to do install jenkins on amazon linux aws 安装后 我可以通过浏
  • 当结果集很大时,即使使用索引,mongodb.countDocuments 也会很慢

    mongodb countDocuments当结果集很大时速度很慢 用户收集测试数据 1000 万个带有状态的文档 active 100k 个带有状态的文档 inactive 场status已编入索引 状态 1 db users count
  • 以水平顺序而不是默认的垂直视图在 Shiny 中的元素中显示单选按钮

    我正在开发一个 Shiny 应用程序 使人们能够浏览一些时间序列数据 我有许多小部件 使人们能够选择变量和分析类型 其间 我有一个谦虚的radioButton允许用户为某些变量选择时间序列的机制 radioButtons radio yea
  • Spring Boot中如何配置CORS和基本授权?

    我正在尝试在已设置基本身份验证的 Spring boot 应用程序中配置 CORS 我搜索过很多地方 包括这个答案 这指向基于过滤器的 CORS 支持在官方文档中 到目前为止还没有运气 我的AJAX请求就是这样完成的 如果是从同一个来源完成
  • 如何将 system() 的输出重定向到文件?

    In this C program include
  • Git 状态获取父文件夹的文件

    我刚刚创建了一个新项目htdocs project当我使用状态来获取该文件夹中的文件时 它会列出其中包含的文件和目录htdocs project但它还列出了所有文件夹和子文件夹htdocs以及 例如 Changes not staged f
  • Android 从 URI 读取文本文件

    我有一个Uri指向一个文本文件intent我正在尝试读取该文件以解析其中的字符串 这是我尝试过的 但失败了FileNotFoundException The toString 方法似乎失去了 java io FileNotFoundExce
  • 缺少终身运营商

    我在 Rust 中有以下代码 我知道我不应该返回对局部变量的引用 在这种情况下我不应该返回 要分割的字符串作为 str参考 确定分割边界后 我返回 s 0 idx where idx是边界的终点 我确信这不会导致 悬空 引用相关错误 然而事
  • Spring Boot Hibernate 没有事务正在进行

    我正在使用 Spring Boot 它完美地使我成为实体管理器 我决定测试从实体管理器获取会话工厂并使用它作为示例 但我遇到了下一个问题 javax persistence TransactionRequiredException no t
  • 在继续之前等待 API 调用在 Javascript 中完成

    我过去和今天都在努力解决的问题是 在您收到回复之前 API AJAX 无法继续 目前我正在使用 Facebook API 我需要从调用中获取响应然后返回它 但发生的情况是我的函数在我从 API 调用中获得响应之前就已返回 我知道为什么会发生
  • Spring boot - 预检响应没有 HTTP 正常状态

    我正在使用 Angular 5 制作网络 每次尝试执行此操作时都会收到此错误GET要求 我在这里阅读了大量的答案 但没有一个对我有用 正如我所读到的 这是因为我正在向此请求添加自定义标头 这是需要完成的 因为我正在使用 Spring Sec