gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:06:50 31 4
gpt4 key购买 nike

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

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

我已完成所有步骤并验证提供程序是否已加载。这是来自 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();
}
}

}

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

最佳答案

抱歉。我正在查看错误的“添加执行”选项。我意识到每个表单也有一个相应的“操作”菜单。此菜单还有“添加执行”选项。

enter image description here

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

enter image description here

关于java - Keycloak - 自定义表单操作在流程中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55284482/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com