Skip to content

Commit 0e2b7a9

Browse files
committed
fix(captcha): change encoding format to PNG to fix empty image issue
1 parent 79bb48d commit 0e2b7a9

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

src/main/java/com/example/spring_boot_project/Security/CaptchaUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static Captcha createCaptcha(int width, int height){
1818

1919
public static String encodeCaptcha(Captcha c){
2020
try(ByteArrayOutputStream o = new ByteArrayOutputStream()){
21-
ImageIO.write(c.getImage(), "jpg", o);
21+
ImageIO.write(c.getImage(), "png", o);
2222
return Base64.getEncoder().encodeToString(o.toByteArray());
2323
} catch (IOException e){
2424
throw new RuntimeException("error encoding Captcha", e);

src/main/java/com/example/spring_boot_project/config/SecurityConfig.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
3838
.cors(cors -> cors.configurationSource(corsConfigurationSource()))
3939
.csrf(AbstractHttpConfigurer::disable)
4040
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
41-
.authorizeHttpRequests(auth -> auth
42-
.requestMatchers("/api/auth/**").permitAll()
43-
.requestMatchers("/error").permitAll()
44-
.anyRequest().authenticated()
45-
)
41+
.authorizeHttpRequests(auth -> auth
42+
.requestMatchers("/api/auth/**").permitAll()
43+
.requestMatchers("/test-captcha/**").permitAll() // <-- libera o captcha
44+
.requestMatchers("/error").permitAll()
45+
.anyRequest().authenticated()
46+
)
4647
.addFilterBefore(rateLimitingFilter, UsernamePasswordAuthenticationFilter.class)
4748
.addFilterBefore(jwtAuthFilter, UsernamePasswordAuthenticationFilter.class);
4849

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.example.spring_boot_project.controller;
2+
3+
import cn.apiclub.captcha.Captcha;
4+
import com.example.spring_boot_project.Security.CaptchaUtil;
5+
import jakarta.servlet.http.HttpSession;
6+
import org.springframework.http.ResponseEntity;
7+
import org.springframework.web.bind.annotation.*;
8+
9+
import java.util.Map;
10+
11+
@RestController
12+
@RequestMapping("/test-captcha")
13+
public class CaptchaTestController {
14+
15+
@GetMapping
16+
public Map<String, String> getCaptcha(HttpSession session) {
17+
Captcha captcha = CaptchaUtil.createCaptcha(200, 50);
18+
//System.out.println("--------------" + captcha.getImage());
19+
String encoded = CaptchaUtil.encodeCaptcha(captcha);
20+
21+
session.setAttribute("captcha", captcha.getAnswer());
22+
23+
return Map.of("image", "data:image/png;base64," + encoded);
24+
}
25+
26+
@PostMapping
27+
public ResponseEntity<String> validateCaptcha(@RequestBody Map<String, String> request, HttpSession session) {
28+
String userInput = request.get("captcha");
29+
String captchaAnswer = (String) session.getAttribute("captcha");
30+
31+
if (captchaAnswer == null) {
32+
return ResponseEntity.badRequest().body("Captcha not found or expired.");
33+
}
34+
35+
session.removeAttribute("captcha");
36+
37+
if (captchaAnswer.equalsIgnoreCase(userInput)) {
38+
return ResponseEntity.ok("Captcha is correct!");
39+
} else {
40+
return ResponseEntity.status(400).body("Captcha is incorrect!");
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)