Spring CorsRegistry似乎忽略了一些协议

时间:2020-03-01 02:49:21

标签: spring spring-boot ionic-framework

我正在开发混合离子应用程序,并且在CORS方面遇到问题。

我经常遇到CORS问题,我相信这与Spring安全性有关。

由于Ionic的iOS应用程序在ionic:// localhost上运行,因此我的后端(Spring)不接受来自iOS应用程序的任何请求。请参阅下面的浏览器控制台错误:

Origin ionic://localhost is not allowed by Access-Control-Allow-Origin.
XMLHttpRequest cannot load https://example.com/forum/categories due to access control checks.

这是预料之中的。从技术上讲,我需要做的就是将这个来源添加到我的Spring应用程序中,如下所示:

public WebMvcConfigurer corsConfigurer() {
        return new WebMvcConfigurer() {
            @Override
            public void addCorsMappings(CorsRegistry registry) {
                registry.addMapping("/**")
                        .allowedOrigins("ionic://localhost")
                        .allowedMethods("GET", "POST", "PUT", "DELETE", "PATCH");

但是,这并不能为我解决CORS问题。我仍然在浏览器控制台中收到相同的CORS错误消息。

我也尝试了allowedOrigins(“ *”),但效果不佳。

现在我怀疑Spring是否不允许某些协议,尽管它已在'allowedOrigins'中添加。

有人遇到这个问题吗?任何见识将不胜感激。

1 个答案:

答案 0 :(得分:0)

对不起。当我在开发人员中对所有CORS进行更改时,这是我的愚蠢之举,并试图打入Production实例。

Spring CORS配置不支持任何协议,因为它实际上是后端中的字符串比较,根本不关心协议。