AngularJS和位于另一個(gè)域的Jersey Webservice之間的通信無(wú)法訪問正確的會(huì)話
根據(jù)您使用的AngularJS版本,您可能必須在每個(gè)$ http上進(jìn)行設(shè)置。
從1.2開始,您可以執(zhí)行以下操作:
$http.get(url,{ withCredentials: true, ...})
從1.1.1起,您可以對(duì)其進(jìn)行全局配置:
config([’$httpProvider’, function($httpProvider) { $httpProvider.defaults.withCredentials = true;}]).
如果您使用的是Angular的舊版本,請(qǐng)嘗試將配置對(duì)象傳遞給指定withCredentials的$ http。應(yīng)該可以在1.1之前的版本中使用:
$http({withCredentials: true, ...}).get(...)
另請(qǐng)參閱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。我已經(jīng)在Jersey上構(gòu)建了一個(gè)Web服務(wù),并在Glassfish上部署了該項(xiàng)目。因?yàn)槲倚枰撤N身份驗(yàn)證,并且需要OAuth實(shí)現(xiàn)或JDBCRealm,所以我決定僅在用戶成功登錄后才創(chuàng)建會(huì)話。
@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網(wǎng)頁(yè)發(fā)布到/login,這似乎還可以,我確實(shí)獲得了正確的用戶名,并且已經(jīng)建立了會(huì)話。然后,如果我向/ ping發(fā)送GET請(qǐng)求,則確實(shí)會(huì)獲得我登錄時(shí)使用的用戶名。
我已經(jīng)在需要登錄的node.jsWeb服務(wù)器上部署了AngularJS應(yīng)用程序。因?yàn)樵摲?wù)器位于另一個(gè)域上的另一個(gè)端口上,所以我不得不經(jīng)歷啟用cors的痛苦。我通過構(gòu)建一個(gè)設(shè)置響應(yīng)頭的容器響應(yīng)過濾器來(lái)做到這一點(diǎn)。
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; }}
這確實(shí)使我能夠從AngularJS向玻璃魚上部署的Java EE 6應(yīng)用程序發(fā)送不同類型的HTTP請(qǐng)求。
問題是,當(dāng)我從AngularJS向/ login方法發(fā)送POST請(qǐng)求時(shí),創(chuàng)建了一個(gè)會(huì)話,并且確實(shí)獲得了我的用戶名。但是,當(dāng)我向/ping方法發(fā)送GET請(qǐng)求時(shí),收到“未設(shè)置用戶名屬性的會(huì)話”通知。
我認(rèn)為這與跨域防護(hù)有關(guān),并且在發(fā)送xhr請(qǐng)求時(shí)必須設(shè)置withCredentials標(biāo)記。我一直在嘗試在AngularJS中執(zhí)行此操作,但尚未找到如何執(zhí)行此操作。
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); }); };};
在另一個(gè)控制器中:
$scope.getUsername = function() { $http.get('glassfish:otherport/api/ping',{}).success(function(data) { $scope.username = data;}).error(function() { $scope.username = 'error';}) }
我試圖將withCredentials設(shè)置為true
$http.defaults.withCredentials = true;
但是,這并不能解決我的問題。我還嘗試將它與config參數(shù)中的每個(gè)請(qǐng)求一起發(fā)送,但這也不能解決我的問題。
相關(guān)文章:
1. javascript - sublime快鍵鍵問題2. javascript - immutable配合react提升性能?3. css - 寫頁(yè)面遇到個(gè)布局問題,求大佬們幫解答,在線等,急!~4. javascript - nodejs關(guān)于進(jìn)程間發(fā)送句柄的一點(diǎn)疑問5. Apache 已經(jīng)把網(wǎng)站根目錄的改為allow from all了,但是服務(wù)器還是不能訪問?6. 實(shí)現(xiàn)bing搜索工具urlAPI提交7. 配置Apache時(shí),添加對(duì)PHP的支持時(shí)語(yǔ)法錯(cuò)誤8. vue.js - Vue 如何像Angular.js watch 一樣監(jiān)聽數(shù)據(jù)變化9. javascript - 移動(dòng)端上不能實(shí)現(xiàn)拖拽布局嗎?10. phpstudy8.1支持win11系統(tǒng)嗎?

網(wǎng)公網(wǎng)安備