CORS过滤器在spring中使用angularjs客户端不工作 [英] CORS Filter not working in spring REST with angularjs client

查看:692
本文介绍了CORS过滤器在spring中使用angularjs客户端不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个项目巫婆包含提供json格式和angularJS客户端巫婆消费服务,这完美工作的休息服务。现在我创建一个其他web项目巫婆只包含客户端(angularjs和html视图),但我不知道如何访问我的休息服务从这个项目。我绑定CORS过滤器的春天REST,但它不工作。我不能在客户端项目(html视图)中获取数据。
我在控制台中仍然有这个错误:

I created a project witch contains spring rest service that provide json format and angularJS Client witch consume the service and that work perfectly. Now I create an other web project witch contains just the client (angularjs and html views) but I don't know how to access my rest service from this project. I tied CORS filter for spring REST but it is not working. I can't get data in the the client project (html views). I still have this error in console :

 XMLHttpRequest cannot load http://localhost/springrestprojet/rest/demande. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.

这是我在客户端项目中所做的:

this is what I have done in the client project:

service.js:

service.js :

angular.module('workflowService', ['ngResource']).
    factory('Demande', function ($resource) {
        return $resource('/rest/demande/:id', {}, {
            'save': {method:'PUT'}
        });
    });

application.js:

application.js :

angular.module('todoApp', ['workflowService']).
    config(['$routeProvider', function ($routeProvider) {
$routeProvider.
        when('/demande/list', {templateUrl:'views/demande-list.html', controller:DemandeListController}).
        when('/demande/new', {templateUrl:'views/demande-new.html', controller:DemandeNewController}).
        when('/demande/:id', {templateUrl:'views/demande-detail.html', controller:DemandeDetailController}).
        otherwise({redirectTo:'/demande/list'}); }]);

controller.js

controller.js

function DemandeListController($scope, $location, Demande) {
$scope.demandes = Demande.query();
$scope.gotoDemandeNewPage = function () {
    $location.path("/demande/new");
};
$scope.deleteDemande = function (demande) {
    demande.$delete({'id':demande.idDemande}, function () {
        $location.path('/');
    });
};}

这是我在其余项目中所做的:
SimpleCORSFilter.java

and this is what I have done in the rest project : SimpleCORSFilter.java

public class SimpleCORSFilter implements Filter {

@Override
public void init(FilterConfig arg0) throws ServletException {}

@Override
public void doFilter(ServletRequest req, ServletResponse resp,
        FilterChain chain) throws IOException, ServletException {
    // TODO Auto-generated method stub
    HttpServletResponse response=(HttpServletResponse) resp;

    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", "Origin, X-Requested-With, Content-Type, Accept");

    chain.doFilter(req, resp);
}

@Override
public void destroy() {} }


b $ b

ClassControleur.java:

ClassControleur.java :

    @RequestMapping(value = "/demande", method = RequestMethod.GET, produces = "application/json")
   public @ResponseBody List<Demande> list(HttpServletResponse response) {
       connecter();
       List<Demande> ListDemandes = new ArrayList<Demande>();
       ListDemandes = (List<Demande>)rb.getDemandeRepository().findAll() ;

      return ListDemandes  ;
   }

web.xml:

  <filter>
   <filter-name>simpleCORSFilter</filter-name>
   <filter-class>
   com.springrestprojet.controller.SimpleCORSFilter
   </filter-class>
</filter>
 <filter-mapping>
  <filter-name>simpleCORSFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

任何帮助?

推荐答案

我没有看到任何角CORS配置。将它添加到我们的 .config ,看看会发生什么。请记住注入 $ httpProvider

I don't see any angular CORS configuration. Add this to our .config and see what happens. Remember to inject $httpProvider.

$httpProvider.defaults.useXDomain = true;
$httpProvider.defaults.withCredentials = true;

delete $httpProvider.defaults.headers.common['X-Requested-With'];

这篇关于CORS过滤器在spring中使用angularjs客户端不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆