èªå®¶è£œã»ãã·ã§ã³å®è£ ãªãã·ã§ã³
ãã以åã¯ãPHPã§èšè¿°ãããAPIå®è£ ãããªãèŠãããšããããŸããããPHPã«çµã¿èŸŒãŸããã»ãã·ã§ã³ã¡ã«ããºã ã䜿çšãããå®è£ ã¯èŠãããšããããŸããã§ããã ç§ãäž»ã«åºäŒã£ãã®ã¯ãèªå®¶è£œã»ãã·ã§ã³ã®å®è£ ã§ããã ã»ãšãã©ã®å Žåã次ã®ããã«ãªããŸããã
1.ã¯ã©ã€ã¢ã³ãã¯ãèªèšŒããŒã¿ãšãšãã«ãµãŒããŒã«ãªã¯ãšã¹ããéä¿¡ããŸãã
2.èªèšŒã«æåããå ŽåããµãŒããŒã¯äžæã®èå¥åïŒã©ã³ãã ããã·ã¥ïŒãçæããã¹ãã¬ãŒãžïŒããŒã¿ããŒã¹ããã£ãã·ã¥ãªã©ïŒã«ä¿åãããã®èå¥åã«ã¯ã©ã€ã¢ã³ãã®ã¡ã³ããŒã·ããã«é¢ããæ å ±ãèšé²ãããµãŒããŒãžã®æåŸã®åŒã³åºãã®æéãèšé²ããŸãã ãã®åŸããã®èå¥åãå«ãå¿çãã¯ã©ã€ã¢ã³ãã«éä¿¡ããŸãã
3.ã»ãã·ã§ã³èå¥åãåä¿¡ãããããªãèŠæ±ã®ããã«ãããä¿åããã¯ã©ã€ã¢ã³ãã¯ãããŒã¿ãåä¿¡ããããã«ãéä¿¡ãããã»ãã·ã§ã³èå¥åãïŒãã©ã¡ãŒã¿ãŸãã¯ããããŒãšããŠïŒãµãŒããŒã«èŠæ±ãéä¿¡ããŸãã
4.ã»ãã·ã§ã³èå¥åã確èªãããµãŒããŒã¯ãã¯ã©ã€ã¢ã³ãã«ããŒã¿ãæäŸãããã®èå¥åã§ãµãŒããŒãžã®æåŸã®åŒã³åºãã®æå»ãæŽæ°ããŸãã
ãã©ã°ã©ã3ããã³4ã®ã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®çžäºäœçšã¯ããµãŒããŒäžã®ã»ãã·ã§ã³ã¬ã³ãŒããç Žæ£ããããŸã§çºçããŸãã ã»ãã·ã§ã³ãç Žæ£ãããå Žåãã¹ããã3ããã³4ã«é²ãåã«ãã¹ããã1ããã³2ãå床å®è¡ããå¿ èŠããããŸããå®æçã«ããµãŒããŒã«ã¢ã¯ã»ã¹ããæåŸã®ã»ãã·ã§ã³èå¥åã確èªãã䜿çšããŠããã»ãã·ã§ã³ã¹ãã¢ãã¬ã³ãŒããèªåçã«åé€ã§ããªãå Žåã¯ãåç¶æéãè¶ ãããã®ãåé€ããå¿ èŠããããŸãäžãããã寿åœã®éã ãã®æ¹æ³ã§ã¯ãå®è£ ãå¿ èŠãªã¢ã¯ã·ã§ã³ãããªããããŸãã
æšæºã®PHPã»ãã·ã§ã³ã䜿çšãããªãã·ã§ã³
ãŸããæšæºã®PHPã»ãã·ã§ã³ã䜿çšãããšäœãåŸãããŸããïŒ
1ïŒäžæã®ã»ãã·ã§ã³èå¥åã®èªåçæã
2ïŒã»ãã·ã§ã³ã«ä¿åãããããŒã¿ãžã®ã¢ã¯ã»ã¹ãããã³ã¢ããªã±ãŒã·ã§ã³å ã®ã©ãããã§ããããã®ç®¡çã
3ïŒã©ãããŒãå«ãã»ãã·ã§ã³ã®æäœã«æšæºã®PHPé¢æ°ã䜿çšããã ããšãã°ã Yiiãã¬ãŒã ã¯ãŒã¯ã®ã¯ã©ã¹CHttpSession ã
4ïŒä»¥åã«ä¿åãããç°å¢ã®èªå埩å ã ããšãã°ã以åã«äœæãããã»ãã·ã§ã³ã®èå¥åãåä¿¡ãããšãã®èªåãŠãŒã¶ãŒãã°ã€ã³ã
5ïŒã©ã€ãã¿ã€ã ãçµäºããã»ãã·ã§ã³ãèªåçã«åé€ããŸãã
CookieããŒã¹ã®ã»ãã·ã§ã³ã®ä»çµã¿ãèŠãŠã¿ãŸãããã
1.ãã©ãŠã¶ãŒã¯ãæå®ãããURLã§æ å ±ã®èŠæ±ããµãŒããŒã«éä¿¡ããŸãã
2.ãµãŒããŒã¯ãããããŒã«ãSet-Cookieããšããå¿çãè¿ããŸããããã¯ãCookieã«ã»ãã·ã§ã³IDãèšé²ããå¿ èŠãããããšããã©ãŠã¶ãŒã«äŒããŸãã ãSet-CookieãããããŒã®äŸïŒ
Set-Cookie: PHPSESSID=p2799jqivvk8gnruif1lvtv5l5; path=/
3.ã»ãã·ã§ã³èå¥åãCookieã«æ£åžžã«èšé²ãããã©ãŠã¶ã¯ãæ°ããURLã®ãªã¯ãšã¹ããéä¿¡ããŸãããããããŒã¯ãCookieãã§ãã
Cookie: PHPSESSID=p2799jqivvk8gnruif1lvtv5l5
4.ãµãŒããŒã¯ãã©ãŠã¶ã«ããŒãžãæäŸããŸãã
ãã©ãŠã¶ããã®ä»¥éã®ãã¹ãŠã®ãªã¯ãšã¹ãã«ã¯ãã»ãã·ã§ã³èå¥åã«é¢ããæ å ±ãå«ããCookieããšããèŠåºããä»ããŠããŸãã Cookieãç¡å¹ã«ãªã£ãŠããªãå Žåããããã¯ãã¹ãŠãã©ãŠã¶ãŒã§èªåçã«æ©èœããŸãã ããããCookieãç¡å¹ã«ãªã£ãŠããå ŽåããŸãã¯ãã©ãŠã¶ãã¯ã©ã€ã¢ã³ãã§ã¯ãªãå Žåã¯ã©ãã§ããããïŒ ãã®å Žåããã¹ãŠãããã»ã©åçŽã§ã¯ãããŸããã ãã¡ãããã¯ã©ã€ã¢ã³ãåŽã§ãSet-CookieãããããŒãšãCookieãããããŒã®åä¿¡ãšéä¿¡ã䜿çšã§ããŸããã次ã«ç€ºããã®åé¡ã®å¥ã®è§£æ±ºçãèŠãŠã¿ãŸãããã
APIã§PHPã»ãã·ã§ã³ã䜿çšãã
ã»ãã·ã§ã³ã®äœ¿çšãéå§ããåã«ãã»ãã·ã§ã³ã«é¢é£ããphp.iniã®ãã©ã¡ãŒã¿ãŒã«æ³šæããå¿ èŠããããŸãã 次ã®ãã©ã¡ãŒã¿ãŒã«ç¹ã«æ³šæããŠãã ããïŒ session.use_cookies ã session.use_only_cookies ã session.use_trans_sid APIã®PHPã»ãã·ã§ã³ã¡ã«ããºã ã®äœ¿çšãéå§ããã«ã¯ããããã®ãã©ã¡ãŒã¿ãŒã次ã®ããã«æ§æããå¿ èŠããããŸãã
session.use_cookies = 0 session.use_only_cookies = 0 session.use_trans_sid = 1 session.name = session
ãã¡ããããããã®èšå®ãphp.iniã§çŽæ¥èšå®ããå¿ èŠã¯ãããŸãããPHPé¢æ°ini_setã䜿çšããŠèšå®ããã ãã§ååã§ãã ãããã®èšå®ã§ã¯ãCookieã䜿çšããŠã¯ã©ã€ã¢ã³ãåŽã§èå¥åãä¿åããæ©èœãç¡å¹ã«ããŸããããã¯ããã©ãŠã¶ã ãã§ãªããä»ã®ã¢ããªã±ãŒã·ã§ã³ãã¢ãã€ã«ããã€ã¹ãªã©ã§ãAPIã䜿çšããããšãæå³ããããã§ãã session.use_trans_sidãã©ã¡ãŒã¿ãŒãæå¹ã«ãããšãã»ãã·ã§ã³IDãGETãŸãã¯POSTãã©ã¡ãŒã¿ãŒãšããŠæž¡ãããšãã§ããŸãã REST APIãéçºããå ŽåãRESTãPUTãDELETEãªã©ã®ã¡ãœããã䜿çšãããããã»ãã·ã§ã³ãã©ã¡ãŒã¿ãŒãæž¡ããšæ©èœããªããããPOSTãã©ã¡ãŒã¿ãŒãä»ããŠèå¥åãæž¡ãããšã¯æé©ãªãªãã·ã§ã³ã§ã¯ãããŸããã ãããã£ãŠãREST APIã®ã©ã®ã¡ãœããã§ãæ©èœããGETãã©ã¡ãŒã¿ãŒãšããŠèå¥åãæž¡ãããšããå§ãããŸãã ãŸããsession.nameãã©ã¡ãŒã¿ãŒã«GETãã©ã¡ãŒã¿ãŒã®ååãèšå®ããŸããããã¯ãããã©ã«ãã§PHPSESSIDãšåŒã°ããŸãã æž¡ãããã»ãã·ã§ã³èå¥åãæã€URLã¯æ¬¡ã®ããã«ãªããŸãã
https://api.example.com/action?session=l2kkl7c9sm2dfedr767itc9966
Yiiãã¬ãŒã ã¯ãŒã¯ã§ã®PHPã»ãã·ã§ã³ã®äœ¿çš
ã§ã¯ããã®ã¡ã«ããºã ãYiiãã¬ãŒã ã¯ãŒã¯ã§ã©ã®ããã«äœ¿çšã§ããããèŠãŠã¿ãŸãããã ã»ãã·ã§ã³ãæäœããããã«ãYiiã¯CHttpSessionã¯ã©ã¹ãæäŸããŸãã ããã䜿çšããã«ã¯ãæ§æãã¡ã€ã«ã®ã³ã³ããŒãã³ãé åã«æ¬¡ã®èšå®ãæžã蟌ãå¿ èŠããããŸãã
'session' => array( 'autoStart' => true, 'cookieMode'=>'none', 'useTransparentSessionID' => true, 'sessionName' => 'session', 'timeout' => 28800, ),
ã©ãã§
'cookieMode' => 'none'ã¯php.inièšå®ãsession.use_cookies = 0ããã³session.use_only_cookies = 0ã«èšå®ããŸã
'useTransparentSessionID' => trueã¯ãphp.iniãsession.use_trans_sid = 1ã«èšå®ããŸã
ããŸãå€ãã®åŒã³åºããè¡ããªãAPIã®å Žåãããã§ååã§ãããããã©ã«ãã§ã¯ãã»ãã·ã§ã³ã¯ããã¬ãŒã³ããã¹ãããã¡ã€ã«ãšããŠãã£ã¹ã¯ã«ä¿åãããŸããããã¯ãè² è·ã®é«ãAPIã§éäžçã«ã»ãã·ã§ã³ãèªã¿æžãããå Žåã®åŒ±ããªã³ã¯ã«ãªãå¯èœæ§ããããŸã ãã®å Žåã次ã®ããããã®ãœãªã¥ãŒã·ã§ã³ã䜿çšã§ããŸãã
1ïŒãã©ã€ããSSDã«äº€æããŸãã
2ïŒSSDãã£ã¹ã¯ããã¬ãã«10ã®RAIDãé 眮ããŸãã
3ïŒRAMãã£ã¹ã¯ã䜿çšããŸãã ããšãã°ãLinuxã®Tmpfsãã¡ã€ã«ã·ã¹ãã ã
4ïŒ Memcachedã«ã»ãã·ã§ã³ãä¿åïŒRAMã«ããŒã¿ãä¿åïŒ;
5ïŒã»ãã·ã§ã³ãRedisã«ä¿åããïŒããŒã¿ãRAMã«ä¿åããïŒã
Redisã®ã»ãã·ã§ã³ã¹ãã¬ãŒãž
å€æ§ãªã¹ãã¬ãŒãžæ§é ã®ãããRedisã«çŠç¹ãåœãŠãããšæããŸãã ãŸãããµãŒããŒã®åèµ·ååŸã®ããŒã¿ãªã«ããªïŒãã®å Žåã¯ã»ãã·ã§ã³ïŒã®éèŠãªå¯èœæ§ã«ã泚æããŠãã ããã ã»ãã·ã§ã³ãªããžããªãšããŠRedisã䜿çšããåã«ã RedisãµãŒããŒãšRedisçš PHPæ¡åŒµæ©èœãã€ã³ã¹ããŒã«ããå¿ èŠããããŸã ã ãããã®äž¡æ¹ãã€ã³ã¹ããŒã«ããæ¹æ³ã¯ããã«ãããŸã ã ã€ã³ã¹ããŒã«ãæ£åžžã«å®äºãããšãRedisã®PHPæ¡åŒµæ©èœããPHPã»ãã·ã§ã³ãã³ãã©ãŒã䜿çšã§ããããã«ãªããŸãã php.iniãçŽæ¥å€æŽããã«Redis PHP Sessionãã³ãã©ãŒã䜿çšããYiiæ§æã§Redisãã»ãã·ã§ã³ãªããžããªãšããŠèšå®ã§ããããã«ããã«ã¯ãCHttpSessionãå°ãå€æŽãããããç¶æ¿ããŠç¬èªã®RedisSessionManagerã¯ã©ã¹ãäœæããå¿ èŠããããŸããã
ããã§ãã»ãã·ã§ã³ã³ã³ããŒãã³ãã®èšå®ã¯æ¬¡ã®ããã«ãªããŸãã
'session' => array( 'class' => 'application.components.RedisSessionManager', 'autoStart' => true, 'cookieMode'=>'none', 'useTransparentSessionID' => true, 'sessionName' => 'session', 'saveHandler'=>'redis', 'savePath' => 'tcp://localhost:6379?database=10&prefix=session::', 'timeout' => 28800, ),
APIã§ã®æ¿èªã®ããã®ã»ãã·ã§ã³ã®äœ¿çš
ã»ãã·ã§ã³ã䜿çšããŠãAPIã§ãŠãŒã¶ãŒãèªèšŒã§ããããã«ãªããŸããã ããã¯æ¬¡ã®ããã«å®è¡ã§ããŸãã
ãã°ã€ã³æ¹æ³ïŒ
public function actionLogin() { $params = $this->getRequestParams(); $identity=new UserIdentity($params['username'],$params['password']); if($identity->authenticate()){ $this->sendResponse(Status::OK, array( 'session'=>Yii::app()->session->getSessionID(), 'message'=>'Successful login', )); }else{ $this->sendResponse(Status::UNAUTHORIZED, $identity->errorMessage); } }
ããã§äœãèµ·ãã£ãŠããŸããïŒ ãŸãããªã¯ãšã¹ãããååŸãããŠãŒã¶ãŒåãšãã¹ã¯ãŒããååŸããŸãã 次ã«ããã®ãŠãŒã¶ãŒåãšãã¹ã¯ãŒãã䜿çšããŠãã°ã€ã³ãããã°ã€ã³ã«æåããå Žåã¯ãä»ã®APIã¡ãœããã«ã¢ã¯ã»ã¹ãããšãã«äœ¿çšããããã«ã»ãã·ã§ã³IDãè¿ããŸãã
ããã¯ãUserIdentityã¯ã©ã¹ã®å€èŠ³ã§ãã
class UserIdentity extends CUserIdentity { public function authenticate() { $account = Yii::app()->account->getByName($this->username); $password = Yii::app()->account->hashPassword($this->password); if(!$account || $this->username !== $account->username){ $this->errorCode = self::ERROR_USERNAME_INVALID; $this->errorMessage = 'User with username '.$this->username.' not found'; return false; } else if ($password !== $account->password) { $this->errorCode = self::ERROR_PASSWORD_INVALID; $this->errorMessage = 'Wrong password'; return false; } else { $this->errorCode = self::ERROR_NONE; Yii::app()->user->login($this); Yii::app()->user->setId($account->id); Yii::app()->user->setName($account->nickname); return true; } } }
èªèšŒã«æåãããšããŠãŒã¶ãŒæ å ±ããŠãŒã¶ãŒã³ã³ããŒãã³ãã«å ¥åãããŸãããŠãŒã¶ãŒæ å ±ã¯ãæå®ãããã»ãã·ã§ã³èå¥åãæã€APIãžã®æ¬¡åã®åŒã³åºãã§èªåçã«çœ®ãæããããŸãã
ãã°ã¢ãŠãæ¹æ³ïŒ
public function actionLogout() { if(Yii::app()->session->destroySession()){ $this->sendResponse(Status::OK, 'Successful logout'); }else{ $this->sendResponse(Status::BAD_REQUEST, 'Logout was not successful'); } }
ããã§ã¯ãã¹ãŠãç°¡åã§ãã çŸåšã®ã»ãã·ã§ã³ãšãã®ãã¹ãŠã®ã³ã³ãã³ããç Žæ£ããã ãã§ãã
ãŸããAPIã§ã»ãã·ã§ã³ã䜿çšããããã®ãã³ããããã€ã玹ä»ããŸãã
1ïŒã¯ã©ã€ã¢ã³ããšãµãŒããŒéã®éä¿¡ã«ã¯æå·åãããæ¥ç¶ã䜿çšããããšãéèŠã§ããããã«ãããæ»æè ãã»ãã·ã§ã³èå¥åãååããŠãã以äžäœ¿çšã§ããªãããã«ããããšãã§ããŸãã ããšãã°ãHTTPSãããã³ã«ã䜿çšã§ããŸãã
2ïŒã»ãã·ã§ã³ãããã§ãæ»æè ã«ãã£ãŠååãããå Žåãè¿œå ã®ã»ãã·ã§ã³èªèšŒã¢ã«ãŽãªãºã ã䜿çšã§ããŸãã ããšãã°ãã»ãã·ã§ã³ããŠãŒã¶ãŒã®IPã«ãã€ã³ãããããã«ã»ãã·ã§ã³å ã«IPãä¿æãã次ã®åŒã³åºãäžã«IPãå€æŽããããã©ããã確èªããŸãã 以åã«ä¿åããIPãçŸåšã®IPãšäžèŽããªãå Žåãã»ãã·ã§ã³ãç Žæ£ããå¿ èŠããããŸãã
3ïŒã»ãã·ã§ã³ã®åç¶æéã«å¶éãèšããŸãã ãã®æéã¯ãAPIãžã®æ¬¡ã®ãªã¯ãšã¹ãã§èªåçã«æŽæ°ããããããããšãã°2æéã«èšå®ããŸãã ãããã£ãŠããŠãŒã¶ãŒã2æéã¢ã¯ãã£ãã§ãªãå Žåãã»ãã·ã§ã³ã¯èªåçã«ç Žæ£ãããŸãã ããã«ãããã»ãã·ã§ã³ã¹ãã¬ãŒãžããªãŒããŒãããŒããå¯èœæ§ãäœããªããŸãã
æåŸã«ãYiiã§èšè¿°ãããREST APIã§ã®èªèšŒãRedisã§ã®ã»ãã·ã§ã³ã®ä¿åãšã©ã®ããã«æ©èœãããã«ã€ããŠã®çããã¢ãããªã
èšäºã®èè ïŒ luxurydab