SpringBoot 實現圖形驗證碼

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

  把kaptcha做爲工程的一個類,加上@Configration註解在返回kaptcha的方法中加上@Bean註解前端

/**
 * Created by mhSui on 2019/12/05.
 *
 * @author mhSui
 */
@Configuration
public class KaptchaConfig {

	private final static String CODE_LENGTH = "4";

	private final static String SESSION_KEY = "handsome_yang";

	@Bean
	public DefaultKaptcha defaultKaptcha() {
		DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
		Properties properties = new Properties();
		// 設置邊框,合法值:yes , no
		properties.setProperty("kaptcha.border", "yes");
		// 設置邊框顏色,合法值: r,g,b (and optional alpha) 或者 white,
		properties.setProperty("kaptcha.border.color", "105,179,90");
		// 設置字體顏色, r,g,b 或者 white,black,blue.
		properties.setProperty("kaptcha.textproducer.font.color", "blue");
		// 設置圖片寬度
		properties.setProperty("kaptcha.image.width", "118");
		// 設置圖片高度
		properties.setProperty("kaptcha.image.height", "40");
		// 設置字體尺寸
		properties.setProperty("kaptcha.textproducer.font.size", "30");
		// 設置session key
		properties.setProperty("kaptcha.session.key", SESSION_KEY);
		// 設置驗證碼長度
		properties.setProperty("kaptcha.textproducer.char.length", CODE_LENGTH);
		// 設置字體
		properties.setProperty("kaptcha.textproducer.font.names", "楷體");
		Config config = new Config(properties);
		defaultKaptcha.setConfig(config);
		return defaultKaptcha;
	}

}

  下面是我調用封裝到對象返回給前端,由於前端緣由,這樣我這裏返回base64的方式java

/**
 * PicKaptchaUtil.
 *
 * @author mhSui
 */
@Slf4j
@Component
public class PicKaptchaUtil {

	@Autowired
	private DefaultKaptcha defaultKaptcha;

	@Autowired
	private RedisClient redisTemplate;

	/**
	 * 生成驗證碼.
	 * @param request request
	 * @param response response
	 * @return CaptchaDTO
	 */
	public CaptchaDTO kaptcha(HttpServletRequest request, HttpServletResponse response) {
		// 獲取sessionId
		CaptchaDTO captchaDTO = new CaptchaDTO();
		String key = "JQ" + request.getSession().getId();
		// 生產驗證碼字符串
		String createText = this.defaultKaptcha.createText();
		// 使用生產的驗證碼字符串返回一個BufferedImage對象並轉爲byte寫入到byte數組中
		BufferedImage bufferedImage = this.defaultKaptcha.createImage(createText);
		ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
		try {
			// 使用生產的驗證碼字符串返回一個BufferedImage
			ImageIO.write(bufferedImage, "jpeg", jpegOutputStream);
			Base64.Encoder encoder = Base64.getEncoder();
			String base64 = encoder.encodeToString(jpegOutputStream.toByteArray());
			String captchaBase64 = "data:image/jpeg;base64,"
					+ base64.replaceAll("\r\n", "");

			captchaDTO.setCaptchaBase64(captchaBase64);
			captchaDTO.setKey(key);

			// 存儲到redis,過時時間30分鐘
			this.redisTemplate.set(key, createText);
			this.redisTemplate.expire(key, 15 * 60);

			log.info("圖形驗證碼" + createText);
		}
		catch (IOException e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		finally {
			try {
				if (jpegOutputStream != null) {
					jpegOutputStream.close();
				}
			}
			catch (IOException e) {
				e.printStackTrace();
			}
		}
		return captchaDTO;
	}

	/**
	 * 校驗驗證碼.
	 * @param key kaptcha-key
	 * @param kaptcha kaptcha
	 * @return 校驗結果
	 */
	public boolean check(String key, String kaptcha) {
		String redisKaptcha = this.redisTemplate.get(key);
		return (!StringUtils.isEmpty(kaptcha)) && kaptcha.equals(redisKaptcha);
	}

}

  這就是我項目中運用的kaptcha圖形驗證碼git

相關文章
相關標籤/搜索