I have created an interceptor to set the header authorization:
.config(['$httpProvider', function($httpProvider) {
var httpAuthInterceptor = [ 'AuthStateService', function(AuthStateService) {
return {
'request': function(config) {
var user = AuthStateService.getCurrentUser();
var token = AuthStateService._getToken();
if (token) {
config.headers['Authorization'] = 'Basic' + user + ':' + token;
}
return config || $q.when(config);
}
};
}];
$httpProvider.interceptors.push(httpAuthInterceptor);
}]);
So, when I call the service below, the return of service (data) is OK 3 times (OKOKOK).
.factory("connectAPI", ['$http', 'config', 'loginAPI', function($http, config, loginAPI){
var _connect = function(callBack){
var param = {
user: loginAPI.getUser(),
pass: loginAPI.getPass()
};
$http.get(config.baseUrl + 'connect/user/' + param.user + '/pass/' + param.pass)
.success(function(data){
callBack(data);
})
.error(function(data, status, headers, config){
console.log(data, status, headers, config);
});
};
return {
connect: _connect
};
}])
My api method:
@Path("user/{user}/pass/{pass}")
@GET
@Produces({ MediaType.APPLICATION_JSON + ";charset=utf-8"})
public String getUserData(@PathParam("user") String user, @PathParam("pass") String pass) {
if (user != "" && pass != ""){
//test
return "OK"
}
return "NOK"
}
I have checked it and the api method was calling 3 times. I don't know what is happening.
When I comment the header authorization line, the api method is calling just 1 time. What is wrong?
.config(['$httpProvider', function($httpProvider) {
var httpAuthInterceptor = [ 'AuthStateService', function(AuthStateService) {
return {
'request': function(config) {
var user = AuthStateService.getCurrentUser();
var token = AuthStateService._getToken();
//if (token) {
// config.headers['Authorization'] = 'Basic' + user + ':' + token;
//}
return config || $q.when(config);
}
};
}];
$httpProvider.interceptors.push(httpAuthInterceptor);
}]);
I realized it was a problem in my web.xml, I duplicated my filter unintentionally.