package com.kmall.common.security.verif;

import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.Properties;

import static com.google.code.kaptcha.Constants.*;

/**
 * Kaptcha验证码生成器
 * @author Scott Chen
 * @since 1.0
 * 2019-03-08
 */
@Component
public class VerificationCode {

    /**
     * 生成Kaptcha验证码生成
     * @return
     */
    @Bean
    public Producer producer() {
        Properties properties = new Properties();
        properties.setProperty(KAPTCHA_BORDER, "no");
        properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "4");
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
        properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_STRING, "123456789");

        DefaultKaptcha kaptcha = new DefaultKaptcha();
        Config config = new Config(properties);
        kaptcha.setConfig(config);
        return kaptcha;
    }
}
