导入以下依赖
<!--图片验证码--><dependency><groupId>com.github.whvcse</groupId><artifactId>easy-captcha</artifactId><version>1.6.2</version></dependency>
编写controller
package com.anXin.user.controller;import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;@Controller
public class KaptchaController {private static final Logger logger = LoggerFactory.getLogger(KaptchaController.class);public static HttpSession session;@RequestMapping(path = "/kaptcha",method = RequestMethod.GET)public void getKaptcha(HttpServletResponse response, HttpSession session) throws Exception {Random r=new Random();int a = r.nextInt(5) + 1;switch (a){case 1:get1(response, session);break;case 2:get2(response, session);break;case 3:get3(response, session);break;case 4:get4(response, session);break;case 5:get5(response, session);break;}}private void get1(HttpServletResponse response, HttpSession session){SpecCaptcha captcha = new SpecCaptcha(130, 40);System.out.println("验证码为:"+captcha.text());session.setAttribute("captcha",captcha.text());response.setContentType("image/png");try {OutputStream os = response.getOutputStream();captcha.out(os);} catch (IOException e) {logger.error("响应验证码失败" + e.getMessage());}}private void get2(HttpServletResponse response, HttpSession session) throws Exception {GifCaptcha gifCaptcha = new GifCaptcha(130, 40);gifCaptcha.setCharType(Captcha.TYPE_ONLY_UPPER);//设置字体gifCaptcha.setFont(Captcha.FONT_5);System.out.println("验证码为:"+gifCaptcha.text());session.setAttribute("captcha",gifCaptcha.text());response.setContentType("image/gif");OutputStream os = response.getOutputStream();gifCaptcha.out(os);}private void get3(HttpServletResponse response, HttpSession session){ChineseCaptcha chineseCaptcha = new ChineseCaptcha(130, 40);System.out.println("验证码为:"+chineseCaptcha.text());session.setAttribute("captcha",chineseCaptcha.text());response.setContentType("image/png");try {OutputStream os = response.getOutputStream();chineseCaptcha.out(os);} catch (IOException e) {logger.error("响应验证码失败" + e.getMessage());}}private void get4(HttpServletResponse response, HttpSession session){ChineseGifCaptcha chineseGifCaptcha = new ChineseGifCaptcha(130, 40);System.out.println("验证码为:"+chineseGifCaptcha.text());session.setAttribute("captcha",chineseGifCaptcha.text());response.setContentType("image/gif");try {OutputStream os = response.getOutputStream();chineseGifCaptcha.out(os);} catch (IOException e) {logger.error("响应验证码失败" + e.getMessage());}}private void get5(HttpServletResponse response, HttpSession session){ArithmeticCaptcha arithmeticCaptcha = new ArithmeticCaptcha(180, 40);//设置几位数的运算,几个数计算arithmeticCaptcha.setLen(3);//打印算式System.out.println("算式为:"+arithmeticCaptcha.getArithmeticString());System.out.println("验证码为:"+arithmeticCaptcha.text());session.setAttribute("captcha",arithmeticCaptcha.text());response.setContentType("image/png");try {OutputStream os = response.getOutputStream();arithmeticCaptcha.out(os);} catch (IOException e) {logger.error("响应验证码失败" + e.getMessage());}}
}
以上为随机生成五种不同效果的验证码图片,可以根据自己的需求灵活改写
测试结果