Add configurable captcha endpoints

This commit is contained in:
Tim
2025-07-01 14:36:29 +08:00
parent 27d498cb06
commit 10fa61d3ed
8 changed files with 160 additions and 0 deletions

View File

@@ -6,11 +6,13 @@ import com.openisle.model.Reaction;
import com.openisle.service.CommentService;
import com.openisle.service.PostService;
import com.openisle.service.ReactionService;
import com.openisle.service.CaptchaService;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import java.time.LocalDateTime;
import java.util.List;
@@ -23,9 +25,19 @@ public class PostController {
private final PostService postService;
private final CommentService commentService;
private final ReactionService reactionService;
private final CaptchaService captchaService;
@Value("${app.captcha.enabled:false}")
private boolean captchaEnabled;
@Value("${app.captcha.post-enabled:false}")
private boolean postCaptchaEnabled;
@PostMapping
public ResponseEntity<PostDto> createPost(@RequestBody PostRequest req, Authentication auth) {
if (captchaEnabled && postCaptchaEnabled && !captchaService.verify(req.getCaptcha())) {
return ResponseEntity.badRequest().build();
}
Post post = postService.createPost(auth.getName(), req.getCategoryId(), req.getTitle(), req.getContent());
return ResponseEntity.ok(toDto(post));
}
@@ -110,6 +122,7 @@ public class PostController {
private Long categoryId;
private String title;
private String content;
private String captcha;
}
@Data