2016-11-28 1 views
0

안녕하세요 저는 이온 (각형) 앱의 나머지 클라이언트 (로그인 인증 포함)에 대해 cakephp를 설정하려고합니다.CakePHP 및 REST Api 이온 (각도) 앱

좋아, 내가 this setup tutorial 같은 CakePHP의를 구성하고, 예를 들어 나는 데이터를 얻을 :

public function projects() 
{ 

    $projects = $this->Projects->find('all'); 
    $this->set([ 
     'projects' => $projects, 
     '_serialize' => ['projects'] 
    ]); 
} 

및 이온

완벽하게이 작품에 $.http를 통해 데이터를 얻을 수 있지만 모바일 클라이언트에 대한 케이크 인증을 구성하려고 .

나는 이것을 어떻게하는지 모른다. 내 resttest 컨트롤러에서 나는 이온 애플 리케이션에 대한 세션 ID를 설정하지만 이온이 세션을 캐시하지 코드를 썼다면 내 cakePhp 코드가 잘못 생각합니다.

CakePHP의 컨트롤러 :

<?php 
namespace App\Controller; 

use App\Controller\AppController; 
use Cake\Controller\Component\RequestHandlerComponent; 
// use Cake\View\Helper\SessionHelper; 

class ResttestController extends AppController 
{ 


    public function initialize() 
    { 
     parent::initialize(); 
     $this->loadComponent('RequestHandler'); 
     $this->loadModel('Projects'); 
     $this->loadModel('Task'); 
     $this->loadModel('User'); 
     $this->viewBuilder()->layout(false); 
     $this->response->header('Access-Control-Allow-Origin', '*'); 
     $this->loadComponent('Auth', [ 
      'loginAction' => [ 
       'controller' => $this->name, 
       'action' => 'login', 
       // '_ext'=>'json' 
      ], 
      'authorize'=>['Controller'], 

     ]); 

     // Basic setup 
     $this->Auth->config('authorize', ['Controller']); 
    } 


    public function login(){ 
     header('Access-Control-Allow-Headers: Content-Type, x-xsrf-token'); 
     $this->response->header('Access-Control-Allow-Methods', '*'); 


     if($this->request->is('post')){ 


      $postdata = file_get_contents("php://input"); 
      $d = json_decode($postdata); 

      if($this->Auth->user()){ 
       $response =array("success"=>2,'msg'=>'logged After'); 
      } 

      // $d = $this->request->data; 

      if(!$d->password || !$d->login){ 
       $response = array("success"=>0,'msg'=>'n');   
      } 


      $u = $this->User->find() 
       ->where(['email'=>$d->login]) 
       ->first(); 


      if($u){ 
       $salt = $u->salt; 
       $input_password = crypt($d->password, '$2y$12$' . $salt); 
       $password = $u->password; 


       if($password == $input_password){ 

        $tok = self::getToken(); 
        $u->token = $tok; 

        $out = $this->Auth->setUser($u); 




        $response = array("success"=>1,'msg'=>'logged', 'token'=>$tok, 'out'=>$out,'sadga'=>$this->Auth->identify,'asf'=>$this->Auth,'adsafsfq'=>$d,'$this->request'=>$this->request,'$this->response'=>$this->response,'apache_request_headers '=>apache_request_headers()); 

       }else{ 
        $response = array("success"=>0,'msg'=>'n'); 
       } 


      }else{ 
       $response = array("success"=>0,'msg'=>'n'); 
      } 

     }else{ 
       $response =array("success"=>0,'msg'=>'n'); 

     } 

     $this->set([ 
      'response' => $response, 
      '_serialize' => ['response'] 
     ]); 
    } 


    private function getToken(){ 
     return crypt(sha1(md5(uniqid(rand(), true)))); 
    } 

    public function testAuth(){ 

    } 
} 

이 코드 반환 세션 및 사용자 데이터가 아니라이 일을하고 내가 생각하는 모바일 인증을위한 좋은 방법은 아닙니다. cakephp에 대한 인증에 대한 아이디어가 있습니까? 내 코드를보다 안전하게 보호하려면 어떻게해야합니까?

답변

1

우리는 애플리케이션을 백엔드 api와 프론트 엔드로 분리 할 때 백엔드를 무국적 애플리케이션으로 고려해야합니다. 즉, 인증을 위해 세션을 사용할 수 없습니다.

대신에 JWT와 같은 토큰을 반환하는 인증 끝내기/인증 및 나머지 끝점을 구현해야합니다.

cakephp2를 들어 당신이 그런 라이브러리를 찾을 수 easely 수 있습니다 https://github.com/t73biz/cakephp2-jwt-auth

대신 사용하면 인증 구성 요소를 구성 할 때 양식의 인증을. 프런트 엔드 패스 토큰과 마찬가지로 플러그인에 설명되어 있습니다.