如果没有 Spring Security,CORS 无法与 Spring Boot 和 Spring Cloud 一起使用
我正在使用 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) });
I have tried multiple ways to solve the issue as suggested on same platform in different questions but no one worked for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的案例中问题背后的可能原因是您在
SimpleCORSFilter
类中提到的以下语句。看起来
Origin
标头在到达本地服务器之前已被删除,或者可能被本地服务器本身删除。您可以尝试进行以下更改来验证您的情况。The possible reason behind the issue in your case would be due to the following statement that you've mentioned in the
SimpleCORSFilter
class.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.