CORS过滤器无法在spring RESTs中使用angularjs客户端

时间:2015-06-23 15:03:54

标签: angularjs spring rest jboss cors

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

 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.

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

service.js:

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

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

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

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() {} }

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>

有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

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

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

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

答案 1 :(得分:-1)

最后,我找到了一个解决方案:我只是对URL进行了一些小修改: http://localhost:8080:8080/springprojet/rest/demande/:id。  现在工作正常!