Skip to content

Instantly share code, notes, and snippets.

@eallais
Created July 20, 2015 14:49
Show Gist options
  • Select an option

  • Save eallais/57d1e738705bcad32629 to your computer and use it in GitHub Desktop.

Select an option

Save eallais/57d1e738705bcad32629 to your computer and use it in GitHub Desktop.
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class ReCaptchaService {
private static final String URL = "https://www.google.com/recaptcha/api/siteverify?secret={secret}&response={response}&remoteip={remoteip}";
private RestTemplate restTemplate = new RestTemplate();
@Value("${reCaptcha.secret}")
private String secret;
public boolean verify(HttpServletRequest request) {
String gRecaptchaResponse = request.getParameter("g-recaptcha-response");
String remoteAddress = request.getRemoteAddr();
Map<String, String> uriVariable = new HashMap<>();
uriVariable.put("secret", secret);
uriVariable.put("response", gRecaptchaResponse);
uriVariable.put("remoteip", remoteAddress);
ReCaptchaResponse response = restTemplate.postForObject(URL, null, ReCaptchaResponse.class, uriVariable);
return response.isSuccess();
}
static class ReCaptchaResponse {
public boolean success;
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment