-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
441 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.ms.reggie.config; | ||
|
||
import com.ms.reggie.enums.CodeTypeEnum; | ||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* @ClassName EasyCaptchaConfig | ||
* @Author ChangLu | ||
* @Date 4/12/2022 5:28 PM | ||
* @Description 验证码控制器 | ||
*/ | ||
@Data | ||
@ConfigurationProperties(prefix = "easycaptcha") | ||
@Configuration | ||
public class EasyCaptchaConfig { | ||
|
||
/** | ||
* 验证码配置 | ||
*/ | ||
private CodeTypeEnum codeType; | ||
/** | ||
* 验证码内容长度 | ||
*/ | ||
private int length = 4; | ||
/** | ||
* 验证码宽度 | ||
*/ | ||
private int width = 111; | ||
/** | ||
* 验证码高度 | ||
*/ | ||
private int height = 36; | ||
/** | ||
* 验证码字体 | ||
*/ | ||
private String fontName; | ||
/** | ||
* 字体大小 | ||
*/ | ||
private int fontSize = 25; | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/com/ms/reggie/config/SpringConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.ms.reggie.config; | ||
|
||
import com.baomidou.kisso.captcha.ImageCaptcha; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
/** | ||
* <p> | ||
* </p> | ||
* | ||
* @author SerMs | ||
* @date 2022/6/21 23:38 | ||
*/ | ||
@Configuration | ||
public class SpringConfiguration { | ||
@Bean | ||
public ImageCaptcha imageCaptcha() { | ||
ImageCaptcha imageCaptcha = ImageCaptcha.getInstance(); | ||
// 干扰量 1 | ||
imageCaptcha.setInterfere(1); | ||
// 验证码内容长度 4 位 | ||
imageCaptcha.setLength(4); | ||
// Gif 验证码 | ||
// imageCaptcha.setGif(true); | ||
// 验证码存储处理类,默认存在在 session 实现类 CaptchaStoreSession 仅适用单机 | ||
// 分布式可以采用 Redis 处理,例如 RedisCaptchaStore 实现 ICaptchaStore 接口 | ||
// imageCaptcha.setCaptchaStore(new CaptchaStoreRedis()); | ||
return imageCaptcha; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/ms/reggie/controller/CaptchaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.ms.reggie.controller; | ||
|
||
import com.baomidou.kisso.captcha.ICaptcha; | ||
import com.wf.captcha.SpecCaptcha; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import javax.annotation.Resource; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
import java.io.IOException; | ||
|
||
/** | ||
* <p> | ||
* </p> | ||
* | ||
* @author SerMs | ||
* @date 2022/6/21 23:29 | ||
*/ | ||
@RestController | ||
@RequestMapping("/captcha") | ||
public class CaptchaController { | ||
@Resource | ||
protected HttpServletRequest request; | ||
@Resource | ||
protected HttpServletResponse response; | ||
@Resource | ||
private ICaptcha captcha; | ||
|
||
@RequestMapping("/hello") | ||
public void hello(HttpServletResponse response) throws IOException { | ||
// png类型 | ||
SpecCaptcha captcha = new SpecCaptcha(130, 48); | ||
String text = captcha.text();// 获取验证码的字符 | ||
char[] chars = captcha.textChar();// 获取验证码的字符数组 | ||
|
||
System.out.println("验证码:" + text); | ||
System.out.println(chars); | ||
// 输出验证码 | ||
captcha.out(response.getOutputStream()); | ||
} | ||
|
||
|
||
// 生成验证,例如:http://localhost:8088/v1/captcha/image?ticket=123456 | ||
@GetMapping("image") | ||
public void image(String ticket) { | ||
try { | ||
// 验证码信息存放在缓存中,key = ticket 、 value = 验证码文本内容 | ||
captcha.generate(request, response.getOutputStream(), ticket); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
// 校验图片验证码 123321 | ||
@PostMapping("verification") | ||
public boolean verification(String ticket, String code) { | ||
// ticket 为生成验证码的票据, code 为图片验证码文本内容 | ||
return captcha.verification(request, ticket, code); | ||
} | ||
} | ||
|
54 changes: 54 additions & 0 deletions
54
src/main/java/com/ms/reggie/controller/EasyCaptchaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.ms.reggie.controller; | ||
|
||
import com.ms.reggie.enums.CodeTypeEnum; | ||
import com.ms.reggie.service.EasyCaptchaService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @ClassName CaptchaController | ||
* @Author ChangLu | ||
* @Date 4/12/2022 5:44 PM | ||
* @Description Easy-captcha控制器 | ||
*/ | ||
@Controller | ||
@RestController | ||
public class EasyCaptchaController { | ||
|
||
@Autowired | ||
private EasyCaptchaService easyCaptchaService; | ||
|
||
//1、算术类型 | ||
@GetMapping("/captcha1") | ||
public Map getGifcCaptcha1() { | ||
return easyCaptchaService.getCaptchaValueAndBase64(null); | ||
} | ||
|
||
//2、Gif | ||
@GetMapping("/captcha2") | ||
public Map getGifcCaptcha2() { | ||
return easyCaptchaService.getCaptchaValueAndBase64(CodeTypeEnum.GIF); | ||
} | ||
|
||
//3、png类型 | ||
@GetMapping("/captcha3") | ||
public Map getGifcCaptcha3() { | ||
return easyCaptchaService.getCaptchaValueAndBase64(CodeTypeEnum.SPEC); | ||
} | ||
|
||
//4、chinese文字类型 | ||
@GetMapping("/captcha4") | ||
public Map getGifcCaptcha4() { | ||
return easyCaptchaService.getCaptchaValueAndBase64(CodeTypeEnum.CHINESE); | ||
} | ||
|
||
//4、chinese Gif类型 | ||
@GetMapping("/captcha5") | ||
public Map getGifcCaptcha5() { | ||
return easyCaptchaService.getCaptchaValueAndBase64(CodeTypeEnum.CHINESE_GIF); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.ms.reggie.enums; | ||
|
||
/** | ||
* @ClassName CodeTypeEnum | ||
* @Author ChangLu | ||
* @Date 4/12/2022 5:29 PM | ||
* @Description 验证码类型枚举类 | ||
*/ | ||
public enum CodeTypeEnum { | ||
|
||
/** | ||
* 算数 | ||
*/ | ||
ARITHMETIC, | ||
/** | ||
* 中文 | ||
*/ | ||
CHINESE, | ||
/** | ||
* 中文闪图 | ||
*/ | ||
CHINESE_GIF, | ||
/** | ||
* 闪图 | ||
*/ | ||
GIF, | ||
SPEC | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
src/main/java/com/ms/reggie/service/EasyCaptchaService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ms.reggie.service; | ||
|
||
import com.ms.reggie.enums.CodeTypeEnum; | ||
import com.ms.reggie.util.EasyCaptchaProducer; | ||
import com.wf.captcha.base.Captcha; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.redis.core.RedisTemplate; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.annotation.Resource; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
/** | ||
* @ClassName EasyCaptchaService | ||
* @Author ChangLu | ||
* @Date 4/12/2022 6:20 PM | ||
* @Description easy-captcha业务工具类 | ||
*/ | ||
@Service | ||
@Slf4j | ||
public class EasyCaptchaService { | ||
|
||
@Resource | ||
private EasyCaptchaProducer easyCaptchaProducer; | ||
|
||
@Resource | ||
private RedisTemplate redisTemplate; | ||
|
||
/** | ||
* 获取指定类型的验证码结果以及Base64编码值 | ||
* | ||
* @param codeType 验证码类型 | ||
* @return | ||
*/ | ||
public Map<String, String> getCaptchaValueAndBase64(CodeTypeEnum codeType) { | ||
Captcha captcha = null; | ||
if (codeType == null) { | ||
captcha = easyCaptchaProducer.getCaptcha(); | ||
} else { | ||
captcha = easyCaptchaProducer.getCaptcha(codeType); | ||
} | ||
//1、获取到结果值 | ||
String captchaValue = captcha.text().toLowerCase(); | ||
log.info("===结果值:{}", captchaValue); | ||
//获取key | ||
String key = UUID.randomUUID().toString(); | ||
//存入Redis | ||
String verifyKey = "captcha_codes" + key; | ||
log.info("====={}===={}", verifyKey, captchaValue); | ||
redisTemplate.opsForValue().set(verifyKey, captchaValue, 4, TimeUnit.MINUTES); | ||
//对于数学类型的需要进行处理 | ||
if (codeType == null || codeType == CodeTypeEnum.ARITHMETIC) { | ||
if (captcha.getCharType() - 1 == CodeTypeEnum.ARITHMETIC.ordinal() && captchaValue.contains(".")) { | ||
captchaValue = captchaValue.split("\\.")[0]; | ||
} | ||
} | ||
//2、获取到Base64编码 | ||
String captchaBase64 = captcha.toBase64(); | ||
Map<String, String> result = new HashMap<>(2); | ||
result.put("key", verifyKey); //生成的uuid | ||
result.put("base64", captchaBase64); //生成的64格式验证码 | ||
return result; | ||
|
||
/* | ||
* | ||
* //从Redis获取缓存的验证码 | ||
Object codeInSession = redisTemplate.opsForValue().get(phone); | ||
//如果登录成功,就删除缓存的验证码 | ||
redisTemplate.delete(phone); | ||
* */ | ||
} | ||
|
||
} |
Oops, something went wrong.