ruoyi-cloud集成行为验证码
时间: 2023-07-18 15:47:43 浏览: 153
Ruoyi-Cloud集成行为验证码可以通过引入第三方库Kaptcha来实现。下面是实现步骤:
1. 在pom.xml文件中添加依赖:
```xml
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
```
2. 创建一个验证码生成器类,实现行为验证码的生成逻辑,代码如下:
```java
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
@Component
public class CaptchaGenerator {
@Autowired
private DefaultKaptcha defaultKaptcha;
/**
* 生成行为验证码图片
* @param request
* @param response
* @throws IOException
*/
public void generate(HttpServletRequest request, HttpServletResponse response) throws IOException {
// 设置响应头信息,禁止图像缓存
response.setHeader("Cache-Control", "no-store, no-cache");
response.setContentType("image/jpeg");
// 生成验证码文本
String text = defaultKaptcha.createText();
// 将验证码文本保存到session中
request.getSession().setAttribute("captcha", text);
// 创建验证码图片
BufferedImage image = defaultKaptcha.createImage(text);
// 将验证码图片输出到客户端
try (OutputStream out = response.getOutputStream()) {
ImageIO.write(image, "jpg", out);
out.flush();
}
}
/**
* 验证行为验证码
* @param request
* @param inputText
* @return
*/
public boolean validate(HttpServletRequest request, String inputText) {
String captcha = (String) request.getSession().getAttribute("captcha");
if (captcha == null) {
return false;
}
if (!captcha.equals(inputText)) {
return false;
}
return true;
}
/**
* 配置Kaptcha
* @return
*/
public DefaultKaptcha getDefaultKaptcha() {
Properties properties = new Properties();
// 图片宽度
properties.setProperty("kaptcha.image.width", "150");
// 图片高度
properties.setProperty("kaptcha.image.height", "50");
// 字符集
properties.setProperty("kaptcha.textproducer.char.string", "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");
// 字符长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字符间距
properties.setProperty("kaptcha.textproducer.char.space", "5");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "30");
// 干扰线颜色
properties.setProperty("kaptcha.noise.color", "black");
// 干扰线数量
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.NoNoise");
Config config = new Config(properties);
DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
```
3. 在Spring Boot配置类中注入验证码生成器类,并将Kaptcha配置为Bean:
```java
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CaptchaConfig {
/**
* 注入验证码生成器
* @return
*/
@Bean
public CaptchaGenerator captchaGenerator() {
CaptchaGenerator captchaGenerator = new CaptchaGenerator();
captchaGenerator.setDefaultKaptcha(defaultKaptcha());
return captchaGenerator;
}
/**
* 配置Kaptcha
* @return
*/
@Bean
public DefaultKaptcha defaultKaptcha() {
return captchaGenerator().getDefaultKaptcha();
}
}
```
4. 在需要使用验证码的Controller中调用验证码生成器的generate方法生成验证码图片,代码如下:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
public class CaptchaController {
@Autowired
private CaptchaGenerator captchaGenerator;
@GetMapping("/captcha")
public void generate(HttpServletRequest request, HttpServletResponse response) throws IOException {
captchaGenerator.generate(request, response);
}
}
```
5. 在登录接口中调用验证码生成器的validate方法验证用户输入的验证码是否正确,代码如下:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
@Controller
public class LoginController {
@Autowired
private CaptchaGenerator captchaGenerator;
@PostMapping("/login")
public String login(HttpServletRequest request, @RequestParam String captcha) {
boolean valid = captchaGenerator.validate(request, captcha);
if (!valid) {
// 验证码错误
return "redirect:/login?error";
}
// 验证码正确,进行登录操作
// ...
return "redirect:/index";
}
}
```
以上就是在Ruoyi-Cloud中集成行为验证码的步骤。
阅读全文