如何用两个数字的总和来编写验证码?例如:1 + 4 (请完全回答这个问题,我对captcha一无所知!)
答案 0 :(得分:3)
验证码的目的是通过自动化软件区分真实的人类。
所以验证码要求人们做人擅长的东西,但机器不。一旦机器具备某些功能,captac就必须改变并做其他事情。简单的文本不再起作用了,因为OCR已经足够自动地读取它了 - 因此,你现在常常看到扭曲的文本。可能会在不久的将来被其他东西取代,比如“这张照片中显示的是什么类型的东西”等等。
计算机的任务越简单,就越不适合使用Captcha。
添加两个数字对于验证码不是一个好主意;事实上,它的有效性可能低于零。
请再次检查您要执行的操作以及原因。如果你想编写自己的验证码而不是使用已经存在的东西,那么在实现之前首先考虑一下。
使用ReCAPTCHA或something similar可能是更好的方法吗?
答案 1 :(得分:1)
CAPTCHA is an acronym for:
Completely
Automated
Public
Turing-test to tell
Computers and
Humans
Apart
创建CAPTCHA代码是为了阻止自动计算机垃圾邮件机器人填写表单,收集电子邮件地址,然后发送无数垃圾邮件。
答案 2 :(得分:0)
您需要使用以下 jar 文件:
<dependency>
<groupId>cn.apiclub.tool</groupId>
<artifactId>simplecaptcha</artifactId>
<version>1.2.2</version>
</dependency>
然后需要扩展接口类TextProducer
package com.java.captcha;
import java.util.Random;
import cn.apiclub.captcha.text.producer.TextProducer;
public class MyTextProducer implements TextProducer {
private int answer;
@Override
public String getText() {
Random random = new Random();
int random1 = random.nextInt(90)+10;
int random2 = random.nextInt(90)+10;
int sum = random1 + random2;
this.setAnswer(sum);
StringBuffer captchaStringBuffer = new StringBuffer();
captchaStringBuffer.append(random1 + "+" + random2 + "=" + "?");
return captchaStringBuffer.toString();
}
public int getAnswer() {
return answer;
}
public void setAnswer(int answer) {
this.answer = answer;
}
}
您需要创建实现 Serializable 接口的类 CaptchaDetails
package com.java.captcha;
import java.io.Serializable;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.util.WebUtils;
import cn.apiclub.captcha.Captcha;
public class CaptchaDetails implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4326389569050611057L;
private final Captcha captcha;
public CaptchaDetails(HttpServletRequest request){
this.captcha=(Captcha) WebUtils.getSessionAttribute(request, "captcha");
}
public Captcha getCaptcha() {
return captcha;
}
}
You need to create class CaptchaGenerator which implements InitializingBean interface
package com.java.captcha;
import org.springframework.beans.factory.InitializingBean;
import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.backgrounds.BackgroundProducer;
import cn.apiclub.captcha.backgrounds.TransparentBackgroundProducer;
import cn.apiclub.captcha.noise.CurvedLineNoiseProducer;
import cn.apiclub.captcha.noise.NoiseProducer;
import cn.apiclub.captcha.text.producer.DefaultTextProducer;
import cn.apiclub.captcha.text.producer.TextProducer;
import cn.apiclub.captcha.text.renderer.DefaultWordRenderer;
import cn.apiclub.captcha.text.renderer.WordRenderer;
public class CaptchaGenerator implements InitializingBean {
private BackgroundProducer backgroundProducer=new TransparentBackgroundProducer();
private TextProducer textProducer=new MyTextProducer();
private WordRenderer wordRenderer= new DefaultWordRenderer();
private NoiseProducer noiseProducer=new CurvedLineNoiseProducer();
public Captcha createCaptcha(int width, int height){
return new Captcha.Builder(width, height)
.addBackground(backgroundProducer)
.addText(textProducer,wordRenderer)
.addNoise(noiseProducer).build();
}
@Override
public void afterPropertiesSet() throws Exception {
if(this.backgroundProducer==null){
this.backgroundProducer=new TransparentBackgroundProducer();
}
if(this.textProducer==null){
this.textProducer=new DefaultTextProducer();
}
if(this.wordRenderer==null){
this.wordRenderer= new DefaultWordRenderer();
}
if(this.noiseProducer==null){
this.noiseProducer=new CurvedLineNoiseProducer();
}
}
}
您需要创建类 CaptchaUtils
package com.java.captcha;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;
import cn.apiclub.captcha.Captcha;
public abstract class CaptchaUtils {
public static String encodeBase64(Captcha captcha){
try {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ImageIO.write(captcha.getImage(), "png", outputStream);
return DatatypeConverter.printBase64Binary(outputStream.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
你需要在包含@SpringBootApplication注解的springboot类中添加@Bean属性
@Bean
public CaptchaGenerator getCaptchaGenerator(){
return new CaptchaGenerator();
}
现在您可以在 dataProvider 中编写以下代码
package com.excelUpload.excelToOracle;
import java.util.HashMap;
import java.util.Map;
import static com.java.baseutil.ApplicationConatants.SUCCESS;
import javax.servlet.http.HttpSession;
import com.java.captcha.CaptchaGenerator;
import com.java.captcha.CaptchaUtils;
import cn.apiclub.captcha.Captcha;
import oracle.jdbc.logging.annotations.Logging;
public class XYZDataprovider{
public Map<String, ?> getData(String dpMethod, HttpSession session, Map<?, ?> allRequestParams, Object masterData,
ServiceProvider serviceProvider, Logging logging) {
Map<String, Object>responseHashMap = new HashMap<String,Object>();
Map<String,Object>dataHashMap = new HashMap<String,Object>();
if(dpMethod.equals("getCaptchaDet")){
try {
CaptchaGenerator captchaGenerator = new CaptchaGenerator();
Captcha captcha = captchaGenerator.createCaptcha(200, 50);
String str1="data:image/jpeg;base64,"+CaptchaUtils.encodeBase64(captcha);
dataHashMap.put("captchaAns", captchaGenerator.getTextProducer().getAnswer());
dataHashMap.put("captchaCode", str1);
responseHashMap.put("responseData", dataHashMap);
responseHashMap.put(SUCCESS, true);
} catch (Exception e) {
e.printStackTrace();
}
}
return responseHashMap;
}
}