By default Keycloak (up to 12) only supports ReCaptcha for Registration, not login. so we created a simple module for activating recaptcha for login
- first run
mvn clean install. it will producetarget/recaptcha-login.jar. - Then start keycloak with
docker-compose up- the
target/recaptcha-login.jaris mounted to/opt/jboss/keycloak/standalone/deployments/recaptcha-login.jar, so it will be detected by JBoss automatically as a plugin. login.ftlis the login template insidebasetheme for keycloak in/opt/jboss/keycloak/themes/base/login/login.ftl. We decided to override it to add recaptcha related items into it directly. so we have a modifiedlogin.ftlfile inside project. which is mounted into the container.- we injected realm settings in to keycloaks' container by
playground-realm.json. One of the most important realm settings isX-Frame-Optionswhich is set toALLOW-FROM https://www.google.comin the file. The other important setting isContent-Security-Policywhich is set toframe-src 'self' https://www.google.com; frame-ancestors 'self'; object-src 'none';.
- the
- Go to
localhost:8080and login withadminusername andadminpassword. - Make sure You're in
Playgroundrealm. Go toAuthenticationsection in left side menu. Go toFlowstab. - Pick
Browser With Recaptchaflow in the drop down - Under
Recaptcha Username Password Formrow clickActionsand thenConfig - Put the
sitekeyandsecretobtained fromhttps://www.google.com/recaptcha/admininto the config - Go back to
Authenticationside menu, go toBindingstab and changebrowser flowtobrowser with recaptcha - Go to
Usersside menu, clickAdd user. create a user and make it enabled, and set it's credential properly. - Go back to
Users, clickView all usersand on that specifiec user you have created in prev step, clickimpersonate. - Then
Sign Outand thenSign In. If you have done everything precisely, then you'll be able to see the recaptcha form.
Inside the login.ftl, We have just added this part inside <form></form> section.
So inside RecaptchaUsernamePasswordForm we will set recaptchaRequired to true, and this part
of login.ftl will be enabled for recaptcha based logins.
If using normal UsernamePasswordForm then this part will not be enabled in the login form.
<#if recaptchaRequired??>
<div class="form-group">
<div class="${properties.kcInputWrapperClass!}">
<div class="g-recaptcha" data-size="compact" data-sitekey="${recaptchaSiteKey}"></div>
</div>
</div>
</#if>