新增:新增获取图片验证码 API(引入 Redisson、Hutool、Easy Captcha 依赖,详情可见 README 介绍)

This commit is contained in:
2022-12-11 15:06:21 +08:00
parent 8967542a10
commit 1e5eaab9d3
15 changed files with 875 additions and 17 deletions

View File

@@ -14,20 +14,22 @@
* limitations under the License.
*/
package top.charles7c;
package top.charles7c.cnadmin;
import java.net.InetAddress;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
/**
* 启动程序
*
@@ -37,6 +39,8 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
@RestController
@SpringBootApplication
@Import(cn.hutool.extra.spring.SpringUtil.class)
@ComponentScan(basePackages = {"top.charles7c.cnadmin", "cn.hutool.extra.spring"})
public class ContinewAdminApplication {
private static Environment env;

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package top.charles7c.cnadmin.webapi.controller.auth;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wf.captcha.base.Captcha;
import cn.hutool.core.util.IdUtil;
import top.charles7c.cnadmin.auth.config.properties.CaptchaProperties;
import top.charles7c.cnadmin.auth.model.vo.CaptchaVO;
import top.charles7c.cnadmin.common.model.vo.R;
import top.charles7c.cnadmin.common.util.RedisUtils;
/**
* 验证码 API
*
* @author Charles7c
* @since 2022/12/11 14:00
*/
@RestController
@RequiredArgsConstructor
@RequestMapping("/captcha")
public class CaptchaController {
private final CaptchaProperties captchaProperties;
/**
* 获取图片验证码
*
* @return 验证码信息
*/
@GetMapping("/img")
public R<CaptchaVO> getImageCaptcha() {
// 生成验证码
Captcha captcha = captchaProperties.getCaptcha();
// 保存验证码
String uuid = IdUtil.simpleUUID();
String captchaKey = RedisUtils.formatKey(captchaProperties.getKeyPrefix(), uuid);
RedisUtils.setCacheObject(captchaKey, captcha.text(),
Duration.ofMinutes(captchaProperties.getExpirationInMinutes()));
// 返回验证码
CaptchaVO captchaVo = new CaptchaVO().setUuid(uuid).setImg(captcha.toBase64());
return R.ok(captchaVo);
}
}

View File

@@ -3,3 +3,33 @@ server:
# HTTP 端口(默认 8080
port: 8000
--- ### Redis 单机配置
spring:
redis:
# 地址
host: ${REDIS_HOST:127.0.0.1}
# 端口(默认 6379
port: ${REDIS_PORT:6379}
# 密码(未设置密码时可为空或注释掉)
password: ${REDIS_PWD:123456}
# 数据库索引
database: ${REDIS_DB:0}
# 连接超时时间
timeout: 10s
# 是否开启 SSL
ssl: false
--- ### 验证码配置
captcha:
# 类型
type: SPEC
# 缓存键的前缀
keyPrefix: CAPTCHA
# 过期时间
expirationInMinutes: 2
# 内容长度
length: 4
# 宽度
width: 111
# 高度
height: 36

View File

@@ -3,3 +3,33 @@ server:
# HTTP 端口(默认 8080
port: 18000
--- ### Redis 单机配置
spring:
redis:
# 地址
host: ${REDIS_HOST:127.0.0.1}
# 端口(默认 6379
port: ${REDIS_PORT:6379}
# 密码(未设置密码时可为空或注释掉)
password: ${REDIS_PWD:123456}
# 数据库索引
database: ${REDIS_DB:0}
# 连接超时时间
timeout: 10s
# 是否开启 SSL
ssl: false
--- ### 验证码配置
captcha:
# 类型
type: SPEC
# 缓存键的前缀
keyPrefix: CAPTCHA
# 过期时间
expirationInMinutes: 2
# 内容长度
length: 4
# 宽度
width: 111
# 高度
height: 36

View File

@@ -52,11 +52,9 @@ spring:
date-format: yyyy-MM-dd HH:mm:ss
# 序列化配置Bean -> JSON
serialization:
# 是否格式化输出
indent_output: false
# 忽略无法转换的对象
# 允许序列化无属性的 Bean
fail_on_empty_beans: false
# 反序列化配置JSON -> Bean
deserialization:
# 忽略 JSON 中不存在的属性
# 允许反序列化不存在的属性
fail_on_unknown_properties: false