图片验证码和阿里云手机短信服务

记录一下最近关于登录过程中,图片验证码和阿里的手机短信功能。

1.图片验证码

图片验证码是使用com.google.code.kaptcha第三方包来实现的,但是由于是google的jar包,所以在国内,Maven是下载不了,但好在有大佬在github中分享了(感谢大佬!,感谢开源!)。
Maven中依赖:

<dependency>
	<groupId>com.github.penggle</groupId>
	<artifactId>kaptcha</artifactId>
    <version>2.3.2</version>
</dependency>

主要代码:

@Autowired
private Producer producer;
@RequestMapping("captcha")
public void captcha(HttpServletResponse response) throws IOException{
   
   
//生成文字验证码
String text = producer.createText();
//生成图片验证
BufferedImage image = producer.createImage(text);
//一般需要将验证码放到redis中,供之后验证使用
//J2CacheUtils.put(Constants.KAPTCHA_SESSION_KEY,text);
//将图片传到前台
ServletOutputStream out = response.getOutputStream();
ImageIo.write(image,"jpg",out);
}

在这过程中会出现Producer的自动装载问题,提示Producer不能自动装载,这里解决的方案就是手动装载。
编写WebConfig工具类

import java.util.Properties;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.util.Config;

@Configuration
public class WebConfig {
   
   

    @Bean
    public Producer KaptchaProducer() {
   
   
        Properties kaptchaProperties = new Properties();
        kaptchaProperties.put("kaptcha.border"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值