如何在 Java 8 上创建 Java 两步验证?
How to create a Java 2-Step authentication on Java 8?
Whosebug 的大家好,
我会问一个我很困惑并搜索了几个小时的问题,它是在 Java 程序上进行两步验证,我想要的是发送生成的代码到我在下面创建的登录页面。
package log;
import javax.swing.JOptionPane;
public class Login {
public static void main(String args[]) {
String username = JOptionPane.showInputDialog("Enter your username");
String password = JOptionPane.showInputDialog("Enter your password");
if (
username != null && password != null &&
(
(username.equals("g17") && password.equals("ire35")) ||
(username.equals("ree") && password.equals("melikejava")) ||
(username.equals("citizenzap") && password.equals("javarules23"))||
(username.equals("devs") && password.equals("password"))
)
)
{
JOptionPane.showMessageDialog(null, "Logged in!" );
} else {
JOptionPane.showMessageDialog(null, "Incorrect username or password! Try again later." );
}
}
}
上面的代码一切正常,只是我想将随机生成的代码发送到 phone 号码,就像我在两步验证之前所说的那样。就像 Google 或 Microsoft 等。例如:您写一个 phone 号码,123-456-7890
,然后它会向 phone 号码发送一个代码,它会说类似 Your code is 178634
然后你把它写到输入框里,然后它会检查它是否是它发送的代码。
如果我说的问题不够具体或者类似的问题请告诉我。
谢谢,继续编码!
-CitizenZap
首先,我建议你把你的数据放到map里,把username, password, phoneNumber合并成一个class,比如UserInfo。因为你需要给用户绑定phoneNumber,或者登录后的任何phoneNumber都可以。
然后,你替换
{
JOptionPane.showMessageDialog(null, "Logged in!" );
}
和
String newPhoneNumber = null;
{
newPhoneNumber = JOptionPane.showInputDialog("Enter your phone number");
}
您需要检查 newPhoneNumber 是否与绑定到用户的 phoneNumber 相等。
// this should be in a while(true) loop
if (newPhoneNumber.equals(phoneNumber)) {
sendSms(phoneNumber);
String code = JOptionPane.showInputDialog("Enter your code");
boolean result = validateAuthorizationCode(code); // here you validate the code
if (result) {
JOptionPane.showMessageDialog(null, "Logged in!" );
} else {
JOptionPane.showMessageDialog(null, "Wrong code!" );
}
} else {
noticeWrongNumber(newPhoneNumber); // tell him the number is wrong, please reinput.
}
Whosebug 的大家好,
我会问一个我很困惑并搜索了几个小时的问题,它是在 Java 程序上进行两步验证,我想要的是发送生成的代码到我在下面创建的登录页面。
package log;
import javax.swing.JOptionPane;
public class Login {
public static void main(String args[]) {
String username = JOptionPane.showInputDialog("Enter your username");
String password = JOptionPane.showInputDialog("Enter your password");
if (
username != null && password != null &&
(
(username.equals("g17") && password.equals("ire35")) ||
(username.equals("ree") && password.equals("melikejava")) ||
(username.equals("citizenzap") && password.equals("javarules23"))||
(username.equals("devs") && password.equals("password"))
)
)
{
JOptionPane.showMessageDialog(null, "Logged in!" );
} else {
JOptionPane.showMessageDialog(null, "Incorrect username or password! Try again later." );
}
}
}
上面的代码一切正常,只是我想将随机生成的代码发送到 phone 号码,就像我在两步验证之前所说的那样。就像 Google 或 Microsoft 等。例如:您写一个 phone 号码,123-456-7890
,然后它会向 phone 号码发送一个代码,它会说类似 Your code is 178634
然后你把它写到输入框里,然后它会检查它是否是它发送的代码。
如果我说的问题不够具体或者类似的问题请告诉我。
谢谢,继续编码!
-CitizenZap
首先,我建议你把你的数据放到map里,把username, password, phoneNumber合并成一个class,比如UserInfo。因为你需要给用户绑定phoneNumber,或者登录后的任何phoneNumber都可以。
然后,你替换
{
JOptionPane.showMessageDialog(null, "Logged in!" );
}
和
String newPhoneNumber = null;
{
newPhoneNumber = JOptionPane.showInputDialog("Enter your phone number");
}
您需要检查 newPhoneNumber 是否与绑定到用户的 phoneNumber 相等。
// this should be in a while(true) loop
if (newPhoneNumber.equals(phoneNumber)) {
sendSms(phoneNumber);
String code = JOptionPane.showInputDialog("Enter your code");
boolean result = validateAuthorizationCode(code); // here you validate the code
if (result) {
JOptionPane.showMessageDialog(null, "Logged in!" );
} else {
JOptionPane.showMessageDialog(null, "Wrong code!" );
}
} else {
noticeWrongNumber(newPhoneNumber); // tell him the number is wrong, please reinput.
}