来自 android 应用程序的 cakephp JWT 身份验证的令牌值应该是多少?

What should be the value of token for cakephp JWT authentication from android app?

我在 CakePHP 中使用 JWT 身份验证来处理 Android 应用程序中的登录操作。我在 Cake 中禁用了 CSRF 保护并通过 "SharedPreferencesConstants" class 传递令牌值,其中使用下面显示的代码设置令牌值:

    // Running default handler
    new Handler().post(new Runnable() {
        @Override
        public void run() {
            fcm = new MyFireBaseIntanceIdservice();
            String token = fcm.getrefeshtoken();
            // Log.e("test", token);
            SharedPreferencesConstants.setTOKEN(LoginActivity.this, token);
        }
    });

不过,我试过玩一下令牌值。

我现在得到的错误是 "Error: [Cake\Http\Exception\InvalidCsrfTokenException] Missing CSRF token cookie".

到目前为止我尝试过的方法是禁用 Cake 的 AppController 中的 CSRF 保护。

            EditText txtEmail = (EditText) findViewById(R.id.txtEmail);
            EditText txtPassword = (EditText) findViewById(R.id.txtPassword);
            jsonObject.put("email", txtEmail.getText().toString());
            jsonObject.put("password", txtPassword.getText().toString());
            jsonObject.put("cookieName", "appname");
            jsonObject.put("_Token", "_csrfToken");
            jsonObject.put("deviceToken", SharedPreferencesConstants.getTOKEN(this));
            jsonObject.put("secureKey", SharedPreferencesConstants.getSECUREKEY(this));

作为计划解决方案,我想到的一个想法与要在请求中传递的 HTTP header 有关。但是,我无法为这个想法找到任何解决方案。

任何solution/suggestions?

编辑 #1: 在我的 AppController.php 文件中,我只加载了安全组件,没有加载 CSRF。这是相同的代码:

    $this->loadComponent('Security');
    // $this->loadComponent('Csrf');

而且,我在 CustomersController.php beforeFilter() 函数中的代码是:

    $this->getEventManager()->off($this->Csrf);

我的 AppController 中的 JWT 授权代码是:

    $this->loadComponent('Auth', [
        'storage' => 'Memory',
        'authenticate' => [
            'ADmad/JwtAuth.Jwt' => [
                'userModel' => 'Customers',
                'fields' => [
                    'username' => 'email'
                ],

                'parameter' => 'token',

                // Boolean indicating whether the "sub" claim of JWT payload
                // should be used to query the Users model and get user info.
                // If set to `false` JWT's payload is directly returned.
                'queryDatasource' => false,
            ],
            'unauthorizedRedirect' => false,
            'checkAuthIn' => 'Controller.initialize',

            // If you don't have a login action in your application set
            // 'loginAction' to false to prevent getting a MissingRouteException.
            'loginAction' => false
        ],
    ]);

我希望我添加的新代码可以帮助您更好地了解问题。

我得到了这个问题的答案。并且,感谢@GregSchmidt。这是因为他善意的建议。所以,这就是我所做的。

我的配置文件夹下的 routes.php 文件中有几行代码,我一直在评论。以下是代码行:

$routes->registerMiddleware('csrf', new CsrfProtectionMiddleware([
    'httpOnly' => true
]));

$routes->applyMiddleware('csrf');

最后,我从控制器中删除了所有 Csrf 禁用代码,奇迹终于发生了。 :)