如何在Java中添加动态验证码

时间:2022-05-01 02:09:16

I want to add dynamic captcha on page.Whenever more than 3 request comes inspecific time from same IP address then Captcha will appear on the screen..

我想在页面上添加动态验证码。每当有超过3个请求来自同一IP地址的特定时间时,屏幕上会出现Captcha ..

3 个解决方案

#1


3  

  • You can write a code in servlet filter to check if many requests come from a specific IP within a specific time duration. In this case, you can add some flag in the session.

    您可以在servlet过滤器中编写代码,以检查在特定持续时间内是否有来自特定IP的许多请求。在这种情况下,您可以在会话中添加一些标志。

  • Use this flag to check on the page if to display the captcha or not.

    使用此标志检查页面是否显示验证码。

#2


2  

  • Create a servlet which dynamically creates the captcha. To create the captcha you can use ImageMagick in conjunction with im4java.
  • 创建一个动态创建验证码的servlet。要创建验证码,您可以将ImageMagick与im4java结合使用。
  • In your HTML page embed an Image-Tag with the path to the servlet which generates the captcha.
  • 在HTML页面中嵌入一个Image-Tag,其中包含生成验证码的servlet的路径。
  • Count the number of requests to the captcha-generating servlet in your session. As long as the threshold has not been reached you may return an empty image to avoid any errors on the client side.
  • 计算会话中生成验证码的servlet的请求数。只要未达到阈值,您可以返回空图像以避免客户端出现任何错误。
  • If the threshold has been reached you generate the captcha. Store the value (i.e. the value the user has to enter in order to proceed) in the session.
  • 如果已达到阈值,则生成验证码。在会话中存储值(即用户必须输入的值才能继续)。
  • The user sees the captcha and has to enter its value.
  • 用户看到验证码并且必须输入其值。
  • Compare the value the user has entered with the value stored in the session.
  • 将用户输入的值与会话中存储的值进行比较。

I hope this helps.

我希望这有帮助。

#3


-1  

A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a test to determine whether the user is human or not.

CAPTCHA(完全自动公共图灵测试告诉计算机和人类除外)是一项测试,以确定用户是否是人。

CAPTCHA provides a way to block bots from interacting with your site by providing something that's hard for them to read, but easy for people to read.

CAPTCHA提供了一种方法,通过提供难以阅读的内容来阻止机器人与您的网站进行交互,但人们可以轻松阅读。

import java.util.Random;
public class GenerateCaptcha {
      /**
       * Generate Length between 5 and 8.
       * @return return length.
       */
      private static int generateRandomLength() {
            int length = 5 + Math.abs((random.nextInt()%4));
            return length;
      }

      /**
       *  Generate a CAPTCHA String consisting of random
       *  lower case & upper case letters, and numbers.
       */
      private static String generateCaptchaString(int length) {

            StringBuffer captchaBuffer = new StringBuffer();

            for (int i = 0; i < length; i++) {

                  /** Generate the Random Character between 0 to 61.
                   * NOTE: Take abs value, because there may
                   * be ArrayIndexOutOfBount
                   * Exception for negative number*/
                  int rndCharIdx = Math.abs(random.nextInt()) % 62;

                  char character = characters[rndCharIdx];

                  captchaBuffer.append(character);
            }
            return captchaBuffer.toString();
      }

      private static Character[] characters = {'a','b','c','d','e','f',
            'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
            'v','w','x','y','z','A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
            'Z','0','1','2','3','4','5','6','7','8','9'};

      private static Random random = new Random();

      public static void main(String[] args) {

            int rndmCaptchaLen = generateRandomLength();

            String captcha = generateCaptchaString(rndmCaptchaLen);

            System.out.println("Random Captcha #"+captcha);
      }
}

More information on my blog:

有关我博客的更多信息:

http://javaexplorer03.blogspot.in/2016/12/program-to-generate-unique-captcha-to.html

http://javaexplorer03.blogspot.in/2016/12/program-to-generate-unique-captcha-to.html

#1


3  

  • You can write a code in servlet filter to check if many requests come from a specific IP within a specific time duration. In this case, you can add some flag in the session.

    您可以在servlet过滤器中编写代码,以检查在特定持续时间内是否有来自特定IP的许多请求。在这种情况下,您可以在会话中添加一些标志。

  • Use this flag to check on the page if to display the captcha or not.

    使用此标志检查页面是否显示验证码。

#2


2  

  • Create a servlet which dynamically creates the captcha. To create the captcha you can use ImageMagick in conjunction with im4java.
  • 创建一个动态创建验证码的servlet。要创建验证码,您可以将ImageMagick与im4java结合使用。
  • In your HTML page embed an Image-Tag with the path to the servlet which generates the captcha.
  • 在HTML页面中嵌入一个Image-Tag,其中包含生成验证码的servlet的路径。
  • Count the number of requests to the captcha-generating servlet in your session. As long as the threshold has not been reached you may return an empty image to avoid any errors on the client side.
  • 计算会话中生成验证码的servlet的请求数。只要未达到阈值,您可以返回空图像以避免客户端出现任何错误。
  • If the threshold has been reached you generate the captcha. Store the value (i.e. the value the user has to enter in order to proceed) in the session.
  • 如果已达到阈值,则生成验证码。在会话中存储值(即用户必须输入的值才能继续)。
  • The user sees the captcha and has to enter its value.
  • 用户看到验证码并且必须输入其值。
  • Compare the value the user has entered with the value stored in the session.
  • 将用户输入的值与会话中存储的值进行比较。

I hope this helps.

我希望这有帮助。

#3


-1  

A CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart) is a test to determine whether the user is human or not.

CAPTCHA(完全自动公共图灵测试告诉计算机和人类除外)是一项测试,以确定用户是否是人。

CAPTCHA provides a way to block bots from interacting with your site by providing something that's hard for them to read, but easy for people to read.

CAPTCHA提供了一种方法,通过提供难以阅读的内容来阻止机器人与您的网站进行交互,但人们可以轻松阅读。

import java.util.Random;
public class GenerateCaptcha {
      /**
       * Generate Length between 5 and 8.
       * @return return length.
       */
      private static int generateRandomLength() {
            int length = 5 + Math.abs((random.nextInt()%4));
            return length;
      }

      /**
       *  Generate a CAPTCHA String consisting of random
       *  lower case & upper case letters, and numbers.
       */
      private static String generateCaptchaString(int length) {

            StringBuffer captchaBuffer = new StringBuffer();

            for (int i = 0; i < length; i++) {

                  /** Generate the Random Character between 0 to 61.
                   * NOTE: Take abs value, because there may
                   * be ArrayIndexOutOfBount
                   * Exception for negative number*/
                  int rndCharIdx = Math.abs(random.nextInt()) % 62;

                  char character = characters[rndCharIdx];

                  captchaBuffer.append(character);
            }
            return captchaBuffer.toString();
      }

      private static Character[] characters = {'a','b','c','d','e','f',
            'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u',
            'v','w','x','y','z','A','B','C','D','E','F','G','H','I','J',
            'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
            'Z','0','1','2','3','4','5','6','7','8','9'};

      private static Random random = new Random();

      public static void main(String[] args) {

            int rndmCaptchaLen = generateRandomLength();

            String captcha = generateCaptchaString(rndmCaptchaLen);

            System.out.println("Random Captcha #"+captcha);
      }
}

More information on my blog:

有关我博客的更多信息:

http://javaexplorer03.blogspot.in/2016/12/program-to-generate-unique-captcha-to.html

http://javaexplorer03.blogspot.in/2016/12/program-to-generate-unique-captcha-to.html