开发者

captcha with sum of two numbers

how can I write captcha with sum of two numbers ?exa开发者_如何学Cmple:1+4 (please answer to this question completely,I don't know any thing about captcha!)


The purpose of a captcha is to tell apart real, live humans from automated software.

So captchas ask people to do stuff people are good at, but machines are not. Once machines get capable of something, captachas have to change and do something else. Simple text isn't working anymore, since OCR got good enough to automatically read it - hence, the distorted text you see often nowadays. Will probably be replaced by something else in the near future, like "what type of thing is shown in this picture" or such.

The easier the task for a computer, the less suited it is for a Captcha.

Adding two numbers is not a good idea for a captcha; in fact, its effectiveness is probably below zero.

Please check again what you want to do and why. If you want to write your own captcha instead of using something that's already there, put some thought in it first before implementation.

Maybe using ReCAPTCHA or something similar is the better way to go about it?


CAPTCHA is an acronym for:

Completely    
Automated    
Public    
Turing-test to tell    
Computers and    
Humans    
Apart

CAPTCHA code was created to stop automated computer spam robots from filling out forms, harvesting email addresses, and then sending out countless spam emails.


You need to use the following jar file:

<dependency>
    <groupId>cn.apiclub.tool</groupId>
    <artifactId>simplecaptcha</artifactId>
    <version>1.2.2</version>
</dependency>

Then you need to extend the interface class 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;
    }

}

You need to create class CaptchaDetails which implements the Serializable interface

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();
            }
    
        }
    
    }

You need to create class 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;
    }
}

You need to add @Bean attribute to your springboot class containing @SpringBootApplication annotation

@Bean
    public CaptchaGenerator getCaptchaGenerator(){
        return new CaptchaGenerator();
    }

Now you can write the following code in your 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;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜