Ionic + Jhipster oauth2 错误无访问控制

问题描述 投票:0回答:1

一直在尝试使用 Jhipsters oauth2 登录本地主机上的离子应用程序,并不断获得:

选项http://172.16.40.31:8080/oauth/token(匿名函数)@ionic.bundle.js:23826sendReq@ionic.bundle.js:23645serverRequest@ionic.bundle.js:23357processQueue@ ionic.bundle.js:27879(匿名函数) @ ionic.bundle.js:27895Scope.$eval @ ionic.bundle.js:29158Scope.$digest @ ionic.bundle.js:28969Scope.$apply @ ionic.bundle.js :29263(匿名函数) @ ionic.bundle.js:36615eventHandler @ ionic.bundle.js:16583triggerMouseEvent @ ionic.bundle.js:2948tapClick @ ionic.bundle.js:2937tapMouseUp @ ionic.bundle.js:3013 ?ionicplatform=android:1 XMLHttpRequest 无法加载 http://172.16.40.31:8080/oauth/token。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“http://localhost:8100”。响应的 HTTP 状态代码为 401。 ?ionicplatform=android:1 XMLHttpRequest 无法加载 http://172.16.40.31:8080/api/logout。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此,不允许访问源“http://localhost:8100”。

我尝试添加谷歌插件,没有改变任何东西。确保 .antMatchers(HttpMethod.OPTIONS, "/**").permitAll() 已在设置中。

我还有什么遗漏的吗?

ionic-framework token jhipster
1个回答
2
投票

尝试在 application.yml 中取消注释 cors

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

要在 ionic 中使用 Oauth2 身份验证访问 REST API,您必须首先通过

在 ionic 应用程序中获取令牌
    $http({
    method: "post", 
    url: "http://192.168.0.4:8085/[Your app name]/oauth/token",
    data:  "username=admin&password=admin&grant_type=password&scope=read write&client_secret=my-secret-token-to-change-in-production&client_id=auth2Sconnectapp",
    withCredentials: true,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Accept': 'application/json',
      'Authorization': 'Basic ' + 'YXV0aDJTY29ubmVjdGFwcDpteS1zZWNyZXQtdG9rZW4tdG8tY2hhbmdlLWluLXByb2R1Y3Rpb24='
      }
  })                
  .success(function(data) {
      alert("success: " + data);
  })
  .error(function(data, status) {
      alert("ERROR: " + data);
  });

此处“YXV0aDJTY29ubmVjdGFwcDpteS1zZWNyZXQtdG9rZW4tdG8tY2hhbmdlLWluLXByb2R1Y3Rpb24 =”等于(clientId +“:”+ clientSecret)--全部采用base64编码

您可以使用 https://www.base64encode.org/ 自行验证或重新创建它

aboue $http如果成功会给你这个JSON

{
  "access_token": "2ce14f67-e91b-411e-89fa-8169e11a1c04",
  "token_type": "bearer",
  "refresh_token": "37baee3c-f4fe-4340-8997-8d7849821d00",
  "expires_in": 525,
  "scope": "read write"
}

如果您想访问任何 API,请注意“access_token”和“token_type”,这是您必须使用的。

例如

$http({
    method: "get", 
    url: "http://192.168.0.4:8085/auth-2-sconnect/api/countries",
    withCredentials: true,
    headers: {
      'Authorization':' [token_type] + [space] + [access_token] '
      }
  })                
  .success(function(data) {
      alert("success: " + data);
  })
  .error(function(data, status) {
      alert("ERROR: " + data);
  });
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.