好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

SSM 实现登录验证码功能(附源码)

如果想用框架来实现验证码,可以看我的另外一篇:

kaptcha 框架的使用很简单的。新手必备。

http://HdhCmsTesttuohang.net/article/171694.html

一、效果图

二、实现代码

第一步:先建一个 randomvalidatecode.java 作为工具类。把下面的代码直接复制粘贴即可。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

package com.utils;

import java.awt.color;

import java.awt.font;

import java.awt.graphics;

import java.awt.image.bufferedimage;

import java.util.random;

import javax.imageio.imageio;

import javax.servlet.http.httpservletrequest;

import javax.servlet.http.httpservletresponse;

import javax.servlet.http.httpsession;

public class randomvalidatecode {

  public static final string randomcodekey = "randomcode_key" ; //放到session中的key

  private random random = new random();

  private string randstring = "0123456789abcdefghijklmnopqrstuvwxyz" ; //随机产生的字符串

  private int width = 80 ; //图片宽

  private int height = 26 ; //图片高

  private int linesize = 40 ; //干扰线数量

  private int stringnum = 4 ; //随机产生字符数量

  /**

   * 生成随机图片

   */

  public void getrandcode(httpservletrequest request,

    httpservletresponse response) {

   httpsession session = request.getsession();

   //bufferedimage类是具有缓冲区的image类,image类是用于描述图像信息的类

   bufferedimage image = new bufferedimage(width,height,bufferedimage.type_int_bgr);

   //产生image对象的graphics对象,改对象可以在图像上进行各种绘制操作

   graphics g = image.getgraphics();

   g.fillrect( 0 , 0 , width, height);

   g.setfont( new font( "times new roman" ,font.roman_baseline, 18 ));

   g.setcolor(getrandcolor( 160 , 200 ));

   //绘制干扰线

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

    drowline(g);

   }

   //绘制随机字符

   string randomstring = "" ;

   for ( int i= 1 ;i<=stringnum;i++){

    randomstring=drowstring(g,randomstring,i);

   }

   session.removeattribute(randomcodekey);

   session.setattribute(randomcodekey, randomstring);

   g.dispose();

   try {

    //将内存中的图片通过流动形式输出到客户端

    imageio.write(image, "jpeg" , response.getoutputstream());

   } catch (exception e) {

    e.printstacktrace();

   }

  }

  /*

   * 获得字体

   */

  private font getfont(){

   return new font("fixedsys",font.center_baseline,18);

  }

  /*

   * 获得颜色

   */

  private color getrandcolor(int fc,int bc){

   if(fc > 255)

    fc = 255;

   if(bc > 255)

    bc = 255;

   int r = fc + random.nextint(bc-fc-16);

   int g = fc + random.nextint(bc-fc-14);

   int b = fc + random.nextint(bc-fc-18);

   return new color(r,g,b);

  }

  /*

   * 绘制字符串

   */

  private string drowstring(graphics g,string randomstring,int i){

   g.setfont(getfont());

   g.setcolor(new color(random.nextint(101),random.nextint(111),random.nextint(121)));

   string rand = string.valueof(getrandomstring(random.nextint(randstring.length())));

   randomstring +=rand;

   g.translate(random.nextint(3), random.nextint(3));

   g.drawstring(rand, 13*i, 16);

   return randomstring;

  }

  /*

   * 绘制干扰线

   */

  private void drowline(graphics g){

   int x = random.nextint(width);

   int y = random.nextint(height);

   int xl = random.nextint(13);

   int yl = random.nextint(15);

   g.drawline(x, y, x+xl, y+yl);

  }

  /*

   * 获取随机的字符

   */

  public string getrandomstring( int num){

   return string.valueof(randstring.charat(num));

  }

}

第二步:在 controller 类写前端 ui 的接口

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

/**

* 获取生成验证码显示到 ui 界面

* @param request

* @param response

* @throws servletexception

* @throws ioexception

*/

@requestmapping (value= "/checkcode" )

public void checkcode(httpservletrequest request, httpservletresponse response)

   throws servletexception, ioexception {

//设置相应类型,告诉浏览器输出的内容为图片

  response.setcontenttype( "image/jpeg" );

  //设置响应头信息,告诉浏览器不要缓存此内容

  response.setheader( "pragma" , "no-cache" );

  response.setheader( "cache-control" , "no-cache" );

  response.setdateheader( "expire" , 0 );

  randomvalidatecode randomvalidatecode = new randomvalidatecode();

  try {

   randomvalidatecode.getrandcode(request, response); //输出图片方法

  } catch (exception e) {

   e.printstacktrace();

  }

}

第三步:jsp 页面获取验证码

?

1

<img src= "checkcode" alt= "" width= "100" height= "32" class = "passcode" style= "height:43px;cursor:pointer;" onclick= "this.src=this.src+'?'" >

第四步:我的是在 ssm 环境下实现的,如果不是的话可以自己改写下。

总结

以上所述是小编给大家介绍的ssm 实现登录验证码功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://blog.csdn.net/weidong_y/article/details/80630383

查看更多关于SSM 实现登录验证码功能(附源码)的详细内容...

  阅读:24次