2017-07-10 43 views
0

我剛開始使用oidc-client-js,所以我很困惑,試圖瞭解它。我有下面的問題:)。OpenID連接Java彈簧服務器CORS禁用錯誤

我已經連接到遠程web服務器運行OpenIDConnect和具體使用https://github.com/mitreid-connect/OpenID-Connect-Java-Spring-Server我已經建造的OpenID的連接 - 服務器 - web應用和正確創建的客戶端。它在虛擬主機上託管。

enter image description here

  • 我跟着這個教程 - >link鏈接的3

  • HTML文件,我修改教程我也跟着 - > here

的錯誤顯示在控制檯上:

enter image description here


所有的好,你可以下面的圖片上看到,我授權簡單的JavaScript應用程序,我與id_token相處回的access_token,儘管當我試圖調用API,繁榮我得到錯誤,我不知道爲什麼?

enter image description here


的HTML指數的代碼,我是(在情況下,它可以幫助你):

<!DOCTYPE html> 
<html> 
<head> 
    <title>JS Application</title> 
    <meta charset="utf-8" /> 
    <link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.css" /> 
    <style> 
     .main-container { 
      padding-top: 70px; 
     } 

     pre:empty { 
      display: none; 
     } 
    </style> 
</head> 
<body> 
    <nav class="navbar navbar-inverse navbar-fixed-top"> 
     <div class="container"> 
      <div class="navbar-header"> 
       <a class="navbar-brand" href="#">JS Application</a> 
      </div> 
     </div> 
    </nav> 

    <div class="container main-container"> 
     <div class="row"> 
      <div class="col-xs-12"> 
       <ul class="list-inline list-unstyled requests"> 
        <li><a href="index.html" class="btn btn-primary">Home</a></li> 
        <li><button type="button" class="btn btn-default js-login">Login</button></li> 
        <li><button type="button" class="btn btn-default js-call-api">Call API</button></li> 
        <li><button type="button" class="btn btn-danger js-logout">Logout</button></li> 
       </ul> 
      </div> 
     </div> 

     <div class="row"> 
      <div class="col-xs-6"> 
       <div class="panel panel-default"> 
        <div class="panel-heading">User data</div> 
        <div class="panel-body"> 
         <pre class="js-user"></pre> 
        </div> 
       </div> 
      </div> 

      <div class="col-xs-6"> 
       <div class="panel panel-default"> 
        <div class="panel-heading">API call result</div> 
        <div class="panel-body"> 
         <pre class="js-api-result"></pre> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 

    <script src="node_modules/jquery/dist/jquery.js"></script> 
    <script src="node_modules/bootstrap/dist/js/bootstrap.js"></script> 
    <script src="node_modules/oidc-client/dist/oidc-client.js"></script> 

    <script> 
     // helper function to show data to the user 
     function display(selector, data) { 
      if (data && typeof data === 'string') { 
       data = JSON.parse(data); 
      } 
      if (data) { 
       data = JSON.stringify(data, null, 2); 
      } 

      $(selector).text(data); 
     } 


     var settings = { 
      authority: 'http://snf-761523.vm.okeanos.grnet.gr:8080/openid-connect-server-webapp', 
      client_id: 'client', 
      client_secret: "secret", 
      user_id: "user", 
      popup_redirect_uri: 'http://localhost/jsApp/popup.html', 
      silent_redirect_uri: 'http://localhost/jsApp/silent-renew.html', 
      post_logout_redirect_uri: 'http://localhost/jsApp/index.html', 

      response_type: 'token id_token', 
      scope: 'openid profile email offline_access', 

      filterProtocolClaims: false 
     }; 

     var manager = new Oidc.UserManager(settings); 
     var user; 

     Oidc.Log.logger = console; 

     manager.events.addUserLoaded(function (loadedUser) { 
      user = loadedUser; 
      display('.js-user', user); 
     }); 

     manager.events.addSilentRenewError(function (error) { 
      console.error('error while renewing the access token', error); 
     }); 

     manager.events.addUserSignedOut(function() { 
      alert('The user has signed out'); 
     }); 

     $('.js-login').on('click', function() { 
      manager 
       .signinPopup() 
       .catch(function (error) { 
        console.error('error while logging in through the popup', error); 
       }); 
     }); 

     $('.js-call-api').on('click', function() { 
      var headers = {}; 
      if (user && user.access_token) { 
       headers['Authorization'] = 'Bearer ' + user.access_token; 
      } 

      $.ajax({ 
       url: 'http://snf-761523.vm.okeanos.grnet.gr:8080/openid-connect-server-webapp/api/tokens/access', 
       method: 'GET', 
       dataType: 'json', 
       headers: headers 
      }).then(function (data) { 
       display('.js-api-result', data); 
      }).catch(function (error) { 
       display('.js-api-result', { 
        status: error.status, 
        statusText: error.statusText, 
        response: error.responseJSON 
       }); 
      }); 
     }); 

     $('.js-logout').on('click', function() { 
      manager 
       .signoutRedirect() 
       .catch(function (error) { 
        console.error('error while signing out user', error); 
       }); 
     }); 
    </script> 
</body> 
</html> 

回答

0

有一個臨時的解決方案僅用於測試您的應用程序,而不是用於生產堅韌,禁用谷歌瀏覽器上的網絡安全

首先你需要殺死所有CHROMOME的實例 ñ安裝名爲CORS切換 一個插件最後運行從終端或Commant提示

波紋管commants對於Windows .\chrome --args --disable-web-security --user-data-dir

對於Ubuntu Linux操作系統(僅測試存在) /opt/google/chrome/google-chrome --args --disable-web-security --user-data-dir