2016-11-13 150 views
1

我正在開發一個移動應用程序,使用Jhipster和Jhipster-ionic以及cordova。 目前,我正在使用基於令牌的AngularJS身份驗證(Satellizer)以OAuth 2.0登錄,並且我有CROSS來源的問題。訪問控制允許源代碼405?

我也跟着上Satellizer-ionic這個例子,我做了休耕

我的配置:

.config(function($authProvider) { 
    $authProvider.httpInterceptor = false; 
    $authProvider.withCredentials = true; 

    var commonConfig = { 
     popupOptions: { 
     location: 'yes', 
     toolbar: 'yes', 
     width: window.screen.width, 
     height: window.screen.height 
     } 
    }; 

    if (ionic.Platform.isIOS() || ionic.Platform.isAndroid()) { 
     commonConfig.redirectUri = 'http://localhost:3000/'; 
    } 

    $authProvider.facebook(angular.extend({}, commonConfig, { 
     clientId: 'MyFacebookId', 
     url: 'http://localhost:8080/social/signup' 
    })); 

    $authProvider.google(angular.extend({}, commonConfig, { 
     clientId: 'Myid.apps.googleusercontent.com', 
     url: 'http://localhost:8080/social/signup' 


     })); 
     }) 
     .run(function($ionicPlatform) { 
     $ionicPlatform.ready(function() { 
      if (window.cordova && window.cordova.plugins.Keyboard) { 
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
      } 
      if (window.StatusBar) { 
      StatusBar.styleDefault(); 
      } 
     }); 
     }); 

控制器:

vm.authenticate = function(provider) { 
    $auth.authenticate(provider) 
    .then(function(res) { 
     $ionicPopup.alert({ 
     title: 'Success', 
     content: 'You have successfully logged in!' 
     }) 
     console.log("yes google login works"); 
     console.log('success', 'Welcome', 'Thanks for coming back, ' + res.user.displayName + '!'); 

    }) 
    .catch(function(error) { 
     console.log(error); 
     $ionicPopup.alert({ 
     title: 'Error', 
     content: error.message || (error.data && error.data.message) || error 
     }); 
     console.log("too bad" + error.data); 
    }); 
}; 

HTML:

<button class="btn btn-full btn-fb active db" ng-click="vm.authenticate('google')" type="submit" translate="{{'welcome.loginGoogle' |translate}}"></button> 

<button class="btn btn-full btn-fb active db" ng-click="vm.authenticate('facebook')" type="submit" translate="{{'welcome.loginFacebook' |translate}}"></button> 

但時I g加時賽此錯誤:

enter image description here

我已經取消註釋上application.yml

cors: #By default CORS are not enabled. Uncomment to enable. 
     allowed-origins: "*" 
     allowed-methods: GET, PUT, POST, DELETE, OPTIONS 
     allowed-headers: "*" 
     exposed-headers: 
     allow-credentials: true 
     max-age: 1800  

我Jhipster版本CORS是V3.5.1。

+0

你有沒有嘗試過做錯誤說什麼?您需要在您的授權域中有一個「Access-Control-Allow-Origin」標頭。 –

+0

@MattClark是的,我試過了,但它不起作用。 – foboss

+1

這是一個後端CORS問題......你所有的前端代碼都沒有意義 – charlietfl

回答

0

我做了我的代碼commonConfig.redirectUri應該是我的後端URL,這裏的解決方案的一些誤區:

.config(function($authProvider) { 
    $authProvider.httpInterceptor = false; 
    $authProvider.withCredentials = true; 

    var commonConfig = { 
     popupOptions: { 
     location: 'yes', 
     toolbar: 'yes', 
     width: window.screen.width, 
     height: window.screen.height 
     } 
    }; 

    if (ionic.Platform.isIOS() || ionic.Platform.isAndroid()) { 
    commonConfig.redirectUri = 'http://localhost:8080/sigin/google'; 
    } 

    $authProvider.google(angular.extend({}, commonConfig, { 
     clientId: 'googleAppId', 
    // url: "http://localhost:8080/sigin/google" 
    })); 
    console.log($authProvider); 
    }) 
    .run(function($ionicPlatform) { 
    console.log($ionicPlatform); 
    $ionicPlatform.ready(function() { 
     if (window.cordova && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     } 
     if (window.StatusBar) { 
     StatusBar.styleDefault(); 
     } 
    }); 
    }); 
相關問題