为什么我的本地网站无法访问我的本地REST服务?

时间:2014-02-14 07:11:33

标签: web-services angularjs rest tomcat spring-mvc

我有一个Spring MVC REST服务在端口8080上通过Tomcat本地运行。当我直接通过浏览器或fiddler点击服务时,它按预期工作。我还有一个使用AngularJS构建的本地网站。如果我将此网站放在Tomcat目录中并导航到Tomcat提供的网站链接,一切仍然可以正常工作。

问题是我没有将网站放在Tomcat目录中。假设我将网站项目放在桌面上并导航到应该从本地REST服务检索数据的页面...我可以在fiddler中看到请求正在通过,命中服务,服务返回数据...但网站没有显示它!

为什么只有当两者都由Tomcat提供时才能正常工作?

直接链接到点击服务:     http://localhost:8080/CPProject/users(以JSON格式返回所有用户)

网站(此作品):     http://localhost:8080/CPWebsite/app/#/users

网站(不起作用):     http://localhost:63342/CPWebsite/app/index.html#/users(63342是Webstorm 7.0使用的端口) 要么     file:///C:/Users/someuser/CPWebsite/app/index.html#/users

我的Angular控制器看起来像这样:

cpControllers.controller('UserListCtrl',
    function($scope, $http) {
        $http.get('http://localhost:8080/CP/users')
            .success(function(data) {
                $scope.users = data;
            }
        )
    }
);

我还在Sprint MVC服务中创建了一个CORS过滤器:

@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", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
        response.setHeader("Access-Control-Max-Age", "3600");
        response.setHeader("Access-Control-Allow-Headers", "X-PINGOTHER, Origin, X-Requested-With, Content-Type, Accept");
        chain.doFilter(req, res);
    }

    public void init(FilterConfig filterConfig) {}

    public void destroy() {}

}

2 个答案:

答案 0 :(得分:1)

您需要在应用中添加正确配置的CORS filter,以便进行预检请求。这是一个示例配置:

<filter>
    <filter-name>CORS</filter-name>
    <filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>

    <init-param>
        <param-name>cors.allowGenericHttpRequests</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowOrigin</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.allowSubdomains</param-name>
        <param-value>false</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedMethods</param-name>
        <param-value>GET, HEAD, POST, PUT, DELETE, OPTIONS</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportedHeaders</param-name>
        <param-value>*</param-value>
    </init-param>

    <init-param>
        <param-name>cors.supportsCredentials</param-name>
        <param-value>true</param-value>
    </init-param>

    <init-param>
        <param-name>cors.maxAge</param-name>
        <param-value>3600</param-value>
    </init-param>

    <init-param>
        <param-name>cors.tagRequests</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>CORS</filter-name>
    <url-pattern>/api/*</url-pattern>
</filter-mapping>

答案 1 :(得分:0)

感谢上面的答案。事实证明我的CORS过滤器很好,但我忘了将它添加到我的应用程序初始化程序中,以便过滤器实际工作:

public class WebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext servletContext) {
        WebApplicationContext rootContext = createRootContext(servletContext);
        configureSpringMvc(servletContext, rootContext);

        FilterRegistration.Dynamic corsFilter = servletContext.addFilter("corsFilter", SimpleCORSFilter.class);
        corsFilter.addMappingForUrlPatterns(null, false, "/*");
    }

有关详细信息,请参阅此处:http://jpgmr.wordpress.com/2013/12/12/cross-origin-resource-sharing-cors-requests-with-spring-mvc/