如何在liferay 7中覆盖类DefaultScreenNameValidator?

时间:2016-11-08 06:15:51

标签: liferay liferay-7

我试图覆盖实现DefaultScreenNameValidator接口的类ScreenNameValidator。为此,我复制了该类并将其放入另一个模块中。我做的一个改变是注释如下: -

@Component(
        property = {
                "service.ranking:Integer=500"
                }

    )

我使用它成功构建了一个。但是当我尝试部署项目时,我收到错误java.lang.NoClassDefFoundError: com/liferay/portal/kernel/security/auth/ScreenNameValidator。你能否告诉我如何根除这个错误。 Thanx提前..

2 个答案:

答案 0 :(得分:1)

我想知道,创建一个同样实现ScreenNameValidator接口的模块并在那里定义你的自定义逻辑不是更好吗?然后你可以简单地告诉Liferay使用那个验证器而不是DefaultScreenNameValidator

例如,简约实现:

import com.liferay.portal.kernel.security.auth.ScreenNameValidator;

import org.osgi.service.component.annotations.Component;

@Component(
    immediate = true,
    service = ScreenNameValidator.class
)
public class CustomScreenNameValidator implements ScreenNameValidator {

    @Override
    public boolean validate(long companyId, String screenName) {
        // Your custom logic
    }
}

答案 1 :(得分:0)

确保您在build.gradle中具有对portal-kernel的依赖性

dependencies {
   compile 'com.liferay.portal:com.liferay.portal.kernel:2.0.0'

我使用blade-cli创建了一个screenNameValidator,你可以在https://github.com/bruinen/liferay-blade-samples/tree/master/liferay-workspace/modules/blade.screenname.validator

看到这个项目
import com.liferay.portal.kernel.security.auth.ScreenNameValidator;
import org.osgi.service.component.annotations.Component;

import java.util.Locale;

@Component(
        immediate = true,
        property = {"service.ranking:Integer=100"},
        service = ScreenNameValidator.class
)
public class CustomScreenNameValidator implements ScreenNameValidator {

    @Override
    public String getAUIValidatorJS() {
        return "function(val) {return !(val.indexOf(\"admin\") !==-1)}";
    }

    @Override
    public String getDescription(Locale locale) {
        return "The screenName contains reserved words";
    }

    @Override
    public boolean validate(long companyId, String screenName) {
        return !screenName.contains("admin");
    }
}