[英]Security for DataBase password and user login password in Spring and Java Encoder How to?
[英]User Login and Password: Database Conformation
我正在创建一个需要用户登录才能进入主应用程序的应用程序。 当他单击“登录”按钮时,应在允许用户进入应用程序之前检查以确保电子邮件/用户名和密码正确无误,否则将给他们带来错误。 我一直在努力弄清楚如何做到这一点。 这是我的main_activity页面,显示主登录屏幕。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/Email"/>
<EditText
android:id="@+id/Email_enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:hint="@string/Email_enter"
android:singleLine="true"
android:inputType="textEmailAddress"/>
<!-- Password Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="@string/edit_message2"/>
<EditText
android:id="@+id/Password_enter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:hint="@string/Password_enter"
android:inputType="textPassword"
android:singleLine="true" />
<!-- Login button -->
<Button android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/Login"
android:onClick="sendMessage"/>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Query"
android:onClick="View arg0"/>
请暂时忽略“查询”按钮。 这是Java文件:
package com.example.awesomefilebuilder;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Base_Activity {
public final static String EXTRA_MESSAGE = "com.example.awesomefilebuilder.MESSAGE";
public final String TAG = "MainActivity";
public final String edit_message ="Username";
public static final String DATABASE_NAME = "data";
public static final String TABLE_NAME = "comments_table";
public static final String C_ID = "_id";
public static final String NAME = "name";
public static final String COMMENT = "comment";
public static final String EMAIL = "email";
public static final String TIME = "time";
public static final String PASSWORD = "password";
public static final int VERSION = 1;
View view;
SQLiteDatabase db;
DbHelper dbhelper;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
DbHelper dbhelper = new DbHelper(getApplicationContext());
db = dbhelper.getWritableDatabase();
view = inflater.inflate(R. layout.activity_main, container,false);
ContentValues cv = new ContentValues();
cv.put(DbHelper.NAME, NAME);
cv.put(DbHelper.COMMENT, COMMENT);
cv.put(DbHelper.EMAIL, EMAIL);
cv.put(DbHelper.PASSWORD, PASSWORD);
db.insert(DbHelper.TABLE_NAME, null, cv);
Button query = (Button) view.findViewById(R.id.query);
query.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
String [] columns = {DbHelper.NAME, DbHelper.COMMENT, DbHelper.EMAIL};
Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null);
cursor.moveToFirst();
while(cursor.moveToNext())
{
String name = cursor.getString(cursor.getColumnIndex(DbHelper.NAME));
String comment = cursor.getString(cursor.getColumnIndex(DbHelper.COMMENT));
String password = cursor.getString(cursor.getColumnIndex(DbHelper.PASSWORD));
String email = cursor.getString(cursor.getColumnIndex(DbHelper.EMAIL));
Toast.makeText(getApplicationContext(), "Name = "+ name +"/nComment= "+ comment,Toast.LENGTH_SHORT).show();
}
cursor.close();
}
});
return view;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.i(TAG, "onCreate" );
}
@Override
public void onDestroy() {
super.onDestroy(); // Always call the superclass
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, HomePageMain.class);
EditText editText = (EditText) findViewById(R.id.Email_enter);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
Log.i(TAG, "sendMessage" );
}
};
当用户点击“登录”按钮时,我希望该应用在允许他们进入或给出错误之前先检查数据。 我在这里怎么做? 之后,如果数据确实正确签出,我希望该应用程序将用户发送到名为HomePageMain.class的下一个主页。
如果需要其他页面,请告诉我。 提前致谢! 我真的很欣赏。
这是一个广泛的问题。 您可能需要做的是检测用户何时首次运行该应用程序。 首次登录时,您可以要求用户输入用户名和密码。 现在,最简单(但可能不是最好)的方法是将其保存在SharedPreferences
。 然后,当用户下次访问该应用时,您可以从SharedPreference
检查它,如果登录正确,则继续,否则显示错误。
在sendMessage
函数内部,您可以首先检查是否已使用您用于用户名和密码的特定密钥将任何内容存储在sharepreference中。 如果未保存,则用户是第一次来。 然后分层存储一个新的Intent。 如果有保存的东西,则可以从共享首选项中提取thme并将这些值与输入的值进行比较。
现在,将密码保存在SharedPreference
不是一个好主意,因为在可以访问的根设备上。 您可以对密码使用哈希/加密。
在Android和Windows中登录的SharedPreferences
用于代码段和其他逻辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.