Wildfly中的自定义登录模块中的“禁止”

时间:2015-05-28 07:08:18

标签: java-ee jboss wildfly wildfly-8 jaas

我已配置自定义登录模块

在域“guest”中为领域ApplicationRealm添加了一个应用程序用户“jmsuser”。但是我仍然在结果页面中得到“禁止”。

独立-full.xml

<default-security-domain value="MyLoginModule"/>
<security-domain name="MyLoginModule" cache-type="default">
                    <authentication>
                        <login-module code="com.auth.MyLoginModule" flag="required"/>
                    </authentication>
</security-domain>

Web.xml中

<security-role>  
    <role-name>guest</role-name>  
</security-role>  
<security-constraint>  
    <web-resource-collection>  
        <web-resource-name></web-resource-name>  
        <url-pattern>/*</url-pattern>  
        <http-method>GET</http-method> 
    <http-method>POST</http-method> 
    </web-resource-collection>  
    <auth-constraint>  
        <role-name>guest</role-name>  
    </auth-constraint>  
</security-constraint>  
<login-config>  
    <auth-method>BASIC</auth-method>  
<realm-name>MyLoginModule</realm-name>
</login-config>

的JBoss-web.xml中

<?xml version="1.0" encoding="UTF-8"?>
<jboss-web> 
    <security-domain>MyLoginModule</security-domain> 
</jboss-web>

MyLoginModule.java

public boolean login() throws LoginException
    {
//returns true;
}

Audit.log

11:17:51,560 TRACE [org.jboss.security] (default task-2) PBOX000224: End getAppConfigurationEntry(MyLoginModule), AuthInfo: AppConfigurationEntry[]:
[0]
LoginModule Class: com.auth.MyLoginModule
ControlFlag: LoginModuleControlFlag: required
Options:

11:19:16,232 TRACE [org.jboss.security] (default task-2) PBOX000210: defaultLogin, login context: javax.security.auth.login.LoginContext@8ba05ec, subject: Subject(1075984995).principals=com.auth.MyLoginModulePrincipal@1936852516(MyLoginModulePrincipal:  jmsuser)
11:19:18,863 TRACE [org.jboss.security] (default task-2) PBOX000207: updateCache, input subject: Subject(1075984995).principals=com.auth.MyLoginModulePrincipal@1936852516(MyLoginModulePrincipal:  jmsuser), cached subject: Subject(689516194).principals=com.auth.MyLoginModulePrincipal@1936852516(MyLoginModulePrincipal:  jmsuser)
11:19:18,865 TRACE [org.jboss.security] (default task-2) PBOX000208: Inserted cache info: org.jboss.security.authentication.JBossCachedAuthenticationManager$DomainInfo@6a06d399
11:19:18,866 TRACE [org.jboss.security] (default task-2) PBOX000201: End isValid, result = true
11:19:18,879 TRACE [org.jboss.security] (default task-2) PBOX000354: Setting security roles ThreadLocal: null

更新: 我在主题中添加了主体和角色,但仍然获得Forbidden.PFB更新MyLoginModule类

 private Principal userPrincipal;
 private RolePrincipal rolePrincipal;
 private List<String> userGroups;
...
public login(){
//returns true;
}

@Override
    public boolean commit() throws LoginException {

        if (!isAuthenticated) {
            return false;
        } else {

            userPrincipal = new Principal(username);
            subject.getPrincipals().add(userPrincipal);

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

            commitSucceeded = true;

            return true;
        }
    }
      private List<String> getRoles() {

        List<String> roleList = new ArrayList<>();
        roleList.add("guest");

        return roleList;
    }

2 个答案:

答案 0 :(得分:1)

如果您只想使用ApplicationRealm及其属性文件中定义的用户/角色,则只需从部署中删除jboss-web.xml 。它将自动使用other安全域(默认域) - 它使用ApplicationRealm作为用户存储库。

如果您想在新的安全域中使用自己的登录模块,则无需触摸ApplicationRealm

您需要在自定义登录模块中执行以下操作:

  1. 对用户进行身份验证(向主题添加带有用户名的主体)

  2. 在主题

  3. 中为 Roles 组添加一些角色

    如果您使用用户名/密码身份验证,那么最简单的方法是扩展抽象的PicketBox类org.jboss.security.auth.spi.UsernamePasswordLoginModule

    查看 JBoss EAP Security Guide 以获取有关如何为JBoss应用程序服务器实现自定义登录模块的详细信息。

答案 1 :(得分:1)

您的模块是直接还是间接延伸AbstractServerLoginModule

  • 如果是,则需要设置loginOk = true
  • 如果没有commit也需要返回true