验证码工具

断鸿 2019年07月15日 125次浏览
		<dependency>
			<groupId>com.github.penggle</groupId>
			<artifactId>kaptcha</artifactId>
			<version>2.3.2</version>
		</dependency>
    /**
     * 配置生成验证码bean
     * @return
     */
    @Bean
    public DefaultKaptcha defaultKaptcha() {

        DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
        Properties properties = new Properties();
        properties.setProperty("kaptcha.border", "yes");
        properties.setProperty("kaptcha.border.color", "105,179,90");
        properties.setProperty("kaptcha.textproducer.font.color", "blue");
        properties.setProperty("kaptcha.image.width", "110");
        properties.setProperty("kaptcha.image.height", "40");
        properties.setProperty("kaptcha.textproducer.font.size", "30");
        properties.setProperty("kaptcha.session.key", "code");
        properties.setProperty("kaptcha.textproducer.char.length", "4");
        properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
        Config config = new Config(properties);
        defaultKaptcha.setConfig(config);
        return defaultKaptcha;
    }
//获取验证码图片
		@RequestMapping("back/validateCode")
    public void validateCode(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {

        String validateCode = defaultKaptcha.createText();
        ImageCode imageCode = new ImageCode(defaultKaptcha.createImage(validateCode), validateCode, 60);
        session.setAttribute(SESSION_KEY_VERIFY_CODE, imageCode);
        ImageIO.write(imageCode.getImage(), "JPEG", response.getOutputStream());

    }