为 Liberty 开发定制 JASPIC 认证服务提供者
通过创建用于实现 JSR 196: Java™ 容器认证服务提供者接口规范中说明的必需接口的类,可开发定制 Java Authentication SPI for Containers (JASPIC) 认证服务提供者。
开始之前
关于此任务
WebSphere® Application Server Liberty 支持使用与 Java Authentication SPI for Containers (JASPIC) V1.1 中指定的 servlet 容器相符的第三方认证服务提供者。
servlet 容器定义了一些接口,安全性运行时环境将这些接口与 WebSphere Application Server 中的 Web 容器配合使用,以在应用程序处理 Web 请求前后调用认证模块。仅当在安全配置中启用了 JASPIC 时,才会执行使用 JASPIC 模块的认证。
要开发定制认证服务提供者,请创建用于实现“JSR 196: Java Authentication Service Provider Interface for Containers”规范中所记载的必需接口的类。提供程序可以使用一个或多个认证模块进行认证。模块可以使用回调来执行认证,它们也可以将必需的用户身份信息手动添加至客户机主体集。
过程
- 创建用于实现 javax.security.auth.message.config.AuthConfigProvider 接口的类。 AuthConfigProvider 实现类必须定义一个具有两个自变量的公用构造函数以及 getServerAuthConfig 公用方法:
import java.util.Map; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.message.AuthException; import javax.security.auth.message.config.AuthConfigFactory; import javax.security.auth.message.config.AuthConfigProvider; import javax.security.auth.message.config.ServerAuthConfig; public class SampleAuthConfigProvider implements AuthConfigProvider { public SampleAuthConfigProvider(Map<String, String> properties, AuthConfigFactory factory) { ... } public ServerAuthConfig getServerAuthConfig(String layer, String appContext, CallbackHandler handler) throws AuthException { ... } }
要由应用程序的 Web 模块处理的请求到达时,WebSphere Application Server 将使用 AuthConfigProvider 实现类的实例。getServerAuthConfig 方法用于获取 ServerAuthConfig 实例。authentication 模块使用方法调用中的 CallbackHandler 自变量。
- 创建用于实现 javax.security.auth.message.config.ServerAuthConfig 接口的类。 ServerAuthConfig 实现类必须定义 getAuthContextID 和 getAuthContext 公用方法:
import java.util.Map; import javax.security.auth.Subject; import javax.security.auth.message.AuthException; import javax.security.auth.message.MessageInfo; import javax.security.auth.message.config.ServerAuthConfig; import javax.security.auth.message.config.ServerAuthContext; public class SampleServerAuthConfig implements ServerAuthConfig { public String getAuthContextID(MessageInfo messageInfo) throws IllegalArgumentException { ... } public ServerAuthContext getAuthContext(String authContextID, Subject serviceSubject, Map properties) throws AuthException { ... } }
ServerAuthConfig 实现类中的 getAuthContextID 和 getAuthContext 方法用于获取 ServerAuthContext 实例。
- 创建用于实现 javax.security.auth.message.config.ServerAuthContext 接口的类。 ServerAuthContext 实现类必须定义 validateRequest 和 secureResponse 公用方法:
import javax.security.auth.Subject; import javax.security.auth.message.AuthException; import javax.security.auth.message.AuthStatus; import javax.security.auth.message.MessageInfo; import javax.security.auth.message.config.ServerAuthContext; public class SampleServerAuthContext implements ServerAuthContext { public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException { ... } public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException { ... } }
ServerAuthContext 实现类中的 validateRequest 方法用于调用认证所接收 Web 请求消息的模块。如果认证结果成功,那么 Web 容器将分派目标 Web 模块在应用程序中处理的所接收的 Web 请求消息。如果认证结果不成功,那么将使用相应的响应状态来拒绝该请求。
- 创建用于实现 javax.security.auth.message.module.ServerAuthModule 接口的类。 ServerAuthModule 实现类必须定义 initialize、validateRequest 和 secureResponse 公用方法:
import javax.security.auth.Subject; import javax.security.auth.callback.CallbackHandler; import javax.security.auth.message.AuthException; import javax.security.auth.message.AuthStatus; import javax.security.auth.message.MessageInfo; import javax.security.auth.message.MessagePolicy; import javax.security.auth.message.module.ServerAuthModule; public class SampleAuthModule implements ServerAuthModule { public void initialize(MessagePolicy requestPolicy, MessagePolicy responsePolicy, CallbackHandler handler, Map options) throws AuthException { ... } public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException { ... } public AuthStatus secureResponse(MessageInfo messageInfo, Subject serviceSubject) throws AuthException { ... } public void cleanSubject(MessageInfo messageInfo, Subject subject) throws AuthException { ... } }
ServerAuthContext 实现类调用 ServerAuthModule 实现类中的 initialize 方法以初始化 authentication 模块并使其与 ServerAuthContext 实例相关联。
此类中的 validateRequest 和 secureResponse 方法用于认证所接收的 javax.security.auth.message.MessageInfo 中包含的 javax.servlet.http.HttpServletRequest 和 javax.servlet.http.HttpServletResponse。这些方法可使用 initialize 方法中接收到的 CallbackHandler 实例以与 WebSphere 安全运行时交互来验证用户密码,并与活动用户注册表交互以检索用户的唯一标识和成员资格组。检索到的数据放在客户机主体集中的专用凭证集的散列表中。CallbackHandler 的 WebSphere Application Server 实现支持以下三个回调:- CallerPrincipalCallback
- GroupPrincipalCallback
- PasswordValidationCallback
WebSphere Application Server 期望使用 PasswordValidationCallback.getUsername() 和 CallerPrincipalCallback.getName() 获取的名称值相同。如果它们不相同,那么会产生不可预测的结果。CallbackHandler 的 handle() 方法依次处理该方法的自变量数组中给出的每个回调。因此,客户机主体集的专用凭证中的名称值集就是从所处理的最后一个回调获得的名称值集。
如果认证模块未使用 CallbackHandler,并且 validateRequest 返回成功状态,那么 WebSphere Application Server 要求 Hashtable 实例与用户身份信息一起包含在 clientSubject 中,以使可执行定制登录以获取用户的凭证。可如以下示例中所示将此散列表添加至客户机主体集:import java.util.Hashtable; import java.util.String; import javax.security.auth.Subject; import javax.security.auth.message.AuthException; import javax.security.auth.message.AuthStatus; import javax.security.auth.message.MessageInfo; import com.ibm.wsspi.security.registry.RegistryHelper; import com.ibm.wsspi.security.token.AttributeNameConstants.AttributeNameConstants; public AuthStatus validateRequest(MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException { ... UserRegistry reg = RegistryHelper.getUserRegistry(null); String uniqueid = reg.getUniqueUserID(username); Hashtable hashtable = new Hashtable(); hashtable.put(AttributeNameConstants.WSCREDENTIAL_UNIQUEID, uniqueid); hashtable.put(AttributeNameConstants.WSCREDENTIAL_SECURITYNAME, username); hashtable.put(AttributeNameConstants.WSCREDENTIAL_PASSWORD, password); hashtable.put(AttributeNameConstants.WSCREDENTIAL_GROUPS, groupList); //optional clientSubject.getPrivateCredentials().add(hashtable); ... }
有关散列表需求及定制登录的更多信息,请参阅开发用于系统登录配置的 JAAS 定制登录模块。

文件名:twlp_develop_jaspic.html