Laravel Rest Authentication when post via HttpClie

2019-05-20 22:45发布

问题:

How can I filter the laravel rest app, just authenticated userid and correct password when I post via android HttpClient? I try to find the best way, but get no best sample.

This is my code:

public static int LogExceptionError(final ExceptionLog exceptionLog) {

        final HttpResponse[] response = new HttpResponse[1];
        new Thread(new Runnable() {
            public void run() {
                // Create a new HttpClient and Post Header
                HttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(AndroidHelperUtility.getBaseUrl("exceptionlog/create", AndroidHelperUtility.IS_DEBUG_MODE));
                try {

                    Gson gson = new Gson();
                    String jsonString = gson.toJson(exceptionLog);

                    JSONObject jsonObject = new JSONObject();
                    jsonObject = new JSONObject(jsonString);

                    JSONArray jsonArray = new JSONArray();
                    jsonArray.put(jsonObject);

                    // Post the data:
                    httpPost.setHeader("json", jsonObject.toString());
                    httpPost.getParams().setParameter("jsonpost", jsonArray);

                    // Execute HTTP Post Request
                    System.out.print(jsonObject);
                    HttpResponse httpResponseponse = httpClient.execute(httpPost);
                } catch (JSONException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();

        if (response[0] != null) {
            return 1;
        } else {
            return 0;
        }
    }

Laravel Controller:

public function postCreate() {
        $json = $_SERVER['HTTP_JSON'];
        $data = json_decode($json);  
        $object = ExceptionLogService::create($data);
        return Response::json($object);

    }

Service Class:

class ExceptionLogService extends BaseService {

    public static function create($data) {
        $object = array(
            "refNumber" => $data->refNumber,
            "source" => $data->source,
            "userName" => $data->userName,
            "description" => $data->description
        );
        ExceptionLogRepository::create($object);
        return $object;
    }