L'API REST funziona senza metodi di autenticazione. Ora volevo autenticare l'API REST con l'autenticazione OAuth2 per le richieste API tramite l'applicazione mobile. Ho provato con la guida di yii2, ma non ha funzionato per me.Yii 2 API RESTful autenticare con OAuth2 (modello avanzato Yii 2)
in pratica l'utente mobile deve effettuare il login con nome utente & password, se un nome utente e una password sono corretti, l'utente deve essere il login e la richiesta dell'altra API deve essere convalidata con il token.
Devo creare un client OAuth 2 personalizzato come questo? Creating your own auth clients
campo access_token nella tabella utente è vuoto. devo salvarlo manualmente? come restituire access_token come risposta?
c'è qualche motivo per l'utente tutti e tre i metodi (HttpBasicAuth, HttpBearerAuth, QueryParamAuth) in una sola volta, perché? Come?
la struttura della cartella dell'applicazione è simile a quella riportata di seguito.
api
-config
-modules
--v1
---controllers
---models
-runtime
-tests
-web
backend
common
console
environments
frontend
api \ moduli \ v1 \ Module.php
namespace api\modules\v1;
class Module extends \yii\base\Module
{
public $controllerNamespace = 'api\modules\v1\controllers';
public function init()
{
parent::init();
\Yii::$app->user->enableSession = false;
}
}
api \ moduli \ v1 \ \ controllori CountryController.php
namespace api\modules\v1\controllers;
use Yii;
use yii\rest\ActiveController;
use common\models\LoginForm;
use common\models\User;
use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBasicAuth;
use yii\filters\auth\HttpBearerAuth;
use yii\filters\auth\QueryParamAuth;
/**
* Country Controller API
*
* @author Budi Irawan <[email protected]>
*/
class CountryController extends ActiveController
{
public $modelClass = 'api\modules\v1\models\Country';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['authenticator'] = [
//'class' => HttpBasicAuth::className(),
'class' => CompositeAuth::className(),
'authMethods' => [
HttpBasicAuth::className(),
HttpBearerAuth::className(),
QueryParamAuth::className(),
],
];
return $behaviors;
}
}
modelli \ Common \ User.php
namespace common\models;
use Yii;
use yii\base\NotSupportedException;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_ACTIVE = 10;
public static function tableName()
{
return '{{%user}}';
}
public function behaviors()
{
return [
TimestampBehavior::className(),
];
}
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
];
}
public static function findIdentity($id)
{
return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
}
tabella utente
id
username
auth_key
password_hash
password_reset_token
email
status
created_at
access_token
access_token è stato aggiunto dopo la tabella utente migrate
hai trovato una soluzione? – bdart
access_token deve essere generatet manualmente (ad esempio, sha1 (uniqid())). Fondamentalmente, dopo che il login (username e password) ha ricevuto il token di accesso, e lo usa in uno dei tre metodi (HttpBasicAuth, HttpBearerAuth, QueryParamAuth) in ogni altra richiesta al server. – Tahiaji