【问题标题】:Access with csrf token using Volley Android使用 Volley Android 使用 csrf 令牌访问
【发布时间】:2017-02-03 02:03:11
【问题描述】:

我想发送一个请求并从它的 cookie 中获取 XSRF-TOKEN,当我将 POST 发送到寄存器时,我想将它放入标题中。其实我想做这样的事情:access laravel app from android app with csrf token

我正在使用 StringRequest 发送这样的请求:

private void registerUser(final String name, final String email,
                          final String password,final String confirm) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

    pDialog.setMessage("Registering ...");
    showDialog();

    final StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_REGISTER, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {

            datares = response;

            Log.d("REGISTER RESPONSE", "Register Response: " + response.toString());

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getActivity().getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            params.put("password-confirm",confirm);

            return params;
        }

    };
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

还有 AppController.java

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

您知道如何使用 Volley 来做到这一点吗?

【问题讨论】:

    标签: android laravel android-volley


    【解决方案1】:

    这是一个迟到的答案。无论如何,如果有人偶然发现这里寻找任何答案,我会在这里添加一种方法:)

    您可以使用 csrf_exempt。它将免除 CSRF 考虑的观点。我不确定这是否存在重大安全风险,但它确实有效。

    from django.views.decorators.csrf import csrf_exempt
    
    # Create your views here.
    from django.http import HttpResponse
    
    @csrf_exempt
    def homePageView(request):
        data = request.POST.get('data')
        return HttpResponse(data)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-16
      • 2012-05-29
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 2012-06-21
      • 2018-06-05
      • 1970-01-01
      相关资源
      最近更新 更多