sac*_*011 8 android one-time-password
我想在我的Android应用程序中实现OTP功能.在此应用程序中,注册后用户将收到一次密码密钥.经过核实OTP,用户将能够成功注册/开立帐户通过使用OTP.我需要做些什么呢?
Vip*_*rma 11
我实现了一个非常简单的OTP方法..
正如@Vipin 提到的,最好的方法是自己实现它:
首先,您必须生成一个 4 位(或任何您想要的)密码,例如:
int range = 9; // to generate a single number with this range, by default its 0..9
int length = 4; // by default length is 4
public int generateRandomNumber() {
int randomNumber;
SecureRandom secureRandom = new SecureRandom();
String s = "";
for (int i = 0; i < length; i++) {
int number = secureRandom.nextInt(range);
if (number == 0 && i == 0) { // to prevent the Zero to be the first number as then it will reduce the length of generated pin to three or even more if the second or third number came as zeros
i = -1;
continue;
}
s = s + number;
}
randomNumber = Integer.parseInt(s);
return randomNumber;
}
Run Code Online (Sandbox Code Playgroud)
然后,您必须将此号码保存在某个地方,例如在您的首选项中:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(mContext);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("OTP_PIN", randomNumber);
editor.commit();
Run Code Online (Sandbox Code Playgroud)
下一步,将使用适当的 SMS 网关将该 OTP 发送到相应的电话号码,对我来说,我使用clickATell和我们的 php 服务器来发送消息,api 文档非常清楚。如果您想直接从应用程序发送消息,可能SMSgateway可以提供帮助。
最后一步,是验证 SMS 收到的代码是存储在设备首选项中的代码,这非常简单直接,您所要做的就是EditText为用户提供一个允许他输入手机收到的代码, 如果代码与设备首选项中保存的 OTP 匹配,则让他通过应用程序,否则,显示正确的错误消息。
一个优雅的举动:
不是强制性的,但最好,因为很多应用程序都可以提供短信监听器来收听即将到来的消息,从收到的消息中获取代码,在代码验证中显示它editText,验证它,如果是真的,通过应用程序。
在manifest.xml 中:
<receiver
android:name=".Services.SmsListener"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="999">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)
听众:
public class SmsListener extends BroadcastReceiver {
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
public void onReceive(Context context, Intent intent) {
Log.d("messageBody", intent.getAction());
if (intent.getAction().equals(Telephony.Sms.Intents.SMS_RECEIVED_ACTION)) {
try {
String messageBody = "";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
messageBody = smsMessage.getMessageBody();
}
Intent messageReceived = new Intent(SVPreferences.SMS_RECEIVED);
messageReceived.putExtra("sms", messageBody);
context.sendBroadcast(messageReceived); // when receiving it somewhere in your app, subString the additional text and leave only the code, then place it in the editText and do your verification
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
收件人:
BroadcastReceiver receiveSMS = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
try {
String smsBody = intent.getStringExtra("sms");
String pin = smsBody.replace(getResources().getString(R.string.your_extra_text), "").trim();
editText_confirm_pin.setText(pin);
if (validatePin(pin))
// go through the app
} catch (Exception ex) {
ex.printStackTrace();
}
}
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
57445 次 |
| 最近记录: |