gpt4 book ai didi

angular - 在 Spring Boot 后端/Angularjs 前端应用程序中允许多个跨源?

转载 作者:行者123 更新时间:2023-12-05 04:10:22 24 4
gpt4 key购买 nike

我在当前工作的 Web 应用程序中收到以下错误,错误代码为:

XMLHttpRequest cannot load http://rev-final.staging.cashu.com:8080/merchants/signup. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'http://localhost:3000' is therefore not allowed access.

那么我能做些什么来解决这样的问题,解决方案必须来自 Angular 方面或 Spring 方面,因为我在寻找解决方案的同时在这两个方面都找到了配置

最佳答案

只需在您的组件扫描包中添加以下类,然后一切都会由 spring 处理。请引用 here供进一步引用..

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class SimpleCORSFilter implements Filter {

private final Logger log = LoggerFactory.getLogger(SimpleCORSFilter.class);

public SimpleCORSFilter() {
log.info("SimpleCORSFilter init");
}

@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");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With, remember-me");

chain.doFilter(req, res);
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

关于angular - 在 Spring Boot 后端/Angularjs 前端应用程序中允许多个跨源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44647477/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com