簡體   English   中英

這個LoginModule出了什么問題? 以及如何使用Jboss EAP 6.1中的JAAS對用戶進行身份驗證?

[英]What's wrong with this LoginModule? And how to authenticate users with JAAS in Jboss EAP 6.1?

我正在嘗試使用JAAS在Java EE應用程序中實現用於檢查用戶名和密碼的身份驗證/授權模塊。 我正在使用Jboss EAP 6.1進行部署。 但是在我的webapp的所有頁面中使用LoginModule后,我得到403(拒絕訪問)。 我的LoginModule是硬編碼的,用於檢查“user123”和“pass123”,並為我的用戶設置“admin”角色。

import javax.security.auth.Subject;
import javax.security.auth.callback.*;
import javax.security.auth.login.LoginException;
import javax.security.auth.spi.LoginModule;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LoginMod implements LoginModule {

private CallbackHandler handler;
private Subject subject;
private UserPrincipal userPrincipal;
private RolePrincipal rolePrincipal;
private String login;
private List<Principal> userGroups;
private boolean succeeded = false;

@Override
public void initialize(
        Subject subject,
        CallbackHandler callbackHandler,
        Map<String, ?> sharedState,
        Map<String, ?> options)
{

    handler = callbackHandler;
    this.subject = subject;
}

@Override
public boolean login() throws LoginException
{
    Callback[] callbacks = new Callback[2];
    callbacks[0] = new NameCallback("login");
    callbacks[1] = new PasswordCallback("password", true);

    try {
        handler.handle(callbacks);
        String name = ((NameCallback) callbacks[0]).getName();
        String password = String.valueOf(((PasswordCallback) callbacks[1])
                .getPassword());

        if (name != null &&
                name.equals("user123") &&
                password != null &&
                password.equals("pass123")) {

            System.out.println("\t\t[LoginMod] login:"+name);
            login = name;
            userGroups = new ArrayList<Principal>();
            userGroups.add(new RolePrincipal("admin"));
            succeeded = true;
            return true;
        }
        throw new LoginException("Authentication failed");

    } catch (IOException e) {
        throw new LoginException(e.getMessage());
    } catch (UnsupportedCallbackException e) {
        throw new LoginException(e.getMessage());
    }

}

@Override
public boolean commit() throws LoginException
{
    if (succeeded == false) {
        return false;
    } else {
        userPrincipal = new UserPrincipal(login);
        subject.getPrincipals().add(userPrincipal);

        if (userGroups != null && userGroups.size() > 0) {
            for (Principal role : userGroups) {
                rolePrincipal = new RolePrincipal(role.getName());
                subject.getPrincipals().add(rolePrincipal);
            }
        }

        System.out.println("\t\t[LoginMod] subject contains the role:"+
                subject.getPrincipals().contains(rolePrincipal));
        return true;
    }
}

@Override
public boolean abort() throws LoginException
{
    System.out.println("\t\t[LoginMod] Aborted" );
    return false;
}

@Override
public boolean logout() throws LoginException
{
    subject.getPrincipals().remove(userPrincipal);
    subject.getPrincipals().remove(rolePrincipal);
    return true;
}
}

我創建了一個安全域,並在我的standalone.xml中為此模塊添加了一個引用:

<security-domains>
 <security-domain name="acfwebRealm" cache-type="default">
   <authentication>
      <login-module code="acfweb.autenticacao.teste.LoginMod" flag="required"/>
   </authentication>
 </security-domain>
</security-domains>

並在我的應用程序中聲明一個jboss-web.xml文件:

 <?xml version='1.0' encoding='UTF-8'?>
   <jboss-web>
     <context-root>acfweb</context-root>
     <security-domain>acfwebRealm</security-domain>
   </jboss-web>

在我的web.xml中,我有:

<welcome-file-list>
    <welcome-file>interfaces/pages/index.xhtml</welcome-file>
</welcome-file-list>
<security-constraint>
    <web-resource-collection>
        <web-resource-name>Usuarios Autenticados</web-resource-name>
        <url-pattern>/interfaces/*</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>admin</role-name>
    </auth-constraint>
</security-constraint>

<security-role>
    <role-name>admin</role-name>
</security-role>

<login-config>
    <auth-method>FORM</auth-method>
    <realm-name>acfwebRealm</realm-name>
    <form-login-config>
        <form-login-page>/logar.xhtml</form-login-page>
        <form-error-page>/logar.xhtml</form-error-page>
    </form-login-config>
</login-config>

我創建一個j_表單來使用LoginModule,這會正確調用我的LoginModule。

 <form method="post" action="j_security_check">
    <h:panelGrid columns="2">
        <h:outputLabel value="Username: " />
        <input type="text" id="j_username" name="j_username" />
        <h:outputLabel value="Password: " />
        <input type="password" id="j_password" name="j_password" />
        <h:outputText value="" />
        <h:panelGrid columns="1">
            <input type="submit" name="submit" value="Login" />
        </h:panelGrid>
    </h:panelGrid>
    <br />
</form>

發送表單數據后,我的服務器控制台顯示LoginModule sysout日志:

INFO [stdout](http-/127.0.0.1:8080-2) [LoginMod] login:user123
INFO [stdout](http-/127.0.0.1:8080-2) [LoginMod] subject contains the role true

然后我得到403(拒絕訪問),之后的所有頁面。

實際上,我不需要在數據庫或外部LDAP中驗證我的用戶,因為我有另一個可用的服務。 也許我不需要使用JAAS的完整安全模塊,但是我需要通過我的EJBS中的角色來控制我的用戶,而不是在從Managed Beans到EJB的每次調用中發送代表我的用戶的數據,由Servlet過濾器捕獲,例如任何人都可以幫助我? 我在這個LoginModule中做錯了什么? 對描述的要求有什么選擇?

嘗試更改jboss-web.xml

<jboss-web>  
    <security-domain>java:/jaas/acfwebRealm</security-domain>
</jboss-web>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM