mirror of
https://github.com/nagisa77/OpenIsle.git
synced 2026-02-23 14:40:49 +08:00
Add configurable captcha endpoints
This commit is contained in:
14
src/main/java/com/openisle/service/CaptchaService.java
Normal file
14
src/main/java/com/openisle/service/CaptchaService.java
Normal 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);
|
||||
}
|
||||
35
src/main/java/com/openisle/service/RecaptchaService.java
Normal file
35
src/main/java/com/openisle/service/RecaptchaService.java
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user