You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中WebSecurityConfigurerAdapter弃用后如何获取可注入的AuthenticationManager

Spring Security 弃用WebSecurityConfigurerAdapter后获取AuthenticationManager的方案

关于自定义SHA256加盐密码编码器

你自己实现的SHA256加盐哈希密码编码器,只要实现Spring Security的PasswordEncoder接口,重写encode和matches方法即可。注意编码时要生成随机盐并和哈希值一起存储,匹配时从已加密密码中提取盐,再用相同逻辑验证原密码的哈希值是否一致。

获取可注入的AuthenticationManager

WebSecurityConfigurerAdapter弃用后,有两种简单方式获取可直接注入自定义过滤器的AuthenticationManager:

方式一:通过AuthenticationConfiguration暴露Bean

直接在配置类中通过AuthenticationConfiguration获取并暴露AuthenticationManager Bean,这样就能在任意组件中注入使用:

@Configuration
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            // 根据需求配置授权规则、CSRF、会话管理等
            .authorizeHttpRequests(auth -> auth
                .anyRequest().authenticated()
            );
        return http.build();
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        return authConfig.getAuthenticationManager();
    }
}

方式二:结合自定义AuthenticationProvider配置

如果需要结合你自己的密码编码器定制认证逻辑,可以先配置AuthenticationProvider,再通过同样的方式暴露AuthenticationManager:

@Configuration
public class SecurityConfig {

    private final CustomSha256PasswordEncoder customPasswordEncoder;

    public SecurityConfig(CustomSha256PasswordEncoder customPasswordEncoder) {
        this.customPasswordEncoder = customPasswordEncoder;
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService()); // 替换为你的UserDetailsService实现
        authProvider.setPasswordEncoder(customPasswordEncoder);
        return authProvider;
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        return authConfig.getAuthenticationManager();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        // 实现从数据库或其他数据源获取用户信息的逻辑
        return username -> {
            // 示例:替换为实际的用户查询逻辑
            User dbUser = userRepository.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException("用户不存在"));
            return User.withUsername(dbUser.getUsername())
                .password(dbUser.getEncodedPassword())
                .roles(dbUser.getRoles().toArray(new String[0]))
                .build();
        };
    }
}

注意事项

  • 不需要手动实例化AuthenticationManager,通过AuthenticationConfiguration获取的是Spring Security默认构建的实例,只要配置了正确的UserDetailsService和PasswordEncoder,就能正常处理认证请求。
  • 在自定义过滤器中注入AuthenticationManager后,直接调用authenticationManager.authenticate(authenticationToken)即可执行认证逻辑,和之前使用WebSecurityConfigurerAdapter时的用法一致。

内容的提问来源于stack exchange,提问作者DLarcom

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 07:48:27