SpringBoot集成kaptcha驗(yàn)證碼
本文實(shí)例為大家分享了SpringBoot集成kaptcha驗(yàn)證碼的具體代碼,供大家參考,具體內(nèi)容如下
1.kaptcha相關(guān)介紹
Kaptcha是一個(gè)基于SimpleCaptcha的驗(yàn)證碼開源項(xiàng)目。
2.集成方案
①pom.xml中配置依賴
<!-- 驗(yàn)證碼--> <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
②配置驗(yàn)證碼Kaptcha相關(guān)設(shè)置
@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下創(chuàng)建myKaptcher.xml文件
<?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>
然后在啟動(dòng)類Application中加載配置
@EnableTransactionManagement// 啟動(dòng)注解事務(wù)管理,等同于xml配置方式的 <tx:annotation-driven /> @SpringBootApplication @EnableScheduling//啟動(dòng)注解定時(shí)任務(wù) @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
@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); //將驗(yàn)證碼存到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.測(cè)試效果
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringBoot整合kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
- SpringBoot+kaptcha實(shí)現(xiàn)驗(yàn)證碼花式玩法詳解
- Springboot?+redis+谷歌開源Kaptcha實(shí)現(xiàn)圖片驗(yàn)證碼功能
- springboot整合kaptcha生成驗(yàn)證碼功能
- Google Kaptcha 框架實(shí)現(xiàn)登錄驗(yàn)證碼功能(SSM 和 SpringBoot)
- springboot整合kaptcha驗(yàn)證碼的示例代碼
- SpringBoot 集成Kaptcha實(shí)現(xiàn)驗(yàn)證碼功能實(shí)例詳解
- SpringBoot使用Kaptcha實(shí)現(xiàn)驗(yàn)證碼的生成與驗(yàn)證功能
相關(guān)文章
Spring?Boot?整合?Fisco?Bcos部署、調(diào)用區(qū)塊鏈合約的案例
本篇文章介紹?Spring?Boot?整合?Fisco?Bcos?的相關(guān)技術(shù),最最重要的技術(shù)點(diǎn),部署、調(diào)用區(qū)塊鏈合約的工程案例,本文通過流程分析給大家介紹的非常詳細(xì),需要的朋友參考下吧2022-01-01Java兩大工具庫(kù)Commons和Guava使用示例詳解
這篇文章主要為大家介紹了Java兩大工具庫(kù)Commons和Guava使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Javaweb項(xiàng)目session超時(shí)解決方案
這篇文章主要介紹了Javaweb項(xiàng)目session超時(shí)解決方案,關(guān)于解決方案分類比較明確,內(nèi)容詳細(xì),需要的朋友可以參考下。2017-09-09IDEA中Maven報(bào)錯(cuò)Cannot resolve xxx的解決方法匯總(親測(cè)有效)
在IDEA中的pom文件中添加了依賴,并且正確加載了相應(yīng)依賴,pom文件沒有報(bào)紅,看起來像是把所有依賴庫(kù)全部加載進(jìn)來了,但是代碼中使用依賴的類庫(kù)使報(bào)紅,本文給大家介紹了IDEA中Maven報(bào)錯(cuò)Cannot resolve xxx的解決方法匯總,需要的朋友可以參考下2024-06-06springboot解決使用localhost或127.0.01模擬CORS失效
CORS允許不同源的網(wǎng)頁請(qǐng)求訪問另一個(gè)源服務(wù)器上的某些資源,本文主要介紹了springboot解決使用localhost或127.0.01模擬CORS失效,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07