CAS Client-初始化参数处理

系统在继承CAS Client时,需要通过参数对其进行配置。该配置一般以Filter初始化参数或全局初始化参数的形式体现。

1 <context-param>
2   <description>全局参数配置</description>
3   <param-name>casServerLoginUrl</param-name>
4   <param-value>http://localhost:8080/cas/login</param-value>
5 </context-param>
 1 <filter>
 2     <filter-name>CAS Authentication Filter</filter-name>
 3     <filter-class>
 4             org.jasig.cas.client.authentication.AuthenticationFilter
 5     </filter-class>
 6     <init-param>
 7       <param-name>casServerLoginUrl</param-name>
 8       <param-value>
 9        http://localhost:8080/cas/login
10      </param-value>
11     </init-param>
12     <init-param>
13       <param-name>renew</param-name>
14       <param-value>false</param-value>
15     </init-param>
16     <init-param>
17       <param-name>gateway</param-name>
18       <param-value>false</param-value>
19     </init-param>
20     <init-param>
21       <param-name>serverName</param-name>
22       <param-value>http://localhost:8081</param-value>
23     </init-param>
24 </filter>
25 <filter-mapping>
26     <filter-name>CAS Authentication Filter</filter-name>
27     <url-pattern>*.do</url-pattern>
28 </filter-mapping>

在CAS Client的各个过滤器中,获取初始化参数会调用AbstractConfigurationFilter类的getPropertyFromInitParams()方法。

 1 public void init(final FilterConfig filterConfig) throws ServletException {
 2         if (!isIgnoreInitConfiguration()) {
 3             HANDLER.setArtifactParameterName(getPropertyFromInitParams(filterConfig, "artifactParameterName",
 4                     SingleSignOutHandler.DEFAULT_ARTIFACT_PARAMETER_NAME));
 5             HANDLER.setLogoutParameterName(getPropertyFromInitParams(filterConfig, "logoutParameterName",
 6                     SingleSignOutHandler.DEFAULT_LOGOUT_PARAMETER_NAME));
 7             HANDLER.setFrontLogoutParameterName(getPropertyFromInitParams(filterConfig, "frontLogoutParameterName",
 8                     SingleSignOutHandler.DEFAULT_FRONT_LOGOUT_PARAMETER_NAME));
 9             HANDLER.setRelayStateParameterName(getPropertyFromInitParams(filterConfig, "relayStateParameterName",
10                     SingleSignOutHandler.DEFAULT_RELAY_STATE_PARAMETER_NAME));
11             HANDLER.setCasServerUrlPrefix(getPropertyFromInitParams(filterConfig, "casServerUrlPrefix", ""));
12             HANDLER.setArtifactParameterOverPost(parseBoolean(getPropertyFromInitParams(filterConfig,
13                     "artifactParameterOverPost", "false")));
14             HANDLER.setEagerlyCreateSessions(parseBoolean(getPropertyFromInitParams(filterConfig,
15                     "eagerlyCreateSessions", "true")));
16         }
17         HANDLER.init();
18         handlerInitialized.set(true);
19     }
protected final String getPropertyFromInitParams(final FilterConfig filterConfig, final String propertyName,
            final String defaultValue) {
        //从过滤器的初始化参数中读取
        final String value = filterConfig.getInitParameter(propertyName);

        if (CommonUtils.isNotBlank(value)) {
            if ("renew".equals(propertyName)) {
                throw new IllegalArgumentException(
                        "Renew MUST be specified via context parameter or JNDI environment to avoid misconfiguration.");
            }
            logger.info("Property [{}] loaded from FilterConfig.getInitParameter with value [{}]", propertyName, value);
            return value;
        }
        //从全局初始化参数中读取
        final String value2 = filterConfig.getServletContext().getInitParameter(propertyName);

        if (CommonUtils.isNotBlank(value2)) {
            logger.info("Property [{}] loaded from ServletContext.getInitParameter with value [{}]", propertyName,
                    value2);
            return value2;
        }
        InitialContext context;
        try {
            context = new InitialContext();
        } catch (final NamingException e) {
            logger.warn(e.getMessage(), e);
            return defaultValue;
        }

        final String shortName = this.getClass().getName().substring(this.getClass().getName().lastIndexOf(".") + 1);
        //从过滤器对应的JNDI目录下读取
        final String value3 = loadFromContext(context, "java:comp/env/cas/" + shortName + "/" + propertyName);

        if (CommonUtils.isNotBlank(value3)) {
            logger.info("Property [{}] loaded from JNDI Filter Specific Property with value [{}]", propertyName, value3);
            return value3;
        }
        //从全局的JNDI目录下读取
        final String value4 = loadFromContext(context, "java:comp/env/cas/" + propertyName);

        if (CommonUtils.isNotBlank(value4)) {
            logger.info("Property [{}] loaded from JNDI with value [{}]", propertyName, value4);
            return value4;
        }

        logger.info("Property [{}] not found.  Using default value [{}]", propertyName, defaultValue);
        return defaultValue;
    }        

从上面代码中可以看出,CAS Client支持通过JNDI指定参数。

你可能感兴趣的:(CAS Client-初始化参数处理)