ruoyi-modules
下新建 ruoyi-captcha
模块并在 ruoyi-captcha
的 pom.xml
文件中添加以下依赖
<!-- 滑块验证码 -->
<dependency>
<groupId>com.github.anji-plus</groupId>
<artifactId>captcha-spring-boot-starter</artifactId>
<version>1.2.7</version>
</dependency>
nacos
1、在gateway
的配置文件中增加滑动验证码
- id: ruoyi-captcha
uri: lb://ruoyi-captcha
predicates:
- Path=/captcha/**
filters:
- StripPrefix=1
2、并且将
- /captcha/get
- /captcha/check
- /captcha/verify
增加到白名单
3、关闭原来的验证码校验 设置为false
下载链接: https://pan.baidu.com/s/1b4yMF9UAqXxf5uLW9h-_9Q?pwd=meow
提取码: meow
package com.ruoyi.captcha.controller;
import com.anji.captcha.model.common.ResponseModel;
import com.anji.captcha.model.vo.CaptchaVO;
import com.anji.captcha.service.CaptchaService;
import com.ruoyi.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 滑动验证码
*/
@RestController
public class CaptchaController {
@Autowired
private CaptchaService captchaService;
@PostMapping("/get")
public ResponseModel get(@RequestBody CaptchaVO captchaVO) {
return captchaService.get(captchaVO);
}
@PostMapping("/check")
public ResponseModel check(@RequestBody CaptchaVO captchaVO) {
return captchaService.check(captchaVO);
}
@PostMapping("/verify")
public ResponseModel verify(@RequestBody CaptchaVO captchaVO) {
return captchaService.verification(captchaVO);
}
}
package com.ruoyi.captcha.config;
import com.anji.captcha.model.common.Const;
import com.anji.captcha.service.CaptchaCacheService;
import com.anji.captcha.service.CaptchaService;
import com.anji.captcha.service.impl.CaptchaServiceFactory;
import com.anji.captcha.util.ImageUtils;
import com.anji.captcha.util.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.util.Base64Utils;
import org.springframework.util.FileCopyUtils;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration
public class CaptchaConfig {
@Bean(name = "AjCaptchaCacheService")
public CaptchaCacheService captchaCacheService() {
//缓存类型redis/local/....
return CaptchaServiceFactory.getCache("local");
}
@Bean
@DependsOn("AjCaptchaCacheService")
public CaptchaService captchaService() {
Properties config = new Properties();
// try {
// try (InputStream input = CaptchaConfig.class.getClassLoader()
// .getResourceAsStream("application.properties")) {
// config.load(input);
// }
// }catch (Exception ex){
// ex.printStackTrace();
// }
//各种参数设置....
//缓存类型redis/local/....
config.put(Const.CAPTCHA_CACHETYPE, "local");
config.put(Const.CAPTCHA_WATER_MARK, "我的水印");
config.put(Const.CAPTCHA_FONT_TYPE, "宋体");
config.put(Const.CAPTCHA_TYPE, "default");
config.put(Const.CAPTCHA_INTERFERENCE_OPTIONS, "0");
config.put(Const.ORIGINAL_PATH_JIGSAW, "");
config.put(Const.ORIGINAL_PATH_PIC_CLICK, "");
config.put(Const.CAPTCHA_SLIP_OFFSET, "5");
config.put(Const.CAPTCHA_AES_STATUS, "true");
config.put(Const.CAPTCHA_WATER_FONT, "宋体");
config.put(Const.CAPTCHA_CACAHE_MAX_NUMBER, "1000");
config.put(Const.CAPTCHA_TIMING_CLEAR_SECOND, "180");
//更多自定义参数,请参考service/springboot/../resources/application.properties
if ((StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_JIGSAW))
&& config.getProperty(Const.ORIGINAL_PATH_JIGSAW).startsWith("classpath:"))
|| (StringUtils.isNotBlank(config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK))
&& config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK).startsWith("classpath:"))) {
//自定义resources目录下初始化底图
config.put(Const.CAPTCHA_INIT_ORIGINAL, "true");
initializeBaseMap(config.getProperty(Const.ORIGINAL_PATH_JIGSAW),
config.getProperty(Const.ORIGINAL_PATH_PIC_CLICK));
}
CaptchaService s = CaptchaServiceFactory.getInstance(config);
return s;
}
private static void initializeBaseMap(String jigsaw, String picClick) {
ImageUtils.cacheBootImage(getResourcesImagesFile(jigsaw + "/original/*.png"),
getResourcesImagesFile(jigsaw + "/slidingBlock/*.png"),
getResourcesImagesFile(picClick + "/*.png"));
}
public static Map<String, String> getResourcesImagesFile(String path) {
Map<String, String> imgMap = new HashMap<>();
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = resolver.getResources(path);
for (Resource resource : resources) {
byte[] bytes = FileCopyUtils.copyToByteArray(resource.getInputStream());
String string = Base64Utils.encodeToString(bytes);
String filename = resource.getFilename();
imgMap.put(filename, string);
}
} catch (Exception e) {
e.printStackTrace();
}
return imgMap;
}
}
package com.ruoyi.captcha.service;
import com.anji.captcha.service.CaptchaCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import java.util.concurrent.TimeUnit;
public class CaptchaCacheServiceRedisImpl implements CaptchaCacheService {
@Override
public String type() {
return "redis";
}
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Override
public void set(String key, String value, long expiresInSeconds) {
stringRedisTemplate.opsForValue().set(key, value, expiresInSeconds, TimeUnit.SECONDS);
}
@Override
public boolean exists(String key) {
return stringRedisTemplate.hasKey(key);
}
@Override
public void delete(String key) {
stringRedisTemplate.delete(key);
}
@Override
public String get(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
}
package com.ruoyi.captcha;
import com.ruoyi.common.security.annotation.EnableCustomConfig;
import com.ruoyi.common.security.annotation.EnableRyFeignClients;
import com.ruoyi.common.swagger.annotation.EnableCustomSwagger2;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 验证码模块
*
* @author ruoyi
*/
@EnableCustomConfig
@EnableCustomSwagger2
@EnableRyFeignClients
@SpringBootApplication
public class RuoyiCaptchaApplication
{
public static void main(String[] args)
{
SpringApplication.run(RuoyiCaptchaApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 验证码模块启动成功 ლ(´ڡ`ლ)゙ \n" +
" .-------. ____ __ \n" +
" | _ _ \\ \\ \\ / / \n" +
" | ( ' ) | \\ _. / ' \n" +
" |(_ o _) / _( )_ .' \n" +
" | (_,_).' __ ___(_ o _)' \n" +
" | |\\ \\ | || |(_,_)' \n" +
" | | \\ `' /| `-' / \n" +
" | | \\ / \\ / \n" +
" ''-' `'-' `-..-' ");
}
}
打开ruoyi-cloud \集成aj-captcha实现滑块验证码 \ ruoyi-ui
在ruoyi-ui终端安装
npm install crypto-js --save-dev
再按照目录结构,把 api\login.js
替换,views/login.vue
替换,静态资源和组件复制进去就ok了
注意:测试中发现滑动验证码如果使用
redis
存储会出现
CaptchaCacheServiceRedisImpl
内的StringRedisTemplate
无法注入
需要将AJ-Captcha
的依赖版本1.2.7
更改为1.3.0
并且将redis
的引入更改为private static final StringRedisTemplate stringRedisTemplate = SpringUtils.getBean(“stringRedisTemplate”);
powered by kaifamiao