Spring Security Config : AbstractDaoAuthenticationConfigurer

本文围绕Spring Security的一个安全配置器抽象基类展开,介绍了其继承关系,它继承自多个类并实现接口。阐述了该配置器的功能,如创建对象、设定目标属性等。还说明了其使用主要是被子类实现,同时给出了源代码版本及参考文章。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

介绍

AbstractDaoAuthenticationConfigurerSpring Security Config提供的一个安全配置器抽象基类,它继承自UserDetailsAwareConfigurer,而UserDetailsAwareConfigurer又继承自SecurityConfigurerAdapter,实现了接口SecurityConfigurer。除了来自基类和所实现接口定义的能力,AbstractDaoAuthenticationConfigurer自身又为一个安全配置器进行了如下定义:

  1. 所要创建的AuthenticationProvider是一个DaoAuthenticationProvider;
  2. 提供使用者设定目标DaoAuthenticationProvider属性userDetailsService/userDetailsPasswordService的功能;
  3. 提供使用者设定目标DaoAuthenticationProvider属性passwordEncoder的功能;
  4. 提供使用者设定配置过程中安全对象后置处理器的功能;

作为一个SecurityConfigurerAbstractDaoAuthenticationConfigurer的安全构建器初始化方法为空,而配置方法流程是:

  1. 对目标AuthenticationProvider DaoAuthenticationProvider执行后置处理;
  2. 将目标AuthenticationProvider DaoAuthenticationProvider设置到目标安全构建器;

继承关系

UserDetailsManagerConfigurer

使用

AbstractDaoAuthenticationConfigurer的使用主要体现在被子类实现以提供具体实现类。所以其使用可以参考InMemoryUserDetailsManagerConfigurer或者JdbcUserDetailsManagerConfigurer

源代码

源代码版本 Spring Security Config 5.1.4.RELEASE

package org.springframework.security.config.annotation.authentication.configurers.userdetails;

import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.SecurityBuilder;
import org.springframework.security.config.annotation.authentication.ProviderManagerBuilder;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;

/**
 * Allows configuring a DaoAuthenticationProvider
 *
 * @author Rob Winch
 * @since 3.2
 *
 * @param <B> the type of the SecurityBuilder
 * @param <C> the type of AbstractDaoAuthenticationConfigurer this is
 * @param <U> The type of UserDetailsService that is being used
 *
 */
abstract class AbstractDaoAuthenticationConfigurer<B extends ProviderManagerBuilder<B>, 
	C extends AbstractDaoAuthenticationConfigurer<B, C, U>, U extends UserDetailsService>
		extends UserDetailsAwareConfigurer<B, U> {
    // 将要配置到目标安全构建器的 AuthenticationProvider, 是一个 DaoAuthenticationProvider
	private DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
    // 将要设置到 provider 的 UserDetailsService ,可以是 UserDetailsService 的子类,将会由
    // 使用者提供
	private final U userDetailsService;

	/**
	 * Creates a new instance
	 * 构造函数,使用指定的 UserDetailsService 或者 UserDetailsPasswordService
	 * @param userDetailsService
	 */
	protected AbstractDaoAuthenticationConfigurer(U userDetailsService) {
       // 记录使用者提供的 UserDetailsService
		this.userDetailsService = userDetailsService;
       //  设置 userDetailsService 到 provider
		provider.setUserDetailsService(userDetailsService);
		if (userDetailsService instanceof UserDetailsPasswordService) {
			this.provider.setUserDetailsPasswordService(
				(UserDetailsPasswordService) userDetailsService);
		}
	}

	/**
	 * Adds an ObjectPostProcessor for this class. 增加一个provider对象的后置处理器
	 *
	 * @param objectPostProcessor
	 * @return the AbstractDaoAuthenticationConfigurer for further customizations
	 */
	@SuppressWarnings("unchecked")
	public C withObjectPostProcessor(ObjectPostProcessor<?> objectPostProcessor) {
		addObjectPostProcessor(objectPostProcessor);
		return (C) this;
	}

	/**
	 * Allows specifying the PasswordEncoder to use with the
	 * DaoAuthenticationProvider. The default is to use plain text.
	 *
     * 设置所要配置到安全构建器上的provider的密码加密器
	 * @param passwordEncoder The PasswordEncoder to use.
	 * @return the AbstractDaoAuthenticationConfigurer for further customizations
	 */
	@SuppressWarnings("unchecked")
	public C passwordEncoder(PasswordEncoder passwordEncoder) {
		provider.setPasswordEncoder(passwordEncoder);
		return (C) this;
	}

    // 供使用者设置 provider 属性 userDetailsPasswordService 的工具方法
	public C userDetailsPasswordManager(UserDetailsPasswordService passwordManager) {
		provider.setUserDetailsPasswordService(passwordManager);
		return (C) this;
	}

    // SecurityCongigurer 接口定义的配置方法:对目标安全配置器builder进行配置
    // 1. 对 provider 进行后置处理;
    // 2. 将 provider 设置到 builder 上
	@Override
	public void configure(B builder) throws Exception {
		provider = postProcess(provider);
		builder.authenticationProvider(provider);
	}

	/**
	 * Gets the UserDetailsService that is used with the
	 * DaoAuthenticationProvider
	 *
	 * @return the UserDetailsService that is used with the
	 * DaoAuthenticationProvider
	 */
	public U getUserDetailsService() {
		return userDetailsService;
	}
}

参考文章

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值