谷歌驗證碼


谷歌驗證碼

引入依賴

驗證碼圖片工具類依賴
<dependency>            
<groupId>com.github.axet</groupId>
     <artifactId>kaptcha</artifactId>            
     <version>0.0.9</version>
</dependency>

配置

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
​
import java.util.Properties;
@Configuration
public class KaptchaConfig {
    @Bean
    public DefaultKaptcha producer()
    {      Properties properties = new Properties();
    properties.put("kaptcha.border", "no");
    properties.put("kaptcha.textproducer.font.color", "black");
    properties.put("kaptcha.textproducer.char.space", "4");
    properties.put("kaptcha.image.height", "40");
    properties.put("kaptcha.image.width", "120");
    properties.put("kaptcha.textproducer.font.size", "30");
    Config config = new Config(properties);
    DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
    defaultKaptcha.setConfig(config);
    return defaultKaptcha;
    }
}

生成驗證碼

//key
String key= UUID.randomUUID().toString();
//驗證碼
String code=producer.createText();
BufferedImage image=producer.createImage(code);
ByteArrayOutputStream op=new ByteArrayOutputStream();
try {
    ImageIO.write(image,"jpg",op);
} catch (IOException e) {
    e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
String str = "data:image/jpeg;base64,";
String base64Img = str + encoder.encode(op.toByteArray());

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM