一般情况下,如果可能的话,我通常建议使用容器身份验证/授权,因为它已由服务器基础架构提供,并且在大多数情况下足够。但是,如果在您的情况下需要,请参考以下提示。如果您希望避免额外的检查,并允许对不在WebSphere用户注册表中的用户进行身份验证,您必须创建完整的主题(这是固定用户简化),而不是使用回调函数。
public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject,
Subject serviceSubject) throws AuthException {
String uniqueid = "test";
String username = "test";
String password = "test";
Hashtable hashtable = new Hashtable();
hashtable.put(AttributeNameConstants.WSCREDENTIAL_UNIQUEID, uniqueid);
hashtable.put(AttributeNameConstants.WSCREDENTIAL_SECURITYNAME, username);
hashtable.put(AttributeNameConstants.WSCREDENTIAL_PASSWORD, password);
List groups = new ArrayList();
groups.add("testers");
hashtable.put(AttributeNameConstants.WSCREDENTIAL_GROUPS, groups);
hashtable.put(AttributeNameConstants.WSCREDENTIAL_CACHE_KEY, "myCustomAttribute" + uniqueid);
clientSubject.getPrivateCredentials().add(hashtable);
return AuthStatus.SUCCESS;
}
我假设您想将这些用户映射到应用程序中的某些安全角色。您可以使用用户、组或特殊主体进行映射。您需要在您的
application.xml
中定义角色,如下所示:
<security-role>
<role-name>user</role-name>
</security-role>
您需要绑定文件,因为无法通过控制台为不存在的用户/组执行绑定。创建 ibm-application-bnd.xml
文件:
<?xml version="1.0" encoding="UTF-8"?>
<application-bnd xmlns="http://websphere.ibm.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://websphere.ibm.com/xml/ns/javaee http://websphere.ibm.com/xml/ns/javaee/ibm-application-bnd_1_2.xsd"
version="1.2">
<security-role name="user">
<user name="test" access-id="user:defaultWIMFileBasedRealm/test"/>
<group name="testers" access-id="group:defaultWIMFileBasedRealm/testers"/>
<special-subject type="ALL_AUTHENTICATED_USERS" />
</security-role>
</application-bnd>
我为各种映射提供了示例,使用适合您需求的映射:
user
- 将命名用户映射到角色
group
- 将组映射到角色
special-subject
- 如果您希望任何经过身份验证的用户都拥有该角色。
重要提示:如果您想使用虚拟用户/组,则必须提供access-id
属性;如果它们在注册表中,则只需提供name
。
另请参见: