JWT,CakePHP 3和AngularJS的身份验证异常行为


Authentication strange behavior with JWT, CakePHP 3 and AngularJS

我在使用 CakePHP 和 JWT 进行身份验证时遇到了一个奇怪的问题。

我创建了一个测试应用程序,一切正常,而不是我创建了一个新项目(真实项目),配置所有内容相等,但出了点问题。

当我尝试请求不允许的 API 时,我收到了

{
    "message": "You are not authorized to access that location.",
    "url": "'/api'/advertisements.json",
    "code": 401
}<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr57881c28563e8-trace').style.display = (document.getElementById('cakeErr57881c28563e8-trace').style.display == 'none' ? '' : 'none');"><b>Warning</b> (2)</a>: file_put_contents(/home/erick/Projetos/ProjetoX/projetox/6busca/logs/error.log) [<a href='http://php.net/function.file-put-contents'>function.file-put-contents</a>]: failed to open stream: Permissão negada [<b>CORE/src/Log/Engine/FileLog.php</b>, line <b>134</b>]<div id="cakeErr57881c28563e8-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr57881c28563e8-code').style.display = (document.getElementById('cakeErr57881c28563e8-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr57881c28563e8-context').style.display = (document.getElementById('cakeErr57881c28563e8-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr57881c28563e8-code" class="cake-code-dump" style="display: none;"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$mask&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$this</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">_config</span><span style="color: #007700">[</span><span style="color: #DD0000">'mask'</span><span style="color: #007700">];</span></span></code>
<span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">if&nbsp;(empty(</span><span style="color: #0000BB">$mask</span><span style="color: #007700">))&nbsp;{</span></span></code></span>
<code><span style="color: #000000"><span style="color: #0000BB">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">return&nbsp;</span><span style="color: #0000BB">file_put_contents</span><span style="color: #007700">(</span><span style="color: #0000BB">$pathname</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$output</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">FILE_APPEND</span><span style="color: #007700">);</span></span></code></pre><pre id="cakeErr57881c28563e8-context" class="cake-context" style="display: none;">$level = &#039;error&#039;
$message = &#039;[Cake'Network'Exception'UnauthorizedException] You are not authorized to access that location.
Request URL: /api/advertisements.json
Referer URL: http://localhost:85/
Client IP: 127.0.0.1
Stack Trace:
.... and a long stacktrace

当它应该只返回时:

{
    "message": "You are not authorized to access that location.",
    "url": "'/api'/advertisements.json",
    "code": 401
}

我真的不知道为什么它返回 HTML 代码块而不仅仅是 JSON 返回。

由于这段 HTML 代码重定向到登录的方法:

responseError: function (rejection) {
   if(rejection.status === 401) {
      var stateService = $injector.get('$state');
      stateService.go('login');
   }
   return $q.reject(rejection);
}

不起作用,因为无法序列化响应

SyntaxError: Unexpected token < in JSON at position 126

有人知道吗?(如果需要,我可以放更多的代码)

解决方案:

我找到了问题所在。应用程序无权写入错误.log。因此,它返回了无法将日志写入erro.log文件的错误。

为文件提供正确的权限,解决所有问题。