mirror of
https://gitee.com/y_project/RuoYi-Cloud.git
synced 2026-01-26 19:51:56 +08:00
[feat] 数字验证码生成器
This commit is contained in:
@@ -80,6 +80,22 @@
|
|||||||
<artifactId>ruoyi-common-redis</artifactId>
|
<artifactId>ruoyi-common-redis</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.ujcms</groupId>
|
||||||
|
<artifactId>ujcms-commons</artifactId>
|
||||||
|
<version>9.6.0</version>
|
||||||
|
<exclusions>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
<exclusion>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-web</artifactId>
|
||||||
|
</exclusion>
|
||||||
|
</exclusions>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- Swagger -->
|
<!-- Swagger -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.springfox</groupId>
|
<groupId>io.springfox</groupId>
|
||||||
@@ -102,6 +118,11 @@
|
|||||||
<version>${kotlin.version}</version>
|
<version>${kotlin.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib-jdk8</artifactId>
|
||||||
|
<version>${kotlin.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@@ -151,6 +172,13 @@
|
|||||||
<goals>
|
<goals>
|
||||||
<goal>compile</goal>
|
<goal>compile</goal>
|
||||||
</goals>
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sourceDirs>
|
||||||
|
<source>src/main/java</source>
|
||||||
|
<source>src/main/kotlin</source>
|
||||||
|
<source>target/generated-sources/annotations</source>
|
||||||
|
</sourceDirs>
|
||||||
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
<execution>
|
<execution>
|
||||||
<id>test-compile</id>
|
<id>test-compile</id>
|
||||||
@@ -158,6 +186,12 @@
|
|||||||
<goals>
|
<goals>
|
||||||
<goal>test-compile</goal>
|
<goal>test-compile</goal>
|
||||||
</goals>
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<sourceDirs>
|
||||||
|
<source>src/test/kotlin</source>
|
||||||
|
<source>target/generated-test-sources/test-annotations</source>
|
||||||
|
</sourceDirs>
|
||||||
|
</configuration>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
<configuration>
|
<configuration>
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
package com.ruoyi.gateway.config;
|
package com.ruoyi.gateway.config;
|
||||||
|
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
import com.google.code.kaptcha.impl.DefaultKaptcha;
|
||||||
import com.google.code.kaptcha.util.Config;
|
import com.google.code.kaptcha.util.Config;
|
||||||
|
|
||||||
import static com.google.code.kaptcha.Constants.*;
|
import static com.google.code.kaptcha.Constants.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -13,11 +15,9 @@ import static com.google.code.kaptcha.Constants.*;
|
|||||||
* @author ruoyi
|
* @author ruoyi
|
||||||
*/
|
*/
|
||||||
@Configuration
|
@Configuration
|
||||||
public class CaptchaConfig
|
public class CaptchaConfig {
|
||||||
{
|
|
||||||
@Bean(name = "captchaProducer")
|
@Bean(name = "captchaProducer")
|
||||||
public DefaultKaptcha getKaptchaBean()
|
public DefaultKaptcha getKaptchaBean() {
|
||||||
{
|
|
||||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
@@ -37,15 +37,14 @@ public class CaptchaConfig
|
|||||||
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
||||||
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||||
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
|
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.ShadowGimpy.class.getName());
|
||||||
Config config = new Config(properties);
|
Config config = new Config(properties);
|
||||||
defaultKaptcha.setConfig(config);
|
defaultKaptcha.setConfig(config);
|
||||||
return defaultKaptcha;
|
return defaultKaptcha;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean(name = "captchaProducerMath")
|
@Bean(name = "captchaProducerMath")
|
||||||
public DefaultKaptcha getKaptchaBeanMath()
|
public DefaultKaptcha getKaptchaBeanMath() {
|
||||||
{
|
|
||||||
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
Properties properties = new Properties();
|
Properties properties = new Properties();
|
||||||
// 是否有边框 默认为true 我们可以自己设置yes,no
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
@@ -63,7 +62,7 @@ public class CaptchaConfig
|
|||||||
// KAPTCHA_SESSION_KEY
|
// KAPTCHA_SESSION_KEY
|
||||||
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
|
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCodeMath");
|
||||||
// 验证码文本生成器
|
// 验证码文本生成器
|
||||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, "com.ruoyi.gateway.config.KaptchaTextCreator");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, com.ruoyi.gateway.config.KaptchaTextCreator.class.getName());
|
||||||
// 验证码文本字符间距 默认为2
|
// 验证码文本字符间距 默认为2
|
||||||
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_SPACE, "3");
|
||||||
// 验证码文本字符长度 默认为5
|
// 验证码文本字符长度 默认为5
|
||||||
@@ -73,9 +72,38 @@ public class CaptchaConfig
|
|||||||
// 验证码噪点颜色 默认为Color.BLACK
|
// 验证码噪点颜色 默认为Color.BLACK
|
||||||
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
|
properties.setProperty(KAPTCHA_NOISE_COLOR, "white");
|
||||||
// 干扰实现类
|
// 干扰实现类
|
||||||
properties.setProperty(KAPTCHA_NOISE_IMPL, "com.google.code.kaptcha.impl.NoNoise");
|
properties.setProperty(KAPTCHA_NOISE_IMPL, com.google.code.kaptcha.impl.NoNoise.class.getName());
|
||||||
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||||
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy");
|
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.ShadowGimpy.class.getName());
|
||||||
|
Config config = new Config(properties);
|
||||||
|
defaultKaptcha.setConfig(config);
|
||||||
|
return defaultKaptcha;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean(name = "captchaProducerNumber")
|
||||||
|
public DefaultKaptcha getKaptchaBeanNumber() {
|
||||||
|
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
|
||||||
|
Properties properties = new Properties();
|
||||||
|
// 是否有边框 默认为true 我们可以自己设置yes,no
|
||||||
|
properties.setProperty(KAPTCHA_BORDER, "yes");
|
||||||
|
// 验证码文本字符颜色 默认为Color.BLACK
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
|
||||||
|
// 验证码图片宽度 默认为200
|
||||||
|
properties.setProperty(KAPTCHA_IMAGE_WIDTH, "160");
|
||||||
|
// 验证码图片高度 默认为50
|
||||||
|
properties.setProperty(KAPTCHA_IMAGE_HEIGHT, "60");
|
||||||
|
// 验证码文本字符大小 默认为40
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38");
|
||||||
|
// KAPTCHA_SESSION_KEY
|
||||||
|
properties.setProperty(KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode");
|
||||||
|
// 验证码文本生成器
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_IMPL, com.ruoyi.gateway.config.KaptchaNumberCreator.class.getName());
|
||||||
|
// 验证码文本字符长度 默认为5
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
|
||||||
|
// 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)
|
||||||
|
properties.setProperty(KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier");
|
||||||
|
// 图片样式 水纹com.google.code.kaptcha.impl.WaterRipple 鱼眼com.google.code.kaptcha.impl.FishEyeGimpy 阴影com.google.code.kaptcha.impl.ShadowGimpy
|
||||||
|
properties.setProperty(KAPTCHA_OBSCURIFICATOR_IMPL, com.google.code.kaptcha.impl.ShadowGimpy.class.getName());
|
||||||
Config config = new Config(properties);
|
Config config = new Config(properties);
|
||||||
defaultKaptcha.setConfig(config);
|
defaultKaptcha.setConfig(config);
|
||||||
return defaultKaptcha;
|
return defaultKaptcha;
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.ruoyi.gateway.config;
|
||||||
|
|
||||||
|
import com.google.code.kaptcha.text.impl.DefaultTextCreator;
|
||||||
|
import com.ujcms.commons.security.Secures;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 验证码数字生成器
|
||||||
|
*
|
||||||
|
* @author hsdllcw
|
||||||
|
*/
|
||||||
|
public class KaptchaNumberCreator extends DefaultTextCreator {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getText() {
|
||||||
|
return Secures.randomNumeric(getConfig().getTextProducerCharLength());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user