JAAS LoginModule在Websphere Liberty中不起作用

时间:2016-02-11 09:05:00

标签: java-ee websphere jaas websphere-liberty

我需要在Liberty中实现JAAS LoginModule。

我已经能够实现和配置(执行login()和commit()方法并返回true)自定义模块但它仍然无效(我仍然被重定向到登录页面,就像容器一样并不真正认为用户已经过身份验证。)

基于我在IBM documentation上所读到的内容,我很确定我需要在“login()方法”中做一些事情,我只是不知道是什么。 我已经看到了一些例子,其中的东西被添加到了主题或“sharedState”地图中,但我并没有找到能够确切知道我的代码需要的内容。

因此,我们将非常感谢如何将用户/组/角色传递回容器的一些代码示例。

我已经设法取得了一些进展,现在验证工作正常,但授权没有。

我已将此代码添加到login()方法中:

    java.util.Hashtable<String, Object> customProperties = 
            (java.util.Hashtable<String, Object>) sharedState.
            get(AttributeNameConstants.WSCREDENTIAL_PROPERTIES_KEY);
    if (customProperties == null) {
        customProperties = new java.util.Hashtable<String, Object>();
    }

    customProperties.put(AttributeNameConstants.WSCREDENTIAL_UNIQUEID, username);
    customProperties.put(AttributeNameConstants.WSCREDENTIAL_SECURITYNAME, username);
    ((Map<String, Object>)sharedState).put(AttributeNameConstants.WSCREDENTIAL_PROPERTIES_KEY,
            customProperties);

在此之后,一切正常,但只有在server.xml中使用以下内容:

 <application-bnd>
    <security-role name="adminrole">
      <special-subject type="ALL_AUTHENTICATED_USERS" />
    </security-role>
  </application-bnd>

因此,所有经过身份验证的用户都具有访问我的应用程序所需的角色。

但是,如果我尝试用用户或组替换特殊主题,我会收到错误消息:

CWWKS9104A: Authorization failed for user userXXX:defaultRealm while invoking CustomLoginTest on /. The user is not granted access to any of the required roles: [adminrole].

所以必须从login()方法返回其他东西(也许我还需要在主题中设置一些东西)。 将用户组列表添加到sharedState映射也不起作用:

customProperties.put(AttributeNameConstants.WSCREDENTIAL_GROUPS, groups);

2 个答案:

答案 0 :(得分:1)

需要其他默认的loginModules来创建用于身份验证的WebSphere凭证。请查看本文档,了解如何配置JAAS自定义登录模块。 https://www-01.ibm.com/support/knowledgecenter/SSEQTP_8.5.5/com.ibm.websphere.wlp.doc/ae/twlp_sec_jaas.html?cp=SSEQTP_8.5.5&lang=en 您的自定义JAAS登录模块可以使用sharedState或subject中的hashmap将用户信息传递给其他默认loginModule。

答案 1 :(得分:0)

现在是拼图的最后一部分。 对于授权来工作返回用户组和基本注册表中所需的组所需的代码。 代码如下所示:

    customProperties.put(AttributeNameConstants.WSCREDENTIAL_UNIQUEID, username);
    customProperties.put(AttributeNameConstants.WSCREDENTIAL_SECURITYNAME, username);
    List<String> groups = new ArrayList<String>();
    groups.add("oidc-users");       
    customProperties.put(AttributeNameConstants.WSCREDENTIAL_GROUPS, groups);

和server.xml:

<basicRegistry id="basic" realm="BasicRealm">
    <group name="oidc-users"></group>
</basicRegistry>

<webApplication id="CustomLoginTest" location="CustomLoginTest.war" name="CustomLoginTest">
 <application-bnd>
    <security-role name="authenticated">
        <group name="oidc-users" />
    </security-role>
  </application-bnd>
</webApplication>

当然还有web.xml:

<security-constraint>
    <web-resource-collection>
        <web-resource-name>Demo Servlet</web-resource-name>
        <url-pattern>/ *</url-pattern>
    </web-resource-collection>
    <auth-constraint>
        <role-name>authenticated</role-name>
    </auth-constraint>
</security-constraint>