这两天一直想找个机会做一下API的身份验证,就像微博那样提供接口给别人用,但又有所限制,也不会导致接口滥用。 大概一年半之前,写了个大学英语四六级成绩查询的接口(由于历史原因,此Github帐号不再使用了,新的在这里),托管在新浪云,放到了网上,也没有加任何限制,结果被一个人短时间内多次调用,真的是非常频繁,浪费了不少云豆。现在正好可以用之前写的成绩查询接口来做这个身份验证的实验。
在做一个二维码签到/点名系统时,需要后台同时支持移动端、PC端和网页版,因此决定写成接口,这样比较方便。既然写成接口,就写的规范一些咯,之前自己写的小玩意实在是拿不出手,毫无规范可言。了解到RESTful API,查了一些资料,主要看了这篇, 写的很不错。然后就去找个框架呗。 在写二维码签到/点名系统时,用的是CI框架,也有第三方的REST库, 但用的很不爽,说不上来的不得劲。经过查询,知道了slim这个框架,是专门构建RESTful API的框架。之后就开始了一天的折腾。
官方推荐使用composer进行安装,下面不说废话了,Come on Install composer Slim and some third plugins
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer // install composercomposer require slim/slim "^3.0" // install Slimcomposer require tuupola/slim-basic-auth "^2.0" // install slim-basic-authcomposer require lcobucci/jwt "^3.1" // install jwtcomposer require tuupola/slim-jwt-auth "^2.0" // install slim-jwt-auth
啰嗦一句,windowns上面进行开发比较麻烦,建议装个虚拟机跑ubuntu/cenos或者你喜欢的发行版
需要注意的是,当前(2015年12月21日)时间,slim最新版本是3.0 开始之前我找了一些网上别人写的中文入门之类的博文,但大多是2.x, 会有一些坑(不禁想起了Python的版本, o(︶︿︶)o ).
根据我已经写完了的V1的示例代码来分析/学习 index.php: https://github.com/xu42/API/blob/master/index.php cet_score.php: https://github.com/xu42/API/blob/master/v1/cet_score/cet_score.php
https://ip/token
发起GET 请求, 后台生成”access_token”。在Github上查看代码
123456789101112131415161718$app->get("/token", function(ServerRequestInterface $request, ResponseInterface $response, $arguments) use ($app) { if(!$request->hasHeader('key')){ return $response->withStatus(401); } $access_token = (new Builder())->setIssuer('https://api.xu42.cn') // Configures the issuer (iss claim) ->setAudience('https://api.xu42.cn') // Configures the audience (aud claim) ->setId($request->getHeaderLine('key'), true) // Configures the id (jti claim), replicating as a header item ->setIssuedAt(time()) // Configures the time that the token was issue (iat claim) ->setNotBefore(time()+60) // Configures the time that the token can be used (nbf claim) ->setExpiration(time()+3600) // Configures the expiration time of the token (exp claim) ->set('scope', ['read']) // Configures a new claim, called "scope" ->sign(new \Lcobucci\JWT\Signer\Hmac\Sha256(), 'cn.xu42.api') // ALGORITHM HS256 ->getToken(); // Retrieves the generated token $response = $response->withStatus(200); $response = $response->withHeader('Content-type', 'application/json'); $response->getBody()->write(json_encode(['access_token' => (string) $access_token, 'token_type' => 'bearer', 'expires_in' => $access_token->getClaim('exp') - $access_token->getClaim('iat')])); return $response;}); 整个流程就是这样,不难,代码量也不大,第一次完成了还像样的接口,还是挺高兴的。但也明白,这跟真实企业级的接口比,差的还很远,继续努力。
参考资料