配置 http 请求 spring mvc 和 angular js?
Config http request spring mvc and angular js?
我在本地机器上发布了这个服务:
http://joediego.dtdns.net:3000/jcsistemas/rest/cliente/all
如果我点击提到的 URL 端点,我可以通过我的 JSON 提要获得响应。但是当我使用另一个主机名时,例如 http://localhost:8081,服务会响应以下消息:
XMLHttpRequest can not load http: // localhost: 8081 / jcsistemas / rest / customer / all. In the 'Access-Control-Allow-Origin "header is present on the requested resource. Origin 'http: // localhost: 9000' is not allowed Therefore access.
以下是我迄今为止为克服同源政策问题所做的努力:
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.HttpServletResponse;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
//filter
@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, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
@Configuration
@EnableWebMvc
@ComponentScan("my..package...... ")
public class WebAppConfig extends WebMvcConfigurerAdapter {
//configureContentNegotiation
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true).
ignoreAcceptHeader(true).
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("html", MediaType.TEXT_HTML).
mediaType("xml", MediaType.APPLICATION_XML);
}
}
AngularJS 模块配置:
angular.module('my_module').config(['$routeProvider','$httpProvider',function($routeProvider,$httpProvider) {
//how configure headers here??
}]);
}());
我设法通过删除 @Component SimpleCorsFilter
并在 WebApplicationInitializer
子类中添加 getServletFilters()
方法来解决我的问题。对于那些使用基于 Java 的应用程序配置的人来说,这里是组件声明:
public MyWebApplicationInitializer implements WebApplicationInitializer
{
@ Override
protected Filter[] getServletFilters()
{
return new Filter[] {new MyFilter()}; // MyFilter refers to the filter needed class
}
}
我在本地机器上发布了这个服务:
http://joediego.dtdns.net:3000/jcsistemas/rest/cliente/all
如果我点击提到的 URL 端点,我可以通过我的 JSON 提要获得响应。但是当我使用另一个主机名时,例如 http://localhost:8081,服务会响应以下消息:
XMLHttpRequest can not load http: // localhost: 8081 / jcsistemas / rest / customer / all. In the 'Access-Control-Allow-Origin "header is present on the requested resource. Origin 'http: // localhost: 9000' is not allowed Therefore access.
以下是我迄今为止为克服同源政策问题所做的努力:
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.HttpServletResponse;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
//filter
@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, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
chain.doFilter(req, res);
}
public void init(FilterConfig filterConfig) {}
public void destroy() {}
}
@Configuration
@EnableWebMvc
@ComponentScan("my..package...... ")
public class WebAppConfig extends WebMvcConfigurerAdapter {
//configureContentNegotiation
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.favorPathExtension(true).
ignoreAcceptHeader(true).
useJaf(false).
defaultContentType(MediaType.APPLICATION_JSON).
mediaType("html", MediaType.TEXT_HTML).
mediaType("xml", MediaType.APPLICATION_XML);
}
}
AngularJS 模块配置:
angular.module('my_module').config(['$routeProvider','$httpProvider',function($routeProvider,$httpProvider) {
//how configure headers here??
}]);
}());
我设法通过删除 @Component SimpleCorsFilter
并在 WebApplicationInitializer
子类中添加 getServletFilters()
方法来解决我的问题。对于那些使用基于 Java 的应用程序配置的人来说,这里是组件声明:
public MyWebApplicationInitializer implements WebApplicationInitializer
{
@ Override
protected Filter[] getServletFilters()
{
return new Filter[] {new MyFilter()}; // MyFilter refers to the filter needed class
}
}