首先:我是编程新手,这是一个学校项目。
我的问题:我有一个基本的登录掩码,使用字符串来保存用户凭据。如果为真,凭据将通过 SharedPrefences 保存。现在我想将用户输入与我的网络服务器 http://fost19.tk/ 的基本身份验证凭据进行比较(用户:fost19,密码:12345)让用户登录应用程序或向他提供错误消息。到目前为止一切正常,但我一直坚持连接到我的网络服务器并比较凭据。
我在互联网上阅读了一些问题,但说实话,我现在完全迷失了,这是在当前 android 版本中实现此目标的最佳方法。我知道我需要从我的网络服务器解密给定的凭据,但我不知道如何在 Java 中执行此操作,并且我不知道需要将哪些“插件”添加到我的项目中。
感谢您的帮助! :)
最佳答案
在下一步中,您可能想要从您的网站下载内容并在应用程序中对其进行处理。 首先,您的应用程序需要互联网的许可。将以下行添加到 list 文件中应用程序标记下方:
<uses-permission android:name="android.permission.INTERNET" />
您的网站需要 http 身份验证。为此,您需要在请求的 header 中包含您的用户名和密码。 但是,Android 不允许同步网络流量。因此,我们在后台从网站下载内容,然后可以通过回调更新用户界面。您可以将我的类 DownloadContent 添加到您的项目中:
import android.os.AsyncTask;
import android.util.Base64;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import javax.net.ssl.HttpsURLConnection;
public class DownloadContent extends AsyncTask<String, Void, String> {
private final Callback callback;
private final String username, password;
private DownloadContent(@NonNull String username, @NonNull String password, @NonNull Callback callback) {
this.callback = callback;
this.username = username;
this.password = password;
}
public static void run(@NonNull String username, @NonNull String password, @NonNull Callback callback) {
DownloadContent loader = new DownloadContent(username, password, callback);
// runs doInBackground asynchronous
loader.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
protected String doInBackground(String... strings) {
try {
String credentials = username + ":" + password;
URL url = new URL ("https://vertretungsplanbbscux.000webhostapp.com/auth/index.html");
// encode to 64 encoded string (necessary for authorization header)
String base64 = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
// perform a post request
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
// append encoded username and password as authorization header
connection.setRequestProperty ("Authorization", "Basic " + base64);
InputStream stream = connection.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stream));
String inputLine;
StringBuilder response = new StringBuilder();
// read html of website
while ((inputLine = reader.readLine()) != null)
response.append(inputLine);
reader.close();
return response.toString();
} catch(Exception e) {
e.printStackTrace();
}
// return null if an error has occurred
return null;
}
@Override
protected void onPostExecute(@Nullable String content) {
// called after doInBackground has finished, synchronous again
if (content == null) {
callback.onNotLoaded();
} else {
callback.onLoaded(content);
}
}
public interface Callback {
void onLoaded(String content);
void onNotLoaded();
}
}
以下是访问内容的方法:
DownloadContent.run("fost19", "12345", new DownloadContent.Callback() {
@Override
public void onLoaded(String content) {
Log.d("DownloadContent", content);
//TODO display content in user interface
}
@Override
public void onNotLoaded() {
Log.d("DownloadContent", "could not fetch content from website");
// TODO show error message
}
});
我希望能够在您的替代计划应用程序方面为您提供帮助。
Edit1:您应该只使用安全连接 (https)。因此,我没有使用您的 .tk 地址,而是使用了您网站上的实际安全地址。
关于java - Android 中的基本身份验证登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193304/