Java Android中的基本身份验证登录
首先:我是编程新手,这是一个学校项目 我的问题是:我有一个使用字符串保存用户凭据的基本登录掩码。如果凭据为true,则通过SharedReferences保存。现在,我想将用户输入与我的Web服务器(用户:fost19,pw:12345)的基本身份验证凭据进行比较,以便将用户登录到应用程序或向其发送错误消息。到目前为止,一切都正常,但我一直无法连接到我的Web服务器并比较凭据 我在网上读到了一些问题,但说实话,我现在完全不知所措,在当前的android构建中,哪种方法是实现这一点的最佳方法。我知道我需要从我的Web服务器解密给定的凭据,但我不知道如何在Java中做到这一点,我也不知道哪些“插件”需要添加到我的项目中Java Android中的基本身份验证登录,java,android,authentication,Java,Android,Authentication,首先:我是编程新手,这是一个学校项目 我的问题是:我有一个使用字符串保存用户凭据的基本登录掩码。如果凭据为true,则通过SharedReferences保存。现在,我想将用户输入与我的Web服务器(用户:fost19,pw:12345)的基本身份验证凭据进行比较,以便将用户登录到应用程序或向其发送错误消息。到目前为止,一切都正常,但我一直无法连接到我的Web服务器并比较凭据 我在网上读到了一些问题,但说实话,我现在完全不知所措,在当前的android构建中,哪种方法是实现这一点的最佳方法。我知
谢谢你的帮助!:) 在下一步中,您可能希望从您的网站下载内容并在应用程序中进行处理。 首先,你的应用需要互联网许可。在应用程序标记下方的清单文件中添加以下行:
<uses-permission android:name="android.permission.INTERNET" />
我希望我可以帮助你的替代计划应用程序
Edit1:您应该只使用安全连接(https)。因此,我没有使用你的.tk地址,而是使用了你网站上的实际安全地址
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
}
});