【问题标题】:Spring boot redirect angular 5 CORS problemSpring Boot重定向角度5 CORS问题
【发布时间】:2019-04-28 14:54:35
【问题描述】:

我有一个 Angular 5 应用程序,它向我的 Spring Boot 服务器发出 POST 请求。在我的后端所做的只是将重定向到我的角度应用程序的另一个位置。(我这样做是因为我正在模拟当外部服务向我的后端发出 POST 请求时我必须执行的重定向)但是当从我的angular 应用程序我向后端发出 post 请求以执行重定向,出现问题:

访问 XMLHttpRequest 在 'http://localhost:4200/payment_resume?state=1&billNumber=234343&description=descr&value=120.000&method=Visa' (重定向自'http://localhost:8083/v1/redirect')来自原点 “null”已被 CORS 策略阻止:请求标头字段 Access-Control-Allow-Headers 不允许授权 预检响应。

我不明白为什么 CORS 会出现问题,如果我在后端进行了调试并且请求被我的服务器接受并处理并给出重定向响应......当我的后端响应我的重定向时角度应用出现错误...

后端代码:

@RequestMapping(value = "/redirect", method = RequestMethod.POST)
    @Transactional
    public void returnData(UriComponentsBuilder uriComponentsBuilder, final HttpServletRequest request,
            final HttpServletResponse response) throws IOException {

        String transactionState="1";
        String billCode="234343";
        String description="descr";
        String billValue="120.000";
        String paymentMethod="Visa";


        response.sendRedirect(hostname+"/payment_resume?state="+ transactionState
                +"&"+"billNumber="+billCode
                +"&"+"description="+description
                +"&"+"value="+billValue
                +"&"+"method="+paymentMethod);
        return;
    }

CORS 配置:

@Configuration
@EnableWebSecurity
public class WebSecurity extends WebSecurityConfigurerAdapter {
@Bean
    CorsConfigurationSource corsConfigurationSource() {
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedOrigin("*");
        configuration.addAllowedHeader("*");
        configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","PATCH","DELETE","OPTIONS"));
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", configuration);
        return source;
    }
}

前端代码:

redirect(){
    this.http.post(environment.apiUrlBase+"/redirect","").subscribe();
  }

我的问题是我应该在我的 Angular 应用程序中进行哪些配置才能使其正常工作?

非常感谢!

【问题讨论】:

  • 为了在我的项目中对抗 CORS 效应,我在运行 Angular 服务器时使用了代理。这是另一个 Stack Overflow 帖子的 link。它可能会错过配置中的一行。我补充说:"changeOrigin": true。看看吧。

标签: javascript java spring angular spring-boot


【解决方案1】:

我也遇到了同样的问题,然后我使用了 CrosFilter。在 Bean 下方添加并尝试

@Bean
public CorsFilter corsFilter() {
    final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    final CorsConfiguration configuration = new CorsConfiguration();
    configuration.setAllowCredentials(true);
    configuration.addAllowedOrigin("*");
    configuration.addAllowedHeader("*");
    configuration.addAllowedMethod("OPTIONS");
    configuration.addAllowedMethod("HEAD");
    configuration.addAllowedMethod("GET");
    configuration.addAllowedMethod("PUT");
    configuration.addAllowedMethod("POST");
    configuration.addAllowedMethod("DELETE");
    configuration.addAllowedMethod("PATCH");
    source.registerCorsConfiguration("/**", configuration);
    return new CorsFilter(source);
}

【讨论】:

  • 我更换了您建议的配置,但它不起作用..
猜你喜欢
  • 2021-04-11
  • 2016-07-09
  • 2018-07-12
  • 1970-01-01
  • 2020-09-08
  • 2020-05-31
  • 2020-03-02
  • 2019-05-17
相关资源
最近更新 更多