如果没有 Spring Security,CORS 无法与 Spring Boot 和 Spring Cloud 一起使用

发布于 2025-01-13 17:43:26 字数 2159 浏览 0 评论 0原文

我正在使用 Spring Boot 2.2.4.RELEASE 带有 Spring Cloud Hoxton.SR1 和不带 Spring Security,其中 GET 方法工作正常,但 POST 和 POST 方法工作正常。 PUT 方法不起作用。

@Configuration
@Order(value = Integer.MAX_VALUE)
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("http://localhost:5500")
            .allowedHeaders("*")
            .allowedMethods("POST", "PUT", "DELETE", "HEAD");
    }
}
@Component
public class SimpleCORSFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}

AngularJS 代码

    $http({
        method: 'POST',
        url: 'http://b28c-103-85-11-107.ngrok.io/game/brand-styles',
        data: {
            "fontColour": "FFFFFF",
            "backgroundColour": "006400",
            "displayType": 1,
            "buttonColour": "FFC314"
            }
        })
        .then(function (response) { $scope.data = data; })
        .catch(function (err) { console.log(err) });

在此处输入图像描述

我已经尝试了多种方法来解决同一平台上不同问题中建议的问题,但没有人为我工作。

I am using Spring Boot 2.2.4.RELEASE with Spring Cloud Hoxton.SR1 and without Spring Security, In that GET method is working fine but POST & PUT methods are not working.

@Configuration
@Order(value = Integer.MAX_VALUE)
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
            .allowedOrigins("http://localhost:5500")
            .allowedHeaders("*")
            .allowedMethods("POST", "PUT", "DELETE", "HEAD");
    }
}
@Component
public class SimpleCORSFilter implements Filter {

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
            throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

        chain.doFilter(req, response);
    }

    @Override
    public void init(FilterConfig filterConfig) {
    }

    @Override
    public void destroy() {
    }

}

AngularJS Code

    $http({
        method: 'POST',
        url: 'http://b28c-103-85-11-107.ngrok.io/game/brand-styles',
        data: {
            "fontColour": "FFFFFF",
            "backgroundColour": "006400",
            "displayType": 1,
            "buttonColour": "FFC314"
            }
        })
        .then(function (response) { $scope.data = data; })
        .catch(function (err) { console.log(err) });

enter image description here

I have tried multiple ways to solve the issue as suggested on same platform in different questions but no one worked for me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

剪不断理还乱 2025-01-20 17:43:26

访问 XMLHttpRequest
来自起源的“http://b28c-103-85-11-107.ngrok.io/game/brand-styles”
“http://localhost:5500”已被 CORS 策略阻止:响应
预检请求未通过访问控制检查:否
请求中存在“Access-Control-Allow-Origin”标头
资源。

发布http://b28c-103-85- 11-107.ngrok.io/game/brand-styles
网络::ERR_FAILED

您的案例中问题背后的可能原因是您在 SimpleCORSFilter 类中提到的以下语句。

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

看起来 Origin 标头在到达本地服务器之前已被删除,或者可能被本地服务器本身删除。您可以尝试进行以下更改来验证您的情况。

response.setHeader("Access-Control-Allow-Origin", "http://localhost:5500");

Access to XMLHttpRequest at
'http://b28c-103-85-11-107.ngrok.io/game/brand-styles' from origin
'http://localhost:5500' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.

POST http://b28c-103-85-11-107.ngrok.io/game/brand-styles
net::ERR_FAILED

The possible reason behind the issue in your case would be due to the following statement that you've mentioned in the SimpleCORSFilter class.

response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

Looks like Origin header is dropped before reaching to your local server or may be dropped by the local server itself. You can try with the following change to verify your case.

response.setHeader("Access-Control-Allow-Origin", "http://localhost:5500");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文