自定义身份验证策略

虽然CAS中对各种系统的身份验证支持有些全面和复杂,但一个常见的部署用例是设计自定义身份验证方案的任务。本文描述了在CAS中设计和注册自定义身份验证策略(即AuthenticationHandler)所需的必要步骤。

本指南真正适用于对 Spring、Spring Boot和 Spring Webflow 有基本到中等熟悉程度的开发人员。这不是通过复制/粘贴逐字使用的教程。相反,它是开发人员根据特定需求扩展CAS的一种方法。

概述

总体任务可分为以下几类:

  1. 设计身份验证处理程序。
  2. 向CAS身份验证引擎注册身份验证处理程序。
  3. 让CAS识别身份验证配置。

设计身份验证处理程序

第一步是定义身份验证处理程序本身的框架。这是核心的主要组件,其工作是声明对给定类型的凭证的支持,然后尝试验证它并产生成功的结果。所有处理程序从中扩展的核心父组件是 AuthenticationHandler 接口。

假设这里使用的凭据类型处理传统用户名和密码,下面臭名昭著的 UsernamePasswordCredential 指出了这一点,那么为自定义身份验证处理程序定义一个更合适的框架可能看起来像下面的示例:

package com.example.cas;

public class MyAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
    ...
    protected AuthenticationHandlerExecutionResult authenticateUsernamePasswordInternal(
        final UsernamePasswordCredential credential,
        final String originalPassword) {

        if (everythingLooksGood()) {
            return createHandlerResult(credential,
                this.principalFactory.createPrincipal(username), null);
        }
        throw new FailedLoginException("Sorry, you are a failure!");
    }
    ...
}

检查

  1. Authentication handlers 能够生成完全解析的主体以及属性。如果您有能力从与原始用户/主体帐户存储相同的位置检索属性,那么在这里解析的最终主体对象必须能够在构造时在其中携带所有这些属性和声明。

  2. The last parameter, null, 实际上是一组警告,这些警告最终会进入身份验证链并有条件地显示给用户。此类警告的示例包括接近到期日期的密码状态等。

  3. Authentication handlers 还可以通过抛出一些特定的异常来阻止身份验证。要抛出的一个更常见的异常是FailedLoginException,用于记录身份验证失败。可能会引发其他特定异常,以指示帐户状态本身的异常,例如AccountDisabledException。

  4. 各种其他组件,如PrincipalNameTransformers、PasswordEncoders等,也可以根据需要注入到我们的处理程序中,尽管为了简单起见,本文暂时跳过了这些组件。

注册身份验证处理程序

一旦设计了处理程序,就需要向CAS注册并将其放入身份验证引擎。这是通过@Configuration类的魔力实现的,这些类在运行时根据您的批准自动拾取,其任务是了解如何动态修改应用程序上下文。

package com.example.cas;

@Configuration(value = "MyAuthenticationEventExecutionPlanConfiguration", proxyBeanMethods = false)
@EnableConfigurationProperties(CasConfigurationProperties.class)
public class MyAuthenticationEventExecutionPlanConfiguration
                    implements AuthenticationEventExecutionPlanConfigurer {
    @Autowired
    private CasConfigurationProperties casProperties;

    @Bean
    public AuthenticationHandler myAuthenticationHandler() {
        var handler = new MyAuthenticationHandler();
        /*
            Configure the handler by invoking various setter methods.
            Note that you also have full access to the collection of resolved CAS settings.
            Note that each authentication handler may optionally qualify for an 'order`
            as well as a unique name.
        */
        return h;
    }

    @Override
    public void configureAuthenticationExecutionPlan(final AuthenticationEventExecutionPlan plan) {
        if (feelingGoodOnAMondayMorning()) {
            plan.registerAuthenticationHandler(myAuthenticationHandler());
        }
    }
}

现在,我们已经在CAS认证机制中正确创建并注册了我们的处理程序,我们只需要确保CAS能够获取我们的特殊配置。为此,创建一个src/main/resources/META-INF/spring.factories文件并引用其中的配置类:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.example.cas.MyAuthenticationEventExecutionPlanConfiguration

https://apereo.github.io/cas/6.5.x/authentication/Configuring-Custom-Authentication.html

作者:Jeebiz  创建时间:2022-10-25 14:03
最后编辑:Jeebiz  更新时间:2024-05-06 16:13