2017-04-25 24 views
-1

我正在构建原生Android应用程序。我有一个不区分大小写的登录页面。Android - 使应用程序登录,区分大小写

例子:两个密码的管理员管理允许用户登录。这应该是不可能的。

登录正在工作。我的问题是区分大小写。我对Java和Android还是一个新手,并不确定在哪里放置代码以区分大小写。

我应该使用equalsIgnoreCase()吗?
如果是这样,你将如何在这个登录中使用equalsIgnoreCase()。你在哪里主持用户登录与其他API

任何帮助将被占用。

private class LoginProcess extends AsyncTask<Void, Void, String> { 

    protected void onPreExecute(){ 
     super.onPreExecute(); 
     dialog_login.show(); 
    } 

    String user = userInput.getText().toString(); 
    String pass = passInput.getText().toString(); 

    @Override 
    protected String doInBackground(Void... voids) { 

     String address = my_url + "api/user/login.json"; 

     HttpURLConnection urlConnection; 
     String requestBody; 
     Uri.Builder builder = new Uri.Builder(); 

     Map<String, String> params = new HashMap<>(); 
     params.put("username", user); 
     params.put("password", pass); 

     Iterator entries = params.entrySet().iterator(); 
     while (entries.hasNext()) { 
      Map.Entry entry = (Map.Entry) entries.next(); 
      builder.appendQueryParameter(entry.getKey().toString(), entry.getValue().toString()); 
      entries.remove(); 
     } 
     requestBody = builder.build().getEncodedQuery(); 

     try { 
      URL url = new URL(address); 
      urlConnection = (HttpURLConnection) url.openConnection(); 
      urlConnection.setDoOutput(true); 
      urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
      OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream()); 
      BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8")); 
      writer.write(requestBody); 
      writer.flush(); 
      writer.close(); 
      outputStream.close(); 
      JSONObject jsonObject = new JSONObject(); 
      InputStream inputStream; 
      // get stream 
      if (urlConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) { 
       inputStream = urlConnection.getInputStream(); 
       Log.w("URL", "Ok"); 
      } else { 
       inputStream = urlConnection.getErrorStream(); 
       Log.w("URL", "Bad request"); 
      } 

      // parse stream 
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
      String temp, response = ""; 
      while ((temp = bufferedReader.readLine()) != null) { 
       response += temp; 
      } 
      // put into JSONObject 
      jsonObject.put("Content", response); 
      jsonObject.put("Message", urlConnection.getResponseMessage()); 
      jsonObject.put("Length", urlConnection.getContentLength()); 
      jsonObject.put("Type", urlConnection.getContentType()); 

      JSONObject jsonObj = new JSONObject(response); 
      session_name = jsonObj.getString("session_name"); 
      session_id = jsonObj.getString("sessid"); 
      token = jsonObj.getString("token"); 

      return jsonObject.toString(); 

     } catch (IOException | JSONException e) { 
      return e.toString(); 
     } 
    } 
+1

我没有看到任何关于密码的检查,所以我不知道这是否是Android/Java的问题。 – Denny

+1

你不觉得这个问题是在服务器端吗? –

+0

我认为你是对的。感谢您的回复 –

回答

1

首先,为您的登录逻辑使用基本身份验证系统。这应该会将您将用于其他API调用的令牌发回给您。 看看Retrofit和OkHttpClient。 此外,您的所有网络连接逻辑应该在另一类。

在你的情况下,必须在服务器端测试大小写敏感性。 您的android应用程序必须对凭证有效性不知情。它只是把它从用户,并提供给服务器API验证(或不)

+0

谢谢,这非常有帮助。 –