问题
I am trying to implement custom form action for user registration. I have added few custom fields on the form and I wish to validate those fields. After going through the keycloak documentation, I realised that I need to
- Extend FormAction, FormActionFactory
- Package the actionfactory in META-INF/services/org.keycloak.authentication.FormActionFactory
- Deploy the JAR in keycloak/standalone/deployments folder.
I have done all the steps and verified that the provider is getting loaded. Here is the log from keycloak log file
15:35:29,962 WARN [org.keycloak.services] (ServerService Thread Pool -- 46) KC-SERVICES0047: organization-field-validation-action (com.phoenix.keycloak.forms.action.OrganizationFormAction) is implementing the internal SPI form-action. This SPI is internal and may change without notice
But when I go to Authentication execution screen, the provider is not listed.
Here is the code for custom action.
/**
*
*/
package com.phoenix.keycloak.forms.action;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.MultivaluedMap;
import org.keycloak.Config.Scope;
import org.keycloak.authentication.FormAction;
import org.keycloak.authentication.FormActionFactory;
import org.keycloak.authentication.FormContext;
import org.keycloak.authentication.ValidationContext;
import org.keycloak.events.Details;
import org.keycloak.events.Errors;
import org.keycloak.forms.login.LoginFormsProvider;
import org.keycloak.models.AuthenticationExecutionModel.Requirement;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.utils.FormMessage;
import org.keycloak.provider.ProviderConfigProperty;
import org.keycloak.services.validation.Validation;
/**
* @author Yogesh Jadhav
*
*/
public class OrganizationFormAction implements FormAction, FormActionFactory {
private static final String PROVIDER_ID = "organization-field-validation-action";
private static Requirement[] REQUIREMENT_CHOICES = { Requirement.REQUIRED, Requirement.DISABLED };
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.Provider#close()
*/
@Override
public void close() {
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ProviderFactory#create(org.keycloak.models.
* KeycloakSession)
*/
@Override
public FormAction create(KeycloakSession arg0) {
return this;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ProviderFactory#getId()
*/
@Override
public String getId() {
return PROVIDER_ID;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ProviderFactory#init(org.keycloak.Config.Scope)
*/
@Override
public void init(Scope arg0) {
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ProviderFactory#postInit(org.keycloak.models.
* KeycloakSessionFactory)
*/
@Override
public void postInit(KeycloakSessionFactory arg0) {
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.ConfigurableAuthenticatorFactory#getDisplayType()
*/
@Override
public String getDisplayType() {
return "Organization Profile Validation";
}
/*
* (non-Javadoc)
*
* @see org.keycloak.authentication.ConfigurableAuthenticatorFactory#
* getReferenceCategory()
*/
@Override
public String getReferenceCategory() {
return null;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.authentication.ConfigurableAuthenticatorFactory#
* getRequirementChoices()
*/
@Override
public Requirement[] getRequirementChoices() {
return REQUIREMENT_CHOICES;
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.ConfigurableAuthenticatorFactory#isConfigurable()
*/
@Override
public boolean isConfigurable() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.authentication.ConfigurableAuthenticatorFactory#
* isUserSetupAllowed()
*/
@Override
public boolean isUserSetupAllowed() {
return true;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ConfiguredProvider#getConfigProperties()
*/
@Override
public List<ProviderConfigProperty> getConfigProperties() {
return null;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.provider.ConfiguredProvider#getHelpText()
*/
@Override
public String getHelpText() {
return "Validates organization name and mobile number field.";
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.FormAction#buildPage(org.keycloak.authentication.
* FormContext, org.keycloak.forms.login.LoginFormsProvider)
*/
@Override
public void buildPage(FormContext arg0, LoginFormsProvider arg1) {
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.FormAction#configuredFor(org.keycloak.models.
* KeycloakSession, org.keycloak.models.RealmModel,
* org.keycloak.models.UserModel)
*/
@Override
public boolean configuredFor(KeycloakSession arg0, RealmModel arg1, UserModel arg2) {
return false;
}
/*
* (non-Javadoc)
*
* @see org.keycloak.authentication.FormAction#requiresUser()
*/
@Override
public boolean requiresUser() {
return false;
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.FormAction#setRequiredActions(org.keycloak.models
* .KeycloakSession, org.keycloak.models.RealmModel,
* org.keycloak.models.UserModel)
*/
@Override
public void setRequiredActions(KeycloakSession arg0, RealmModel arg1, UserModel arg2) {
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.FormAction#success(org.keycloak.authentication.
* FormContext)
*/
@Override
public void success(FormContext arg0) {
}
/*
* (non-Javadoc)
*
* @see
* org.keycloak.authentication.FormAction#validate(org.keycloak.authentication.
* ValidationContext)
*/
@Override
public void validate(ValidationContext context) {
MultivaluedMap<String, String> formData = context.getHttpRequest().getDecodedFormParameters();
List<FormMessage> errors = new ArrayList<>();
context.getEvent().detail(Details.REGISTER_METHOD, "form");
String eventError = Errors.INVALID_REGISTRATION;
if (Validation.isBlank(formData.getFirst("user.attributes.companyName"))) {
errors.add(new FormMessage("user.attributes.companyName", "missingOrganizationNameMessage"));
}
if (Validation.isBlank(formData.getFirst("user.attributes.contactPersonMobile"))) {
errors.add(new FormMessage("user.attributes.contactPersonMobile", "missingContactPersonMobileMessage"));
}
if (errors.size() > 0) {
context.error(eventError);
context.validationError(formData, errors);
return;
} else {
context.success();
}
}
}
Not able to figure out what went wrong here.
回答1:
Apologies. I was looking at the wrong "Add Execution" option. I realized that each form also has a corresponding "Actions" menu. This menu also has "Add Execution" option.
After selecting that option, I could see my Custom Form action is displayed in the restricted list of actions.
来源:https://stackoverflow.com/questions/55284482/keycloak-custom-form-action-not-visible-in-flow