概述
介绍
AbstractDaoAuthenticationConfigurer
是Spring Security Config
提供的一个安全配置器抽象基类,它继承自UserDetailsAwareConfigurer
,而UserDetailsAwareConfigurer
又继承自SecurityConfigurerAdapter
,实现了接口SecurityConfigurer
。除了来自基类和所实现接口定义的能力,AbstractDaoAuthenticationConfigurer
自身又为一个安全配置器进行了如下定义:
- 所要创建的
AuthenticationProvider
是一个DaoAuthenticationProvider
; - 提供使用者设定目标
DaoAuthenticationProvider
属性userDetailsService/userDetailsPasswordService
的功能; - 提供使用者设定目标
DaoAuthenticationProvider
属性passwordEncoder
的功能; - 提供使用者设定配置过程中安全对象后置处理器的功能;
作为一个SecurityConfigurer
,AbstractDaoAuthenticationConfigurer
的安全构建器初始化方法为空,而配置方法流程是:
- 对目标
AuthenticationProvider DaoAuthenticationProvider
执行后置处理; - 将目标
AuthenticationProvider DaoAuthenticationProvider
设置到目标安全构建器;
继承关系
使用
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;
}
}