{"id":258061,"date":"2015-05-27T14:28:02","date_gmt":"2015-05-27T10:28:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=258061"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=258061","title":{"rendered":"\u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u043e\u043a\u0435\u043d\u0430 \u0434\u043b\u044f API"},"content":{"rendered":"<p>     \t\u041a\u0430\u043a \u0438 \u043e\u0431\u0435\u0449\u0430\u043b \u0440\u0430\u043d\u0435\u0435, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0432\u043e\u044e \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 API \u043d\u0430 Symfony2. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0418\u0437 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0430\u043d\u0434\u043b\u043e\u0432 \u0435\u0441\u0442\u044c JWTAuthenticationBundle \u0438 FOSOAuthServerBundle, \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b, \u043d\u043e \u043c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0430\u043c\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<a name=\"habracut\"><\/a><\/p>\n<p>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c UserAccessToken, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d\u044b \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439.   <\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\CommonBundle\\Entity;  use Doctrine\\ORM\\Mapping AS ORM;  \/**  * @ORM\\Entity  * @ORM\\Table(name=&quot;user_access_tokens&quot;)  *\/ class UserAccessToken {     \/**      * @ORM\\Id      * @ORM\\GeneratedValue(strategy=&quot;AUTO&quot;)      * @ORM\\Column(name=&quot;id&quot;, type=&quot;integer&quot;)      *\/     protected $id;      \/**      * @ORM\\ManyToOne(targetEntity=&quot;User&quot;)      * @ORM\\JoinColumn(name=&quot;user_id&quot;, referencedColumnName=&quot;id&quot;, onDelete=&quot;SET NULL&quot;)      *\/     protected $user;      \/**      * \u0422\u0443\u0442 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u043d\u0430\u0448 \u0442\u043e\u043a\u0435\u043d. \u0422\u043e\u043a\u0435\u043d \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c\u043e\u043c\u0443 \u0438 \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435 \u0438 \u0434\u043b\u0438\u043d\u043d\u0435\u0435, \u0447\u0442\u043e\u0431\u044b \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043f\u043e\u0434\u0431\u043e\u0440\u0430      *       * @ORM\\Column(name=&quot;access_token&quot;, type=&quot;string&quot;)      *\/     protected $accessToken;      \/**      * \u0414\u0430\u0442\u0430, \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0442\u043e\u043a\u0435\u043d \u0431\u0443\u0434\u0435\u0442 \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u043d\u0435 \u0430\u043a\u0442\u0438\u0432\u043d\u044b\u043c      *       * @ORM\\Column(name=&quot;expired_at&quot;, type=&quot;datetime&quot;)      *\/     protected $expiredAt;      \/**      * @ORM\\Column(name=&quot;created_at&quot;, type=&quot;datetime&quot;)      *\/     protected $createdAt;  } <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c Listener, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0441\u043b\u0443\u0448\u0430\u0442\u044c \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0432\u0430\u0448\u0435\u043c\u0443 API \u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/p>\n<pre><code class=\"php\">&lt;?php  namespace App\\CommonBundle\\Listener;  use App\\CommonBundle as Common; use Doctrine\\ORM\\EntityManager; use Symfony\\Component\\HttpFoundation\\JsonResponse; use Symfony\\Component\\HttpKernel\\Event\\GetResponseEvent; use Symfony\\Component\\Security\\Core\\SecurityContextInterface; use Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken;  class AccessTokenListener {     private $entityManager;     private $securityContext;     private $exclude = [         '\/users\/login', '\/users\/registration',     ];      const EMPTY_ACCESS_TOKEN = 'empty_access_token';     const INVALID_ACCESS_TOKEN = 'invalid_access_token';     const ACCESS_TOKEN_EXPIRED = 'access_token_expired';      public function __construct(EntityManager $entityManager, SecurityContextInterface $securityContext)     {         $this-&gt;entityManager = $entityManager;         $this-&gt;securityContext = $securityContext;     }      \/**      * @return Common\\Entity\\UserAccessToken      *\/     private function getByAccessToken($accessToken)     {         return $this-&gt;entityManager-&gt;getRepository('CommonBundle:UserAccessToken')-&gt;findOneByAccessToken($accessToken);     }      public function beforeController(GetResponseEvent $event)     {         \/\/ \u0414\u0435\u043b\u0430\u0435\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b\u043c\u0438 \u0431\u0435\u0437 \u0442\u043e\u043a\u0435\u043d\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0434\u043b\u044f \u043d\u0430\u0441 URL         \/\/ \u042d\u0442\u043e \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043e\u0442\u043a\u0440\u044b\u0442\u044c \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u0438 \u0442\u0434         if (in_array($event-&gt;getRequest()-&gt;getPathInfo(), $this-&gt;exclude)) {             return;         }          \/\/ \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c X-Access-Token, \u0435\u0441\u043b\u0438 \u043e\u043d \u043f\u0443\u0441\u0442\u043e\u0439 \u2013 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043e\u0448\u0438\u0431\u043a\u0443         $accessToken = $event-&gt;getRequest()-&gt;headers-&gt;get('X-Access-Token');         if (!$accessToken) {             $event-&gt;setResponse(new JsonResponse(['error' =&gt; self::EMPTY_ACCESS_TOKEN], 403));             return;         }          \/\/ \u0418\u0449\u0435\u043c \u0432 \u0431\u0430\u0437\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443         $token = $this-&gt;getByAccessToken($accessToken);         if (!$token) {             $event-&gt;setResponse(new JsonResponse(['error' =&gt; self::INVALID_ACCESS_TOKEN], 403));             return;         }          \/\/ \u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0430\u043a\u0442\u0443\u0430\u043b\u0435\u043d \u043b\u0438 \u0432\u0441\u0435 \u0435\u0449\u0435 \u0442\u043e\u043a\u0435\u043d         if ($token-&gt;getExpiredAt() &lt;= new \\DateTime('now')) {             $event-&gt;setResponse(new JsonResponse(['error' =&gt; self::ACCESS_TOKEN_EXPIRED], 403));             return;         }          \/\/ \u0423\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u043c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0431\u044b\u043b \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0432 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440\u0430\u0445 \u0447\u0435\u0440\u0435\u0437 $this-&gt;getUser()         $user = $token-&gt;getUser();          $usernamePasswordToken = new UsernamePasswordToken($user, $user-&gt;getPassword(), &quot;main&quot;, $user-&gt;getRoles());         $this-&gt;securityContext-&gt;setToken($usernamePasswordToken);     } } <\/code><\/pre>\n<p>  \u0418 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043c \u0435\u0433\u043e \u0432 services.yml  <\/p>\n<pre><code class=\"bash\">    common.listener.access_token:         class: App\\CommonBundle\\Listener\\AccessTokenListener         arguments: [@doctrine.orm.entity_manager, @security.context]         tags:             - { name: kernel.event_listener, event: kernel.request, method: beforeController } <\/code><\/pre>\n<p>  \u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0432\u0441\u0435, \u0442\u0435\u043f\u0435\u0440\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u043c\u0435\u0442\u043e\u0434 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0445\u0430 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 UserAccessToken \u0441\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c accessToken \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u043e\u0442\u0432\u0435\u0442\u0435.     \t<\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/258903\/\"> http:\/\/habrahabr.ru\/post\/258903\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041a\u0430\u043a \u0438 \u043e\u0431\u0435\u0449\u0430\u043b \u0440\u0430\u043d\u0435\u0435, \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0441\u0432\u043e\u044e \u0441\u0435\u0440\u0438\u044e \u0441\u0442\u0430\u0442\u0435\u0439 \u043f\u0440\u043e \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 API \u043d\u0430 Symfony2. \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0418\u0437 \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u044b\u0445 \u0431\u0430\u043d\u0434\u043b\u043e\u0432 \u0435\u0441\u0442\u044c JWTAuthenticationBundle \u0438 FOSOAuthServerBundle, \u0443 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0435\u0441\u0442\u044c \u0441\u0432\u043e\u0438 \u043f\u043b\u044e\u0441\u044b \u0438 \u043c\u0438\u043d\u0443\u0441\u044b, \u043d\u043e \u043c\u043d\u0435 \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0430\u043c\u043e\u043c\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043d\u0438\u043c\u0430\u0442\u044c \u043a\u0430\u043a \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-258061","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/258061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=258061"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/258061\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=258061"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=258061"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=258061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}