gpt4 book ai didi

angularjs - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 Ionic、AngularJS、Spring Boot 1.3

转载 作者:行者123 更新时间:2023-12-03 06:58:05 24 4
gpt4 key购买 nike

我正在使用 Ionic 和 Spring Boot 1.3。直到我升级到1.3才遇到这个问题...

显然是在更新到 Spring Boot 1.3 之后。 CorsFilter 被完全忽略。所有这些贬低都让我发疯。所以我查找了新方法,这就是我得到的。

package app.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://192.168.1.66:8101")
.allowCredentials(false)
.maxAge(3600)
.allowedHeaders("Accept", "Content-Type", "Origin", "Authorization", "X-Auth-Token")
.exposedHeaders("X-Auth-Token", "Authorization")
.allowedMethods("POST", "GET", "DELETE", "PUT", "OPTIONS");
}
}

上面的代码在应用程序启动时执行。与每次有请求时执行的 CorsFilter 不同。但是切换到 Spring Boot 1.3,我无法再在链过滤器中获取它。

再次,正在加载代码,我设置了一个断点,并且每次都会调用 addCorsMapping 以便进行设置。那么...为什么我仍然收到此错误

XMLHttpRequest cannot load http://192.168.1.66:8080/login?username=billyjoe&password=happy. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.1.66:8101' is therefore not allowed access.

编辑下面是我的旧 CorsFilter。自从我更新到 Spring Boot 1.3 后它不再起作用

package app.config;

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 CorsFilter implements Filter {

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

public CorsFilter() {
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;
String clientOrigin = request.getHeader("origin");
response.addHeader("Access-Control-Allow-Origin", clientOrigin);
response.setHeader("Access-Control-Allow-Methods", "POST, GET, DELETE, PUT");
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Accept, Content-Type, Origin, Authorization, X-Auth-Token");
response.addHeader("Access-Control-Expose-Headers", "X-Auth-Token");

if (request.getMethod().equals("OPTIONS")) {
response.setStatus(HttpServletResponse.SC_OK);
} else {
chain.doFilter(request, response);
}
}

@Override
public void init(FilterConfig filterConfig) {
}

@Override
public void destroy() {
}

}

最佳答案

你可以尝试这样的事情。它对我有用:.

@Component
public class SimpleCORSFilter implements Filter {

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, OPTIONS, DELETE, PATCH");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
chain.doFilter(req, res);
}

public void init(FilterConfig filterConfig) {}

public void destroy() {}

}

关于angularjs - 请求的资源上不存在 'Access-Control-Allow-Origin' header 。 Ionic、AngularJS、Spring Boot 1.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34173432/

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