이 기사에서는 참조에 대한 SpringBoot 통합 Kaptcha Verification Code의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
1. Kaptcha 관련 소개
Kaptcha는 Simplecaptcha를 기반으로 한 검증 코드를위한 오픈 소스 프로젝트입니다.
2. 통합 솔루션
pom.xml의 구성 종속성
<!-검증 코드-> <pectionency> <groupId> com.github.penggle </groupid> <artifactid> kaptcha </artifactid> <bersion> 2.3.2 </version> </fectionency>
configure 확인 코드 KAPTCHA 관련 설정
@ConfigurationPublic 클래스 KaptChaconFig {@Bean (이름 = "CaptChaProducer") public defaultKaptcha getKaptChabean () {defaultKaptcha defaultKaptcha = new defaultKaptcha (); 속성 속성 = 새로운 속성 (); 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"); 속성 .setProperty ( "kaptcha.session.key", "code"); Properties.setProperty ( "Kaptcha.textProducer.char.length", "4"); properties.setProperty ( "kaptcha.textproducer.font.names", "송 글꼴, Kai Font, Microsoft Yahei"); 구성 구성 = 새 구성 (속성); defaultKaptcha.setConfig (config); DefaultKaptcha를 반환합니다. }}또는
리소스에서 mykaptcher.xml 파일을 만듭니다
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.w3.org/2001/xmlschema-instance" XSI : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "captchapruducer"> property name = "bean> <bean> <bean> 구성 type = "java.util.properties"> <props> <prop key = "kaptcha.border"> 예 </prop> <prop key = "kaptcha.border.color"> 105,179,90 </prop> <prop key = "kaptcha.textProducer.font.color"> blue </prop> key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height"> 50 </prop key = "kaptcha. height. key = "kaptcha.textproducer.font.color"> blue </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.textProficer.scize"> 27 </prop> <prop key = "kaptcha.session.key"> code </proc> <prop key = "kaptcha.textProducer.char.length"> 4 </prop> <prop key = "kaptcha.textProducer.font.names"> 송 글꼴, Kai Font, Microsoft Yahei </prop> <prop key = "kaptcha.textProducer.char.String"> 23456789ABCEFGHJKMNOPQRSTUVWXYZ </prop> <prop key = "kaptcha.obscurificator.impl"> com.google.code.kaptcha.impl.waterripple </prop> <prop key = "kaptcha.nooise.nooise.noofo 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> </bean>> </bean>> </bean>.
그런 다음 시작 클래스 응용 프로그램에 구성을로드하십시오
@enableTransactionManagement // XML 구성 메소드와 동등한 주석 트랜잭션 관리를 시작합니다. <tx : annotation-driven/>@springbootApplication@enablescheduling // 주석 타이밍 작업 @mapperscan (basepackages = "com.shawn.mapper")@importresource (locations = { "classpath : mykaptcha.xml"}) 공개 클래스 응용 프로그램을 확장합니다. }}두 구성 방법 모두 SpringBoot에서 사용할 수 있습니다.
Kaptchacontroller
@commonslog @controllerpublic class kaptchacontroller는 basecontroller를 확장합니다. @GetMapping ( "/getKaptChaimage") public void getKaptChaimage ()는 예외 {response.setDateHeader ( "expires", 0); // 표준 http/1.1없는 캐시 헤더를 설정합니다. response.setHeader ( "캐시 제어", "무자개, 없음, 캐시가 없음, 꼭 봐야 할 반복"); // IE 확장 HTTP/1.1 No-Cache 헤더를 설정합니다 (AddHeader 사용). response.addheader ( "캐시 제어", "post-check = 0, pre-Check = 0"); // 표준 HTTP/1.0 NO 캐시 헤더를 설정합니다. Response.SetHeader ( "Pragma", "No-Cache"); // jpeg response를 반환합니다 .SetContentType ( "image/jpeg"); // 이미지 문자열에 대한 텍스트 생성 captext = captchaproducer.createtext (); // 세션에 텍스트를 저장 //request.getSession().setattribute(constants.kaptcha_session_key, captext); // 검증 코드를 세션 세션에 저장합니다 .SetAttribute (constants.kaptcha_session_key, captext); log.info (captext); // 텍스트 bufferedImage bi = captchaproducer.createimage (captext)로 이미지를 만듭니다. servletoutputStream out = response.getOutputStream (); // 데이터를 꺼내면 ImageIO.Write (BI, "JPG", OUT); {out.flush (); } 마침내 {out.close (); }}}3. 효과를 테스트하십시오
위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.