简体   繁体   English

Ionic + AngularJs + 基于令牌的身份验证

[英]Ionic + AngularJs + Token based authentication

I've set up a standard Ionic Framework (with ionic start myapp tabs ), and I've got a Laravel PHP backend on the other side (which has the database and so on).我已经建立了一个标准的 Ionic 框架(带有ionic start myapp tabs ),并且我在另一端有一个 Laravel PHP 后端(它有数据库等等)。

Now what I want is to ask the database of my backend from my mobile-app (it'll be a native app, compiled by PhoneGap).现在我想要的是从我的移动应用程序(它将是一个本地应用程序,由 PhoneGap 编译)询问我的后端数据库。 There's problems with CORS to be solved, it seems, and I've read a lot of approaches on how to do it, but none really worked. CORS 的问题似乎有待解决,我已经阅读了很多关于如何解决的方法,但没有一个真正奏效。

What is a good way to authenticate (token-based) to my external backend server, and then, once authenticated ask the server API for various things?对我的外部后端服务器进行身份验证(基于令牌)的好方法是什么,然后在身份验证后向服务器 API 询问各种事情? Does anyone additionally have a good tutorial and/or working example?有没有人另外有一个很好的教程和/或工作示例?

This is the tutorial may help you how to use token system with angular and node js.Coming to data base you can use mysql.When using token based authentication header should have a valid token on every end point to server.you can your feasible token generators also lik oauth2.这是本教程可以帮助您如何使用带有角度和节点 js 的令牌系统。来到数据库,您可以使用 mysql。当使用基于令牌的身份验证标头时,服务器的每个端点上都应该有一个有效的令牌。您可以使用可行的令牌生成器也喜欢 oauth2。

http://code.tutsplus.com/tutorials/token-based-authentication-with-angularjs-nodejs--cms-22543 http://code.tutsplus.com/tutorials/token-based-authentication-with-angularjs-nodejs--cms-22543

To solve CORS problem add this php code to the head of your php api要解决 CORS 问题,请将此 php 代码添加到 php api 的头部

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 166400'); 
}

I don't know whether this response can do something good right now.我不知道这个回应现在是否可以做一些好事。 But to be precise, CORS can be avoid by following below steps.但准确地说,可以通过以下步骤避免 CORS。

First create a separate php file in your server and named it as "api.php" (or whatever you prefer) and include this.首先在您的服务器中创建一个单独的 php 文件并将其命名为“api.php”(或您喜欢的任何名称)并包含它。

   <?php
       if (isset($_SERVER['HTTP_ORIGIN'])) {
           header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
           header('Access-Control-Allow-Credentials: true');
           header('Access-Control-Max-Age: 86400');    // cache for 1 day
       }

       // Access-Control headers are received during OPTIONS requests
       if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

           if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
                header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

           if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
                header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

           exit(0);
       }
   ?>

Then in you other php files which send responses to the client use然后在你发送响应给客户端的其他 php 文件中使用

   include_once './api.php'; // or the name use above

That will do the trick.这样就行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM