Created
July 20, 2015 14:49
-
-
Save eallais/57d1e738705bcad32629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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