AngularJS和位于另一個域的Jersey Webservice之間的通信無法訪問正確的會話
根據您使用的AngularJS版本,您可能必須在每個$ http上進行設置。
從1.2開始,您可以執行以下操作:
$http.get(url,{ withCredentials: true, ...})
從1.1.1起,您可以對其進行全局配置:
config([’$httpProvider’, function($httpProvider) { $httpProvider.defaults.withCredentials = true;}]).
如果您使用的是Angular的舊版本,請嘗試將配置對象傳遞給指定withCredentials的$ http。應該可以在1.1之前的版本中使用:
$http({withCredentials: true, ...}).get(...)
另請參閱mruelans答案和:
https://github.com/angular/angular.js/pull/1209http://docs.angularjs.org/api/ng.$httphttps://developer.mozilla.org/zh-CN/docs/HTTP/Access_control_CORS?redirectlocale=zh-CN&redirectslug=HTTP_access_control#section_5解決方法最近,我一直在使用AngularJS和JavaEE6。我已經在Jersey上構建了一個Web服務,并在Glassfish上部署了該項目。因為我需要某種身份驗證,并且需要OAuth實現或JDBCRealm,所以我決定僅在用戶成功登錄后才創建會話。
@POST@Path('/login')@Produces({MediaType.APPLICATION_JSON})@Consumes({MediaType.APPLICATION_JSON})public Response login(LoginDAO loginData,@Context HttpServletRequest req) { req.getSession().invalidate(); loginData.setPassword(PasswordGenerator.hash(loginData.getPassword())); User foundUser = database.login(loginData); if(foundUser == null) {return Response.status(Status.CONFLICT).build(); } req.getSession(true).setAttribute('username',foundUser.getUsername()); return Response.ok().build();}@GET@Path('/ping')public Response ping(@Context HttpServletRequest req) { if(req.getSession().getAttribute('username') == null) {return Response.ok('no session with an username attribute has been set').build(); } return Response.ok(req.getSession(true).getAttribute('username')).build();}
如果我從Postman或從glassfish上部署的基本jQuery網頁發布到/login,這似乎還可以,我確實獲得了正確的用戶名,并且已經建立了會話。然后,如果我向/ ping發送GET請求,則確實會獲得我登錄時使用的用戶名。
我已經在需要登錄的node.jsWeb服務器上部署了AngularJS應用程序。因為該服務器位于另一個域上的另一個端口上,所以我不得不經歷啟用cors的痛苦。我通過構建一個設置響應頭的容器響應過濾器來做到這一點。
public class CrossOriginResourceSharingFilter implements ContainerResponseFilter { @Override public ContainerResponse filter(ContainerRequest creq,ContainerResponse cresp) {cresp.getHttpHeaders().putSingle('Access-Control-Allow-Origin','http://localhost:8000');cresp.getHttpHeaders().putSingle('Access-Control-Allow-Credentials','true');cresp.getHttpHeaders().putSingle('Access-Control-Allow-Methods','GET,POST,DELETE,PUT');cresp.getHttpHeaders().putSingle('Access-Control-Allow-Headers','Content-Type,Accept,X-Requested-With');return cresp; }}
這確實使我能夠從AngularJS向玻璃魚上部署的Java EE 6應用程序發送不同類型的HTTP請求。
問題是,當我從AngularJS向/ login方法發送POST請求時,創建了一個會話,并且確實獲得了我的用戶名。但是,當我向/ping方法發送GET請求時,收到“未設置用戶名屬性的會話”通知。
我認為這與跨域防護有關,并且在發送xhr請求時必須設置withCredentials標記。我一直在嘗試在AngularJS中執行此操作,但尚未找到如何執行此操作。
function LoginCtrl($scope,$http) { $scope.login = function() {$http.post('glassfish:otherport/api/login',$scope.credentials). success(function(data) {console.log(data); }). error(function(data,error) {console.log(error); }); };};
在另一個控制器中:
$scope.getUsername = function() { $http.get('glassfish:otherport/api/ping',{}).success(function(data) { $scope.username = data;}).error(function() { $scope.username = 'error';}) }
我試圖將withCredentials設置為true
$http.defaults.withCredentials = true;
但是,這并不能解決我的問題。我還嘗試將它與config參數中的每個請求一起發送,但這也不能解決我的問題。
相關文章:
1. docker - 各位電腦上有多少個容器啊?容器一多,自己都搞混了,咋辦呢?2. java - spring boot 如何打包成asp.net core 那種獨立應用?3. java - 在用戶不登錄的情況下,用戶如何添加保存到購物車?4. datetime - Python如何獲取當前時間5. javascript - nginx反向代理靜態資源403錯誤?6. docker網絡端口映射,沒有方便點的操作方法么?7. 安全性測試 - nodejs中如何防mySQL注入8. javascript - 關于apply()與call()的問題9. docker start -a dockername 老是卡住,什么情況?10. python - 調用api輸出頁面,會有標簽出現,請問如何清掉?
