authentication - Picketlink 没有选择我的用户定义的验证器

标签 authentication ear jsf-2.2 wildfly-8 picketlink

我正在尝试使用 PickeLink 2.6.0(EAR,Wildfly 8.1.0)实现 JSF 身份验证,如 PicketLink“picketlink-authentication-jsf”快速入门中所示。我提供了一个标有 @PicketLink 注释的身份验证,但 Identity.login() 总是返回 FAILED。这是我的 JSF 表单:

    <h:form>
        <h:panelGrid styleClass="full">
            <h:inputText value="#{loginCredentials.userId}" required="true"
                pt:placeholder="Username" />
            <h:inputSecret value="#{loginCredentials.password}" required="true"
                pt:placeholder="Password" />
            <h:commandButton value="Login" action="#{loginAction.login()}" />
        </h:panelGrid>
    </h:form>

这是我在 WAR 模块中的 LoginAction bean:

import java.util.logging.Logger;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import org.picketlink.Identity;
import org.picketlink.Identity.AuthenticationResult;
import org.picketlink.credential.DefaultLoginCredentials;

@RequestScoped
@Named
public class LoginAction {

    @Inject
    private Identity identity;
    @Inject
    private DefaultLoginCredentials credentials;

    protected Logger log = Logger.getLogger(this.getClass().getSimpleName());

    public void login() {
        this.log.info(String.format("%s => %s", this.credentials.getUserId(), this.credentials.getPassword()));  // Does get printed!
        AuthenticationResult result = this.identity.login();
        this.log.info(result.toString());

        if (AuthenticationResult.FAILED.equals(result)) {
            FacesContext.getCurrentInstance().addMessage(
                    null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR,
                            "Authentication was unsuccessful.  Please check your username and password "
                                    + "before trying again.", ""));
        }
    }
}

还有我在 EJB 模块中的 Authenticator:

import java.util.logging.Logger;    
import javax.inject.Inject;    
import org.picketlink.annotations.PicketLink;
import org.picketlink.authentication.BaseAuthenticator;
import org.picketlink.credential.DefaultLoginCredentials;

@PicketLink
public class Authenticator extends BaseAuthenticator {

    @Inject
    private DefaultLoginCredentials credentials;
    @Inject
    private ApplicationAuthenticator applicationAuthenticator;

    protected Logger log = Logger.getLogger(this.getClass().getSimpleName());

    @Override
    public void authenticate() {
        this.log.info("authenticate"); // Not printed!
        this.log.info(String.format("%s => %s", this.credentials.getUserId(), this.credentials.getPassword()));

        ProcessResult auth = this.applicationAuthenticator.authUser(
                this.credentials.getUserId(), this.credentials.getPassword());
        this.log.info(auth.toString());

        if (auth.getResult()) {
            this.setStatus(AuthenticationStatus.SUCCESS);
            this.log.info(AuthenticationStatus.SUCCESS.toString());
        } else {
            this.setStatus(AuthenticationStatus.FAILURE);
            this.log.info(AuthenticationStatus.FAILURE.toString());
        }
    }

}

看起来好像我的 Authenticator 根本没有被调用。这是我从日志中得到的:

12:25:00,093 INFO  [LoginAction] (LoginAction.java:27) defaultuser => defaultpass
12:25:00,105 INFO  [idm] (DefaultPartitionManager.java:165) PLIDM001000: Bootstrapping PicketLink IDM Partition Manager
12:25:00,107 INFO  [store] (AbstractIdentityStore.java:50) PLIDM001001: Initializing Identity Store [class org.picketlink.idm.file.internal.FileIdentityStore]
12:25:00,110 WARN  [file] (FileDataSource.java:173) PLIDM001101: Working directory [C:\Users\JPANGA~1\AppData\Local\Temp\pl-idm] is marked to be always created. All your existing data will be lost.
12:25:00,165 INFO  [file] (FileDataSource.java:180) PLIDM001100: Using working directory [C:\Users\JPANGA~1\AppData\Local\Temp\pl-idm].
12:25:00,252 INFO  [LoginAction] (LoginAction.java:29) FAILED

PicketLinks jar 位于 $EAR_ROOT/lib。

我在 http://docs.jboss.org/picketlink/2/latest/reference/html-single/ 阅读了文档看起来我没有遗漏任何东西。为什么我的身份验证器无法工作?

最佳答案

在更加熟悉 CDI 之后,我意识到了我的愚蠢错误(答案不在 PLINK 文档中,而是在 EE 文档中)。我只需要添加

@Named
@RequestScoped

到 bean 。设置认证状态后不设置账号还是不行(我漏了):

this.setStatus(AuthenticationStatus.SUCCESS);
this.setAccount(new User(username)); // <-- don't miss this!

关于authentication - Picketlink 没有选择我的用户定义的验证器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24764526/

相关文章:

java - 蚁战任务如何进行?

templates - 何时在 JSF 2.2 中使用资源库契约?

email - Plesk/MailEnable SMTP 中继错误

python - 再次来自用户的 check_password()

java - war 的 lib 和 Ear 的 lib 之间的 Wildfly 类加载器问题

maven-2 - 如何使用 Maven 在 RAD 中构建 J2EE EAR 文件?

jsf - 刷新页面时清除SessionScoped bean

jsf - 通过复合组件传递 MethodParameter

windows - SVN授权失败

c# - 如何在C#中使用SHA1和MD5?(性能和安全性哪个更好)