【问题标题】:Banning users by reading variable via Java通过 Java 读取变量来禁止用户
【发布时间】:2014-06-16 20:16:06
【问题描述】:

我正在创建一个基于基本密码的登录系统。它使用 MD5 来保护密码。正确的密码是“csk”(不带引号)。如果有人正确输入,他就可以访问本地计算机中的 key.html 文件。但如果有人连续三次输入错误的密码,他就会被“禁止”再次登录。但是我所构建的设计只禁止该特定会话的用户。如果他再次打开终端,它会从头开始。如果变量计数自上次以来大于 3(三),则程序在通过 void main() 执行时将显示“您被禁止”。我想保持基本,不使用 JDBC 和 SQL 等。此外,这是一个本地应用程序,而不是基于 Web 的应用程序。我很困惑我应该采取什么方法。这是我编写的代码:

import java.math.*;
import java.security.*;
import java.io.*;
import java.util.*;
import java.net.HttpURLConnection;

public class pwd {


public static void main(String[] args)throws NoSuchAlgorithmException, IOException,     InterruptedException {
    int count = 1;
    boolean run = true;
    while (run && count<4){

    System.out.println("Enter the password");
    Scanner kb = new Scanner(System.in);
    String pass = kb.nextLine();
    String pd = "ea0882721f7f44384ce772375696f9a6"; //Password is "csk" without quotes  geeks, this is it's MD5
    // so enter "csk" in the terminal
    // to run the program on execution
    String md5sum = md5(pass);

    String os = System.getProperty("os.name");
    boolean o = false;
    int win = os.indexOf("Windows");
    if (md5sum.equals(pd)){

        System.out.println("You've logged in successfully, get the Key now");
        String url = "file:///C:/Users/<username>/Desktop/key.html"; // example www
        Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
        run = false;

    }
    else {

        System.out.println("You've entered the wrong password, try again.");
        System.out.println();
        run = true;

        if (count>=3) {
            System.out.println("You are banned from logging in, due to repeated  unsuccessful login attempts.");
        }
        ++count;


    }

    }

}




 public static String md5(String input)throws NoSuchAlgorithmException, IOException {

    String md5 = null;
    MessageDigest digest = MessageDigest.getInstance("MD5");
    digest.update(input.getBytes(), 0, input.length());
    md5 = new BigInteger(1, digest.digest()).toString(16);
   return md5;
 }



 }

编辑:我不需要将 MD5 散列更改为其他任何内容,这只是一个基本的散列。

【问题讨论】:

    标签: java passwords md5


    【解决方案1】:

    您可以简单地使用java.io.FileWriter 写入文件

    FileWriter writer = null;
    String text = "username";
    try{
         writer = new FileWriter("banned.txt", true);
         writer.write("\r\n"); 
         writer.write(text,0,text.length());
    }catch(IOException ex){
        ex.printStackTrace();
    }finally{
      if(writer != null){
         writer.close();
      }
    }
    

    上面的代码允许你在文件中添加一行。

    要读取文件,您可以这样做 (java.io.BufferedReader):

    BufferedReader reader = new BufferedReader(new FileReader("banned.txt"));
    String line = null;
    while ((line = reader.readLine()) != null) {
        // check the username here
    }
    

    【讨论】:

    • 谢谢伙计。我做了一些调整并做到了。
    【解决方案2】:

    嗯,你想做的事可以做,但你必须创建一种“登录页面”,因为现在(使用你提供的代码)不涉及用户信息。

    要保存禁令的重要信息,例如,您可以将布尔值保存在文件(或拥有该文件时的用户)中,并在代码开头读取相同的文件,以了解用户是否被禁止或不。在这种情况下,您必须在尝试新的输入代码之前更改您的代码以添加禁令信息;)

    如果你没有登录页面,那么一旦你禁止某人,任何用户都将无法再登录:)

    PS:Java 类通常以大写开头,不是pwd,而是Pwd

    PS2:您的计数将始终增加,因为它不在 else 中;)因此,即使用户被禁止,每次尝试新代码都会增加它;)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多