Keycloak-自定义表单操作在流程中不可见

时间:2019-03-21 15:56:39

标签: java keycloak keycloak-services

我正在尝试为用户注册实施自定义表单操作。我在表单上添加了一些自定义字段,我希望验证这些字段。浏览了密钥斗篷文档后,我意识到我需要

  • 扩展FormAction,FormActionFactory
  • 将actionfactory包装在META-INF / services / org.keycloak.authentication.FormActionFactory
  • 在keycloak / standalone / deployments文件夹中部署JAR。

我已经完成所有步骤,并验证了提供程序已加载。这是keycloak日志文件中的日志

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

但是当我转到“身份验证执行”屏幕时,未列出提供程序。 enter image description here

这是自定义操作的代码。

/**
 * 
 */
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();
        }
    }

}

无法弄清这里出了什么问题。

1 个答案:

答案 0 :(得分:0)

道歉。我正在查看错误的“添加执行”选项。我意识到每种形式也都有相应的“动作”菜单。此菜单还具有“添加执行”选项。

enter image description here

选择该选项后,我可以看到我的“自定义表单”操作显示在受限操作列表中。

enter image description here