1
resposta

Implementei um exemplo JSF com reCAPTCHA v2 e funcionou corretamente porem com o reCAPTCHA v3 nao funcionou

Implementei um exemplo JSF com reCAPTCHA v2 e funcionou corretamente

Tentei implementar com o reCAPTCHA v3 usando o modelo indicado na documentação do site do Google Recaptcha (https://developers.google.com/recaptcha/docs/v3) e não funcionou, alguem conseguiu fazer o V3 funcionar?

DESSE JEITO NAO FUNCIONOU COM O V3 UTILIZANDO UM BUTTON:

 <script src="https://www.google.com/recaptcha/api.js"></script>
 <script>
   function onSubmit(token) {
     document.getElementById("demo-form").submit();
   }
 </script>
...
<button class="g-recaptcha" 
        data-sitekey="reCAPTCHA_site_key" 
        data-callback='onSubmit' 
        data-action='submit'>Submit</button>
...

O QUE FUNCIONOU FOI FAZENDO COM O V2 UTILIZANDO UMA DIV:

[index.xhtml]
/** @author André Hiroshi Tanaka  */
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui">
    <h:head>
        <title>GoogleRecaptchaTeste_V2_XHTML_JSF Login Page</title>
        <script src="https://www.google.com/recaptcha/api.js"></script>
    </h:head>
    <h:body>

            <h:form  >
                    <h1>GoogleRecaptchaTeste_V2_XHTML_JSF</h1>
                    <br/> 

                    <h:outputLabel value="Username: " /> 
                    <p:inputText value="#{managedBeanLogin.user}" /> <br/>
                    <br/>                    
                    <h:outputLabel value="Password: " />
                    <p:inputText value="#{managedBeanLogin.pwd}" />
                    <br/>

                    <p:messages id="pmessages"/>                    
                    <br/>                    
                    <div id="google-recaptcha" class="g-recaptcha" data-sitekey="minhaChaveSiteCadastradaNoGoogleRecaptcha"> </div>  <br/>
                    <br/> 

                    <p:commandButton value="Login" action="${loginController.login}" ajax="false"/>
                    <br/>
                    <br/>

                    <span style="font-size: smaller">(hint: root admin)</span>

                    <br/> 
                    <br/> 
                    <br/> 
                    Dica: Observe no console terminal de saida do tomcat as chamadas a API do google recaptcha
                    <br/> 
                    <br/> 
                    <br/> 
                    <h2>
                        <a href="http://www.andretanaka.com.br" target="_blank">www.andretanaka.com.br</a>
                    </h2>
                    <br/> 
                    <br/> 
            </h:form>
</h:body>
</html>
/** @author André Hiroshi Tanaka  */
@ManagedBean
public class LoginController implements Serializable{

    public String login() throws IOException{
        try {
            System.out.println("...executando metodo LoginController.login()");

            ManagedBeanLogin managedBeanLogin = (ManagedBeanLogin) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("managedBeanLogin");
            String user = managedBeanLogin==null?"":managedBeanLogin.getUser();
            String pwd = managedBeanLogin==null?"":managedBeanLogin.getPwd();

            String gRecaptchaResponse = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("g-recaptcha-response");
            System.out.println("g-recaptcha-response:");
            System.out.println(gRecaptchaResponse);

            boolean verify = VerifyRecaptcha.verify(gRecaptchaResponse);

            String userID = "root";
            String password = "admin";

            System.out.println("User=" + user + "::password=" + pwd + "::Captcha Verify"+verify);

public class VerifyRecaptcha {

    public static final String url = "https://www.google.com/recaptcha/api/siteverify";
    public static final String secret = "minhaChavePrivadaCadastradaNoGoogleRecaptc";
    private final static String USER_AGENT = "Mozilla/5.0";

    public static boolean verify(String gRecaptchaResponse) throws IOException {

        if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) {
            return false;
        }

        try {
            URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();

            // add reuqest header
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
         ...
1 resposta
O post foi fechado por inatividade. Para continuar o assunto, recomendamos criar um novo tópico. Bons Estudos!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software