Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the CAPTCHA based on the common component of Yi Shui

2025-02-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use CAPTCHA based on common components of easy water". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use the CAPTCHA based on the common components of Yishui.

Quick start

Introduce related dependencies first

Com.yishuifengxiao.common common-spring-boot-starter 4.1.2

Inject the CAPTCHA tool where the CAPTCHA needs to be used

The injection code is as follows:

@ Autowiredprivate CodeProcessorHolder codeProcessorHolder; graphic CAPTCHA generates graphic CAPTCHA

After injecting a CAPTCHA tool, a graphic CAPTCHA can be generated quickly through the following code. The specific example code is as follows:

@ GetMapping ("/ code/image") @ ResponseBody public Response image (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.IMAGE) .create (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

After the above code, the user can use the

Unique random value of http://ip:port/code/image?image=

Get the graphic CAPTCHA.

In the above request, the user should store the image parameter because it is needed when verifying the CAPTCHA.

Verify graphic CAPTCHA

The example code to verify the graphic CAPTCHA is as follows:

@ GetMapping ("/ image/validate") @ ResponseBody public Response validate (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.IMAGE) .validate (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

The user can use it when verifying the graphic verification code.

The only random value of http://ip:port/image/validate?image= & image_code= CAPTCHA content

To determine whether the CAPTCHA is correct.

[note] the value of the image parameter in the above request should be consistent with that of the image parameter in the url of the graphic CAPTCHA in the request.

Special note: for single applications with front and rear end deployed together, the image parameter in the above two requests can be omitted.

Configuration parameters

In addition, the configurations related to the graphic CAPTCHA are

# length of verification code. Default is the expiration time of 4yishuifengxiao.code.image.length=4# verification code (in seconds). Default is whether 300syishuifengxiao.code.image.expireIn=300# CAPTCHA contains letters, default contains whether yishuifengxiao.code.image.isContainLetter=true# CAPTCHA contains numbers, and default contains the request parameter of yishuifengxiao.code.image.isContainNumber=true# CAPTCHA, the parameter corresponding to yishuifengxiao.code.image.codeKey=image# CAPTCHA, the parameter of yishuifengxiao.code.image.codeValue=image_code# graphic CAPTCHA, the graphic width of yishuifengxiao.code.image.width=70# graphic CAPTCHA, the graphic height of yishuifengxiao.code.image.width=70# graphic CAPTCHA.

Note:

In the request for generating CAPTCHA and CAPTCHA, the image parameter in the request parameter is determined by the yishuifengxiao.code.image.codeKey attribute, and the image_code parameter is determined by the yishuifengxiao.code.image.codeValue attribute. In some extreme cases, request parameters can be modified through these two configurations.

If the service is deployed on Aliyun ECS, because Aliyun closes port 25 by default, sending email verification code using the default port will cause failure. Therefore, you need to submit a ticket to Aliyun to apply for opening port 25, or use port 465.

Mail verification code

When using mailbox verification codes, you need to configure the following first:

Import mail-related dependencies in the project

Org.springframework.boot spring-boot-starter-mail

Add configuration properties related to mail delivery to the configuration file of the project

Spring.mail.default-encoding=UTF-8spring.mail.host= mailbox server spring.mail.username= complete email address spring.mail.password= password spring.mail.port=465spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.connectiontimeout=5000spring.mail.properties.mail.smtp.timeout=3000spring.mail.properties.mail.smtp.writetimeout=5000spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactoryspring.mail.properties.mail.smtp.socketFactory.fallback=falsespring.mail.properties.mail.smtp .socketFactory.port = 465spring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=true

If you do not add the above configuration, you will be prompted [CAPTCHA processor does not exist] when using email CAPTCHA, and the email CAPTCHA will not be sent correctly.

[note] on Ali Cloud ECS server, port 25 is turned off by default, so it is best to configure port 465

Send mail verification code

The sending code of mail verification code is basically the same as that of graphic verification code, and it is only necessary to change CodeType.IMAGE to CodeType.EMAIL.

@ GetMapping ("/ code/email") @ ResponseBody public Response email (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.EMAIL) .create (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

Once you have joined the above configuration, you can use the

Http://ip:port/code/email?email= destination email address

The mailbox verification code has been sent.

Verify mailbox verification code

The sample code is as follows:

@ GetMapping ("/ email/validate") @ ResponseBody public Response validate (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.EMAIL) .validate (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

Once you have joined the above configuration, you can use the

Http://ip:port/email/validate?email= destination email address & email_code= CAPTCHA

To verify.

Configuration parameters

In addition, the component provides some additional configuration

# the length of the verification code, which defaults to the expiration time of the 4yishuifengxiao.code.email.length=4# verification code in seconds. It defaults to whether the 1800syishuifengxiao.code.email.expireIn=1800# verification code contains letters, whether the yishuifengxiao.code.email.isContainLetter=true# verification code contains numbers by default, and the parameter yishuifengxiao.code.email.codeValue=email_code of the value corresponding to the request parameter yishuifengxiao.code.email.codeKey=email# verification code of the yishuifengxiao.code.email.isContainNumber=true# verification code by default.

Similarly, the email parameter in the request parameter of the CAPTCHA is determined by the yishuifengxiao.code.email.codeKey attribute, and the email_code parameter is determined by the yishuifengxiao.code.email.codeValue attribute.

Advanced configuration

In the mail CAPTCHA function, the component makes a default implementation of the mailbox CAPTCHA. When users have custom requirements for the format of the mail CAPTCHA, they can implement their own template content by injecting an instance named emailCodeSender into the spring.

The sample code is as follows

@ Component ("emailCodeSender") public class EmailCodeSender implements CodeSender {@ Override public void send (String target, EmailCode emailCode, CodeType codeType) {/ / implement your own sending logic}} SMS verification code

Due to the particularity of SMS CAPTCHA, different SMS providers have different sending interfaces, so before using the SMS CAPTCHA function, they need to complete their own SMS sender named smsCodeSender and inject it into the spring context.

The sample code is as follows

@ Component ("smsCodeSender") public class SmsCodeSender implements CodeSender {@ Override public void send (String target, EmailCode emailCode, CodeType codeType) {/ / implement your own sending logic}} send SMS verification code

After the pre-configuration is completed, you can send the SMS verification code.

The sample code is as follows

@ GetMapping ("/ code/sms") @ ResponseBody public Response sms (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.SMS) .create (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

Once you have joined the above configuration, you can use the

Http://ip:port/code/sms?phone= target cell phone number

The mailbox verification code has been sent.

Verify SMS verification code

The sample code is as follows:

@ GetMapping ("/ sms/validate") @ ResponseBody public Response validate (HttpServletRequest request, HttpServletResponse response) {try {codeProcessorHolder.findValidateCodeProcessor (CodeType.SMS) .validate (new ServletWebRequest (request,response));} catch (ValidateException e) {return Response.error (e.getMessage ());} return Response.suc ();}

Once you have joined the above configuration, you can use the

Http://ip:port/sms/validate?phone= target cell phone number & phone_code= verification code

To verify.

Parameter configuration # the length of the verification code, which defaults to the expiration time of the 4yishuifengxiao.code.sms.length=4# verification code in seconds. It defaults to whether the 300syishuifengxiao.code.sms.expireIn=300# verification code contains letters, whether the yishuifengxiao.code.sms.isContainLetter=true# verification code contains numbers by default, and the parameter yishuifengxiao.code.sms.codeValue=phone_code of the value corresponding to the request parameter yishuifengxiao.code.sms.codeKey=phone# verification code of the yishuifengxiao.code.sms.isContainNumber=true# verification code by default.

Similarly, the phone parameter in the request parameter of the CAPTCHA is determined by the yishuifengxiao.code.sms.codeKey attribute, and the phone_code parameter is determined by the yishuifengxiao.code.sms.codeValue attribute.

For more use of CAPTCHA, see Resources.

At this point, I believe you have a deeper understanding of "how to use the CAPTCHA based on the common components of easy water". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report