配置认证过滤器

门户网站认证过滤器是一组插件点。可以使用它们以通过定制代码来拦截或扩展门户网站登录、注销、会话超时以及请求处理,例如将用户重定向到特定的 URL。

认证过滤器链概念

门户网站中的认证过滤器使用的是由 J2EE Servlet 过滤器工具定义的模式。有关详细信息,请参阅过滤器要点知识。以下示例显示了此模式如何应用于认证过滤器。
Trigger of filter chain, |                        |
   for example explicit  |   CustomFilter1        |
   login or logout      ---> next(..., chain){    |                         |
                         |   // do something #1a  |   CustomFilter2         |
                         |   chain.next(...)     ---> next(..., chain){     |
                         |                        |   // do something #2a   |   DefaultFilter
                         |                        |   chain.next(...)      ---> next(..., chain){
                         |                        |                         |   // Execute the
                         |                        |                         |      default logic
                         |                        |   // do something #2b <---  }
                         |  // do something #1b <---  }                     |  
Redirect, exception,   <--- }                     |  
   or continue           | 
缺省过滤器针对特定用例(例如,登录)执行缺省逻辑。您可以将一组要在缺省过滤器之前运行的定制过滤器链接在一起。过滤器链启动时,它会调用该链中的第一个元素(在示例中为 CustomFilter1)并将某个链接对象作为参数传递给该调用。然后,过滤器实现可以执行某些操作以调用该链对象的相应方法,从而触发该链中的下一个元素 (CustomFilter2)。 此过滤器也可以实现要在调用下一个元素之前运行的某个独立逻辑。 该链的最后一个元素是预定义的 DefaultFilter,它可以确保相应用例的缺省逻辑得以运行。

在运行过滤器之后或抛出异常时,每个过滤器都会返回到调用它的元素处,因此,在调用后继元素之后仍可以实现定制的异常处理或执行更多操作。现在,您可以将一组过滤器链接在一起。每个定制过滤器都可以在链中的下列元素前后执行操作。可以通过门户网站配置属性来指定定制过滤器的顺序和标准类名。有关详细信息,请参阅关于门户网站 WP 认证服务的主题。门户网站仅提供 DefaultFilter 实现和执行,它们通常是链中的最后一个元素;如果没有定义任何定制登录过滤器,缺省过滤器将是唯一元素。

可用的认证过滤器链

上一部分中描述的过滤器链概念适用于 6 种类型的事件,这些事件与门户网站登录、注销以及会话处理的流程有关。这提供了一种将定制逻辑插入到这些流程中的灵活方法。特别是下列事件的过滤器链:
  • 显式登录:这是一种需要用户名和密码的登录,它由接口 com.ibm.portal.auth.ExplicitLoginFilter 表示。例如,这可以是一种使用登录 Portlet 或登录 URL 的登录。
  • 隐式登录:例如,这可以出现在用户已通过 WAS 认证但尚未得到门户网站认证的情况下。它由接口 com.ibm.portal.auth.ImplicitLoginFilter 表示。
  • 显式注销:这意味着由用户直接触发注销操作,例如通过在用户界面中单击注销,它由接口 interface com.ibm.portal.auth.ExplicitLogoutFilter 表示。
  • 隐式注销:例如,这可以出现在会话超时之后,或是在认证的用户访问公开页面时,或是用户在不是相关用户域的成员的情况下浏览虚拟门户网站时。它由接口 com.ibm.portal.auth.ImplicitLogoutFilter 表示。
  • 会话超时:在用户会话的空闲超时发生后立即调用此过滤器。它由接口 com.ibm.portal.auth.SessionTimeoutFilter 表示。
  • 会话验证:在触发操作和呈示页面之前针对每个请求调用此过滤器。它由接口 com.ibm.portal.auth.SessionValidationFilter 表示。
除了会话超时过滤器之外,每个以前的过滤器可以使用 HTTP 请求和响应对象。可以使用特殊的上下文对象在过滤器之间共享信息,并设置在处理完过滤器链之后运行的重定向。有关每个过滤器和过滤器链接口的更多详细信息,请参阅 WebSphere® Portal 和 API JavaDoc 的文档。有关过滤器链示例,请参阅含有定制认证过滤器示例的主题。

配置过滤器链

您可以通过在门户网站 WP 认证服务中设置下列属性来指定各个过滤器链的过滤器顺序
login.explicit.filterchain    = colon or semicolon-separated list of fully qualified class names
login.implicit.filterchain    = colon or semicolon-separated list of fully qualified class names
logout.explicit.filterchain   = colon or semicolon-separated list of fully qualified class names
logout.implicit.filterchain   = colon or semicolon-separated list of fully qualified class names
sessiontimeout.filterchain    = colon or semicolon-separated list of fully qualified class names
sessionvalidation.filterchain = colon or semicolon-separated list of fully qualified class names
注: 使用属性以仅指定定制过滤器元素,因为缺省过滤器实现由门户网站基础结构隐式添加。因此,缺省情况下不会为这些属性设置任何值。
此外,可以根据以下模式在门户网站 WP 认证服务中设置属性:
filterchain.properties.fully qualified class name of the filter implementation.property name
通过使用键 property name,这将使此属性的值在指定类的过滤器配置对象中可用。

有关如何设置门户网站配置属性的详细信息,请参考关于“设置服务配置属性”的主题。

定制认证过滤器示例

下面给出了定制过滤器的示例,此过滤器可插入到显式门户网站登录的过滤器链中。此定制过滤器包含为特殊用户标识定义特殊重定向 URL 的属性,此属性还会在任何这些用户成功登录时触发相应的重定向。要实现这样的示例,请完成下列步骤:
  1. 通过向 WebSphere Portal 应用程序的扩展类路径目录 (PortalServer_root/shared/app) 中添加 JAR 文件,实现 com.ibm.portal.auth.ExplicitLoginFilter 接口并使您的类可用于门户网站类路径。要获取有关如何实现此接口的方法的示例,请参阅以下代码样本:
    package com.ibm.portal.example;
    
    public class UserRedirectLoginFilter implements ExplicitLoginFilter {
    	
        // hash map to store the mappings from user id to redirect URL
           private java.util.Map userToRedirectURLs = new java.util.HashMap();
    
    public void init(SecurityFilterConfig filterConfig)
           throws SecurityFilterInitException {
        // iterate the list of init parameters and store 
           the mappings of user to redirect urls for 
           (java.util.Iterator it = filterConfig.getInitParameterNames(); it.hasNext(); ) {
               String currentParameter = (String)it.next();
               userToRedirectURLs.put(currentParameter, 
               filterConfig.getInitParameter(currentParameter));
            }
        }
    
        public void login(HttpServletRequest req, HttpServletResponse resp,
                    String userID, char[] password,
                    FilterChainContext portalLoginContext, Subject subject,
                    String realm, ExplicitLoginFilterChain chain)
                    throws LoginException, WSSecurityException,
                    PasswordInvalidException, UserIDInvalidException,
                    AuthenticationFailedException, AuthenticationException,
                    SystemLoginException, com.ibm.portal.auth.exceptions.LoginException {
            // call the next element in the filter chain to trigger the default login
               chain.login(req, resp, userID, password, portalLoginContext, subject, realm);
    		
            // if no exception occured, the login was successful
               if (userToRedirectURLs.containsKey(userID)) {
            // set the redirect url for the user if we have an entry
               portalLoginContext.setRedirectURL((String)userToRedirectURLs.get(userID));
            }
        }
    
        public void destroy() {
               // nothing to do here
        }
     }
  2. 在 WP 认证服务属性中指定定制过滤器的类名:
    login.explicit.filterchain=com.ibm.portal.example.UserRedirectLoginFilter
  3. 要定义各用户标识的重定向 URL,请相应指定此类的定制属性集。示例:
    filterchain.properties.com.ibm.portal.example.UserRedirectLoginFilter.alice=/wps/myportal/pageA
    filterchain.properties.com.ibm.portal.example.UserRedirectLoginFilter.bob=/wps/myportal/pageB
  4. 重新启动门户网站。
现在,可以使用用于显式登录的新过滤器。在通过登录 Portlet 或登录 URL 登录后,在属性中定义的用户将重定向到指定 URL。