jeudi 12 mars 2015

i add custom filter to solve allow cross origin and still denied the angular send only option method and doesn't send get or post

i add filter on java and trying to send request but it still get same error ::



XMLHttpRequest cannot load http://localhost:8080/CodeSnipping/registration/validate. A wildcard '*' cannot be used in the 'Access-Control-Allow-Origin' header when the credentials flag is true. Origin 'http://localhost:9000' is therefore not allowed access.



the browser send validate with request method (option)


my controller



package com.Controller.tmp;


@RestController
@RequestMapping("/registration")
public class GreetingController {

@RequestMapping(value = "/validate", method = RequestMethod.GET)
public ResponseEntity validate() {
System.out.println("HEREEEEE");
return new ResponseEntity(HttpStatus.OK);
}

}


My Filter



@Component
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", "x-requested-with");

chain.doFilter(req, resp);
}

@Override
public void destroy() {}

}


web.xml



<filter>
<filter-name>simpleCORSFilter</filter-name>
<filter-class>
com.Controller.tmp.SimpleCORSFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>simpleCORSFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


angular.js



MainCtrl.controller('MainCtrl', function($scope, $rootScope, $cookieStore,AppService,CityRetriever) {

var loginReqobject = {
username : "ahmed",
password : "ahmed"
};

var test = function(){
var res1 = AppService.Test(loginReqobject);
}

test();

});

MainCtrl.factory('AppService', function($http , base64) {
var BasicUrl = "http://localhost:8080/CodeSnipping/registration/validate";
return {

Test : function(loginReqobject) {
var loginRequest = {
url: BasicUrl,
method: 'get',
withCredentials: true,
};
return $http(loginRequest).then(function(response){
console.log("success " + response);
return response;
}, function(response){
console.log("failed " + response);
return response;
});
}
};
});


i add config on angular



// snippingConfig is the angular module
SnippingConfig.config(function($httpProvider){
$httpProvider.defaults.useXDomain = true;
});

Aucun commentaire:

Enregistrer un commentaire