文章目录
-
- 项目地址
- 引入依赖
- 代码展示
- 注意细节
- 小结
引入依赖
提示:我的基于springboot项目
(1)使用maven引入坐标
代码展示
(1)我是使用的系统默认的模板所以在application.yml这么配置
(2)在配置类中添加一个照片
@Component @RequiredArgsConstructor public class CaptchaResourceConfiguration { private final ResourceStore resourceStore; @PostConstruct public void init() { // 2. 添加自定义背景图片, resource 的参数1为资源类型(默认支持 classpath/file/url ), resource 的参数2为资源路径, resource 的参数3为标签 resourceStore.addResource(CaptchaTypeConstant.SLIDER, new Resource("classpath", "bgimages/a.jpg", "default")); } }
(3)现在可以在控制器中给前端响应了
@GetMapping public ResponseEntity test(HttpServletResponse response) throws IOException { // 生成验证码 CaptchaResponse<ImageCaptchaVO> imageCaptchaVOCaptchaResponse = captchaApplication.generateCaptcha(CaptchaTypeConstant.SLIDER); log.info("imageCaptchaVOCaptchaResponse:{}", imageCaptchaVOCaptchaResponse); return ResponseEntity.ok(imageCaptchaVOCaptchaResponse); }
(4)现在我们去127.0.0.1:8080/或者http://localhost:8080/看效果
注意细节
(1)一定要添加照片要不然会出现
小结
这个开源项目可以让我们的验证码更加炫酷
加油兄弟们,快快尝试起来!!!