【问题标题】:User creation in laravel Passport using Android Volley使用 Android Volley 在 laravel Passport 中创建用户
【发布时间】:2018-11-17 11:16:36
【问题描述】:

我们的 localhost (xampp) 中有 Laravel + 护照。我们希望从 android 应用程序向 laravel 应用程序发送请求,但下面的方法没有响应,也没有错误,并且 btnSubmit 文本在发送请求后为空,我们没有收到任何访问令牌。这个方法的问题在哪里?

private void createUser(){
    final String url = "http://192.168.1.3/me2we/public/oauth/token";
    StringRequest stringRequest = new StringRequest(
            Request.Method.POST, url
            , new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    btnSubmit.setText(response);
                }
            }, new Response.ErrorListener(){
                @Override
                public void onErrorResponse(VolleyError error) {
                    btnSubmit.setText(error.getMessage());
                }
            }
        ){
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<String, String>();
            params.put("grant_type","password");
            params.put("client_id","2");
            params.put("client_secret","MKCFtd1U7pJ3J85iSu380SyGIVltWlYdrL334pbF");
            params.put("username","aminshabanzadeh1@gmail.com");
            params.put("password","A1b2C");
            params.put("scope","");
            return params;
        }
    };
    AppController.getInstance().addToRequestQueue(stringRequest);
}

我们是否在请求 url 中选择了正确的 IP 地址?下面是 CMD 中的 ipconfig 命令输出:

Windows IP Configuration


Ethernet adapter Ethernet 3:

   Connection-specific DNS Suffix  . : test.com
   Link-local IPv6 Address . . . . . : fe80::7d4b:27f7:1e6d:f341%20
   IPv4 Address. . . . . . . . . . . : 10.253.31.78
   Subnet Mask . . . . . . . . . . . : 255.255.248.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Ethernet 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Wi-Fi:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 2:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Wireless LAN adapter Local Area Connection* 13:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Ethernet adapter Ethernet:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::8852:3d75:59b3:a871%9
   IPv4 Address. . . . . . . . . . . : 192.168.1.3
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.1.1

Ethernet adapter VMware Network Adapter VMnet1:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::41f9:ce63:37a2:134%10
   IPv4 Address. . . . . . . . . . . : 192.168.229.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter VMware Network Adapter VMnet8:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::11b1:57cb:b413:26de%4
   IPv4 Address. . . . . . . . . . . : 192.168.230.1
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . :

Ethernet adapter Bluetooth Network Connection:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

laravel 验证令牌类:

<?php

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;

class VerifyCsrfToken extends Middleware
{
    /**
     * Indicates whether the XSRF-TOKEN cookie should be set on the response.
     *
     * @var bool
     */
    protected $addHttpCookie = true;

    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
}

【问题讨论】:

    标签: java php android laravel


    【解决方案1】:

    在 Laravel 中,使用 POST 方法,您需要验证 Csrf Token 以确保安全。 我知道的简单方法是忽略此检查安全性。

    你可以尝试:在 Laravel 的文件中添加你的 name 方法: 控制器/中间件/VerifyCsrfToken.php

    如果您知道如何接受验证并在 Android Studio 上进行检查,请告诉我 VerifyCsrfToken class in Laravel

    【讨论】:

    • 我在 Laravel verfycsrf 类中添加了以下 url 以被忽略。但仍然响应是空的!网址:192.168.1.3/me2we/public/oauth/token
    • 我创建了一个示例 post api 并且有效。所以它在一个简单的 api 方法中工作,但在这个 url 中没有给我访问令牌
    • 只添加'token',而不是整个url
    • 谢谢,但 btnSubmit 仍然是空的!并且没有工作。
    【解决方案2】:

    我假设您正在尝试从模拟器访问它,在这种情况下,模拟器无法访问您机器的 IP 地址。要从模拟器访问您的本地主机,您必须使用“10.0.2.2”。

    Check here for reference

    Another reference

    【讨论】:

    • 我们没有使用模拟器。我们正在使用真实设备来测试我们的应用程序
    • 如果是这种情况,那么您可以查看此链接,看看该解决方案是否适合您stackoverflow.com/questions/4779963/… 如果这不起作用,您还可以查看此线程中的答案@ 987654324@
    • 谢谢。我们进行了一些测试。 ip是正确的。我们现在不知道如何创建新用户。
    • 我在您的 sn-p 中注意到您没有发送代码应该在其中执行的上下文,也许这就是它不起作用的原因。尝试像这样传递上下文“AppController.getInstance(this).addToRequestQueue(jsonObjectRequest);”看看它是否会起作用,这里指的是当前的执行上下文。
    猜你喜欢
    • 2023-03-23
    • 2017-10-25
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2017-12-14
    • 2018-07-03
    • 2020-07-09
    • 2019-04-14
    相关资源
    最近更新 更多