SpringBoot集成kaptcha验证码

2025-05-29 0 84

本文实例为大家分享了springboot集成kaptcha验证码的具体代码,供大家参考,具体内容如下

1.kaptcha相关介绍

kaptcha是一个基于simplecaptcha的验证码开源项目。

2.集成方案

①pom.xml中配置依赖

?

1

2

3

4

5

6
<!-- 验证码-->

<dependency>

<groupid>com.github.penggle</groupid>

<artifactid>kaptcha</artifactid>

<version>2.3.2</version>

</dependency>

②配置验证码kaptcha相关设置

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
@configuration

public class kaptchaconfig {

@bean(name="captchaproducer")

public defaultkaptcha getkaptchabean(){

defaultkaptcha defaultkaptcha=new defaultkaptcha();

properties properties=new properties();

properties.setproperty("kaptcha.border", "yes");

properties.setproperty("kaptcha.border.color", "105,179,90");

properties.setproperty("kaptcha.textproducer.font.color", "blue");

properties.setproperty("kaptcha.image.width", "125");

properties.setproperty("kaptcha.image.height", "45");

properties.setproperty("kaptcha.session.key", "code");

properties.setproperty("kaptcha.textproducer.char.length", "4");

properties.setproperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");

config config=new config(properties);

defaultkaptcha.setconfig(config);

return defaultkaptcha;

}

}

或者

在resources下创建mykaptcher.xml文件

?

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
<?xml version="1.0" encoding="utf-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"

xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="captchaproducer" class="com.google.code.kaptcha.impl.defaultkaptcha">

<property name="config">

<bean class="com.google.code.kaptcha.util.config">

<constructor-arg type="java.util.properties">

<props>

<prop key = "kaptcha.border ">yes</prop>

<prop key="kaptcha.border.color">105,179,90</prop>

<prop key="kaptcha.textproducer.font.color">blue</prop>

<prop key="kaptcha.image.width">100</prop>

<prop key="kaptcha.image.height">50</prop>

<prop key="kaptcha.textproducer.font.size">27</prop>

<prop key="kaptcha.session.key">code</prop>

<prop key="kaptcha.textproducer.char.length">4</prop>

<prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>

<prop key="kaptcha.textproducer.char.string">23456789abcefghjkmnopqrstuvwxyz</prop>

<prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.waterripple</prop>

<prop key="kaptcha.noise.color">black</prop>

<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.nonoise</prop>

<!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.defaultnoise</prop>-->

<prop key="kaptcha.background.clear.from">185,56,213</prop>

<prop key="kaptcha.background.clear.to">white</prop>

<prop key="kaptcha.textproducer.char.space">3</prop>

</props>

</constructor-arg>

</bean>

</property>

</bean>

</beans>

然后在启动类application中加载配置

?

1

2

3

4

5

6

7

8

9

10

11

12
@enabletransactionmanagement// 启动注解事务管理,等同于xml配置方式的 <tx:annotation-driven />

@springbootapplication

@enablescheduling//启动注解定时任务

@mapperscan(basepackages = "com.shawn.mapper")

@importresource(locations={"classpath:mykaptcha.xml"})

public class application extends springbootservletinitializer {

public static void main(string[] args) throws exception {

springapplication.run(application.class, args);

}

}

两种配置方式在springboot中均可;

kaptchacontroller

?

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
@commonslog

@controller

public class kaptchacontroller extends basecontroller {

@autowired

private producer captchaproducer;

@getmapping("/getkaptchaimage")

public void getkaptchaimage() throws exception {

response.setdateheader("expires", 0);

// set standard http/1.1 no-cache headers.

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

// set ie extended http/1.1 no-cache headers (use addheader).

response.addheader("cache-control", "post-check=0, pre-check=0");

// set standard http/1.0 no-cache header.

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

// return a jpeg

response.setcontenttype("image/jpeg");

// create the text for the image

string captext = captchaproducer.createtext();

// store the text in the session

//request.getsession().setattribute(constants.kaptcha_session_key, captext);

//将验证码存到session

session.setattribute(constants.kaptcha_session_key, captext);

log.info(captext);

// create the image with the text

bufferedimage bi = captchaproducer.createimage(captext);

servletoutputstream out = response.getoutputstream();

// write the data out

imageio.write(bi, "jpg", out);

try {

out.flush();

} finally {

out.close();

}

}

}

3.测试效果

SpringBoot集成kaptcha验证码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:https://blog.csdn.net/zhangxing52077/article/details/75270259

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot集成kaptcha验证码 https://www.kuaiidc.com/111294.html

相关文章

发表评论
暂无评论