65.9K
CodeProject 正在变化。 阅读更多。
Home

简化 CAS 以实现安全

starIconstarIconstarIconstarIconemptyStarIcon

4.00/5 (1投票)

2008年7月17日

CPOL

1分钟阅读

viewsIcon

17482

一篇关于简化 CAS 以实现安全的文章。

引言

在实施中央身份验证服务 (CAS) 时,我的客户不想支持 CAS WAR 的服务注册部分。CAS Web 应用程序有一些 JSP 页面,用于提供一个 UI,用于注册将由 CAS 支持的应用程序或服务。我们的安全主管不想在应用程序中提供任何管理访问权限,该权限会验证我们的用户和企业的凭据。最初我们使用了服务管理器,并且由于使用了 Spring Webflow,因此在应用程序注册完成后,就可以删除 /services 目录。这样做很干净,并且不会显示任何错误消息。事实上,如果调用服务 URL,则会显示登录页面。听起来很完美,对吧?其实不然。

我的 CAS WAR 现在已被修改为完全支持删除服务管理器应用程序。在这些修改之前,服务注册被持久化到文件系统,使用 HSQL 数据库和 CAS 提供的持久化类。我开发了一种更简单的机制来注册服务应用程序,方法是在 Spring 上下文文件中配置它们,并在 CAS 部署时将它们加载到内存中。这是通过 ServiceRegistryDao 的自定义实现来完成的。此自定义文件的源代码在此处提供。

代码

/*
 * Copyright 2007 The JA-SIG Collaborative. All rights reserved. See license
 * distributed with this file and available online at
 * http://www.uportal.org/license.html
 */
package org.jasig.cas.services;

import java.util.*;

import org.jasig.cas.util.DefaultLongNumericGenerator;
import org.jasig.cas.util.LongNumericGenerator;
import org.jasig.cas.util.annotation.NotNull;

/**
 * Brute-force Service Registry Dao for production use (limited service registry).
 * This class is created for a small number of service registrations that will
 * be Spring configured and managed without the use of the services Web
 * interface.
 *
 * @author David Whitehurst
 * @version $
 * @since 3.1
 *
 */

public final class EasyServiceRegistryDaoImpl implements ServiceRegistryDao {

    @NotNull
    private List registeredServices = new ArrayList();

    private LongNumericGenerator generator = new DefaultLongNumericGenerator();

    private Map serviceMap = new HashMap();

    public boolean delete(RegisteredService registeredService) {
        return this.registeredServices.remove(registeredService);
    }

    public RegisteredService findServiceById(final long id) {
        for (final RegisteredService r : this.registeredServices) {
            if (r.getId() == id) {
                return r;
            }
        }

        return null;
    }

    public List load() {

    // always load Service Manager application
    registerApplication("Service Manager", "https://:8443/cas/services/**");

    // get registered applications
    addRegisteredApplications();

        return this.registeredServices;
    }

    public RegisteredService save(final RegisteredService registeredService) {
        if (registeredService.getId() == -1) {
            ((RegisteredServiceImpl) registeredService).setId
                        (this.generator.getNextLong());
        }

        this.registeredServices.remove(registeredService);
        this.registeredServices.add(registeredService);

        return registeredService;
    }

    public void setRegisteredServices(final List registeredServices) {
        this.registeredServices = registeredServices;
    }

    public void setServiceMap(Map serviceMap) {
        this.serviceMap = serviceMap;
    }

    private void addRegisteredApplications() {

        for (Iterator it=serviceMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry entry = (Map.Entry)it.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();

            // register application
            registerApplication(key, value);
        }
    }

    private void registerApplication(String name, String urlPattern) {

        final RegisteredServiceImpl r = new RegisteredServiceImpl();
        r.setName(name);
        r.setDescription("This is the description for the " + name);
        r.setServiceId(urlPattern);
        r.setAllowedToProxy(true);
        r.setAnonymousAccess(false);
        r.setEnabled(true);
        r.setSsoEnabled(true);
        r.setId(this.generator.getNextLong());

        if (r.getId() == -1) {
            ((RegisteredServiceImpl) r).setId(this.generator.getNextLong());
        }

        this.registeredServices.remove(r);
        this.registeredServices.add(r);
    }
}

现在注册 CAS 服务非常容易。在 WEB-INF/deployerConfigContext.xml 的服务注册部分,只需使用名称和 URL 即可添加服务。请记住,URL 使用 Apache 正则表达式语法。

<!--
    |
    | This DAO implementation allows for the registration of
    | CAS services at a configuration level.
    | hard coded service registry.
    | The cas/services directory can be removed from /WEB-INF/view/jsp
    | so that UI access to the service manager is completely disabled.
    | The following properties are set as true by default,
    | enabled, allowed to proxy, and SSO.
    |
+ -->

    <bean
        id="serviceRegistryDao"
        class="org.jasig.cas.services.EasyServiceRegistryDaoImpl">
        <property name="serviceMap">
          <map>
            <entry key="Test Application" value="https://:8080/test1/**" />
          </map>
        </property>
    </bean>
© . All rights reserved.