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

@@ -0,0 +1,14 @@
package com.openisle.service;
/**
* Abstract service for verifying CAPTCHA tokens.
*/
public abstract class CaptchaService {
/**
* Verify the CAPTCHA token sent from client.
*
* @param token CAPTCHA token
* @return true if token is valid
*/
public abstract boolean verify(String token);
}

View File

@@ -0,0 +1,35 @@
package com.openisle.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
/**
* CaptchaService implementation using Google reCAPTCHA.
*/
@Service
public class RecaptchaService extends CaptchaService {
@Value("${recaptcha.secret-key:}")
private String secretKey;
private final RestTemplate restTemplate = new RestTemplate();
@Override
public boolean verify(String token) {
if (token == null || token.isEmpty()) {
return false;
}
String url = "https://www.google.com/recaptcha/api/siteverify?secret={secret}&response={response}";
try {
ResponseEntity<Map> resp = restTemplate.postForEntity(url, null, Map.class, secretKey, token);
Map body = resp.getBody();
return body != null && Boolean.TRUE.equals(body.get("success"));
} catch (Exception e) {
return false;
}
}
}