6 changed files with 126 additions and 3 deletions
-
6pom.xml
-
31src/main/java/com/example/demo/config/KaptchaConfig.java
-
36src/main/java/com/example/demo/controller/coin/AdminController.java
-
49src/main/java/com/example/demo/controller/coin/CaptchaController.java
-
6src/main/java/com/example/demo/domain/entity/Admin.java
-
1src/main/java/com/example/demo/security/SecurityConfig.java
@ -0,0 +1,31 @@ |
|||
// com.example.demo.config.KaptchaConfig.java |
|||
package com.example.demo.config; |
|||
|
|||
import com.google.code.kaptcha.impl.DefaultKaptcha; |
|||
import com.google.code.kaptcha.util.Config; |
|||
import org.springframework.context.annotation.Bean; |
|||
import org.springframework.context.annotation.Configuration; |
|||
|
|||
|
|||
import java.util.Properties; |
|||
|
|||
@Configuration |
|||
public class KaptchaConfig { |
|||
|
|||
@Bean |
|||
public DefaultKaptcha defaultKaptcha() { |
|||
DefaultKaptcha kaptcha = new DefaultKaptcha(); |
|||
Properties properties = new Properties(); |
|||
properties.setProperty("kaptcha.image.width", "130"); |
|||
properties.setProperty("kaptcha.image.height", "45"); |
|||
properties.setProperty("kaptcha.textproducer.char.length", "4"); |
|||
properties.setProperty("kaptcha.textproducer.font.size", "35"); |
|||
properties.setProperty("kaptcha.textproducer.font.color", "black"); |
|||
properties.setProperty("kaptcha.textproducer.char.space", "5"); |
|||
properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple"); |
|||
properties.setProperty("kaptcha.noise.impl", "com.google.code.kaptcha.impl.DefaultNoise"); |
|||
|
|||
kaptcha.setConfig(new Config(properties)); |
|||
return kaptcha; |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
// com.example.demo.controller.CaptchaController.java |
|||
package com.example.demo.controller.coin; |
|||
|
|||
import com.google.code.kaptcha.Producer; |
|||
import jakarta.servlet.http.HttpServletResponse; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.data.redis.core.StringRedisTemplate; |
|||
import org.springframework.web.bind.annotation.GetMapping; |
|||
import org.springframework.web.bind.annotation.RequestParam; |
|||
import org.springframework.web.bind.annotation.RestController; |
|||
|
|||
import javax.imageio.ImageIO; |
|||
import java.awt.image.BufferedImage; |
|||
import java.io.IOException; |
|||
import java.util.concurrent.TimeUnit; |
|||
|
|||
@RestController |
|||
public class CaptchaController { |
|||
|
|||
@Autowired |
|||
private Producer kaptchaProducer; |
|||
|
|||
@Autowired |
|||
private StringRedisTemplate redisTemplate; |
|||
|
|||
/** |
|||
* 获取图形验证码 |
|||
* @param uuid 前端生成的唯一标识,用于关联验证码 |
|||
*/ |
|||
@GetMapping("/captcha") |
|||
public void captcha(@RequestParam String uuid, HttpServletResponse response) throws IOException { |
|||
if (uuid == null || uuid.trim().isEmpty()) { |
|||
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "uuid is required"); |
|||
return; |
|||
} |
|||
|
|||
// 生成验证码文本和图片 |
|||
String code = kaptchaProducer.createText(); |
|||
BufferedImage image = kaptchaProducer.createImage(code); |
|||
|
|||
// 存入 Redis,5分钟过期 |
|||
redisTemplate.opsForValue().set("CAPTCHA:" + uuid, code, 5, TimeUnit.MINUTES); |
|||
|
|||
// 输出图片 |
|||
response.setHeader("Cache-Control", "no-store"); |
|||
response.setContentType("image/jpeg"); |
|||
ImageIO.write(image, "jpg", response.getOutputStream()); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue