简体   繁体   English

如何在Java 8上创建Java两步验证?

[英]How to create a Java 2-Step authentication on Java 8?

Hello everyone at StackOverflow, 大家好,StackOverflow,

I will be asking a question that I'm confused about and searched for hours for, it's to put a 2-Step authentication on a Java program, what I want is that is send a generated code to a login page like the one I created below. 我会问一个让我感到困惑和搜索了好几个小时的问题,它是在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." );
        }
    }
}

Everything is fine with the code above, it's just that I want to send a randomly generated code to a phone number, like as I said before a 2-Step verification. 上面的代码一切都很好,只是我想将随机生成的代码发送到电话号码,就像我在两步验证之前所说的那样。 Like Google has or Microsoft and etc. For example: You write a phone number, 123-456-7890 , then it sends a code to the phone number and it's says something like Your code is 178634 then you write it into the input box, then it checks if it was the code it sent. 例如Google或Microsoft等。例如:您输入一个电话号码123-456-7890 ,然后它向该电话号码发送一个代码,并且显示类似Your code is 178634然后将其写入输入框,然后检查它是否是发送的代码。

If the question I said is not specific enough or something like that please tell me. 如果我说的问题不够具体或类似问题,请告诉我。

Thanks and keep on coding! 谢谢,继续编码!

-CitizenZap -CitizenZap

First, I suggest you put your data in map, combine username, password, phoneNumber into one class, like UserInfo. 首先,建议您将数据放在地图中,将用户名,密码,phoneNumber合并到一个类中,例如UserInfo。 Because you need to bind phoneNumber to user, or any phoneNumber after login is acceptable. 因为您需要将phoneNumber绑定到用户,否则可以接受登录后的任何phoneNumber。

Then, you replace 然后,您替换

    {
            JOptionPane.showMessageDialog(null, "Logged in!" );
    }

with

    String newPhoneNumber = null;

    {
            newPhoneNumber = JOptionPane.showInputDialog("Enter your phone number");
    }

You need to check if newPhoneNumber equals with the phoneNumber bind to the user. 您需要检查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.
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM