Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
403在Spring RestAPI开发期间请求POST或PUT时出错_Spring_Spring Security_Cors_Http Status Code 403 - Fatal编程技术网

403在Spring RestAPI开发期间请求POST或PUT时出错

403在Spring RestAPI开发期间请求POST或PUT时出错,spring,spring-security,cors,http-status-code-403,Spring,Spring Security,Cors,Http Status Code 403,我试图在一个应用了Spring5+SpringSecurity5的项目中实现和使用RESTAPI 在本地运行Tomcat后与Postman一起测试时 url:http://localhost:8080/api~ 已确认get、post和put等请求正常工作 我把这个项目上传到https服务器上,在本地邮递员的服务器上测试api时 请求(如put和post)中发生403禁止的错误,get请求除外 当我在谷歌上搜索时,它说Spring Security的csrf问题+cors处理问题 所以我就这样

我试图在一个应用了Spring5+SpringSecurity5的项目中实现和使用RESTAPI

在本地运行Tomcat后与Postman一起测试时

url:http://localhost:8080/api~

已确认get、post和put等请求正常工作

我把这个项目上传到https服务器上,在本地邮递员的服务器上测试api时

请求(如put和post)中发生403禁止的错误,get请求除外

当我在谷歌上搜索时,它说Spring Security的csrf问题+cors处理问题


所以我就这样修改了代码

  • 网络安全配置
  • WebMvcConfig
  • 控制器
但在freflight请求中,它通常返回200,但在这个请求中,我仍然得到403


哪一部分错了?如果您让我知道,我将不胜感激:)

在您的配置中通过HttpSecurity builder添加Cors过滤器。 i、 e

这将启用PUT和POST使用的cors飞行前请求。
您不需要包括CorsConfiguration源代码,只需确保控制器中也有@CrossOrigin注释。

嗨,mandy,欢迎使用StackOverflow。这不是一个真正的答案。如果您想添加更多信息或提供其他示例,请编辑原始问题
@Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                    .authorizeRequests()
                    .antMatchers("/").permitAll()                   
                   ~
                   .anyRequest().permitAll()
                .and()
                    .formLogin()
                 ~                  
                .and()
                    .logout()
               ~
                .and()
                    .httpBasic().disable().cors().configurationSource(corsConfigurationSource())
                .and()
                    .sessionManagement()
                    ~
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration);
        return source;
    }
@Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET", "POST", "PUT", "DELETE")
                .allowedOrigins("*");
    }
@CrossOrigin("*")
Override
protected void configure(HttpSecurity http) throws Exception {
    http.cors()
        .........