spring-security 不使用Web安全配置器适配器的Java身份验证管理器构建器

4si2a6ki  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(106)

我现在想要实现类似于下面的内容:

@Resource
    private UserDetailsService userDetailsService;

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(userDetailsService);
        authProvider.setPasswordEncoder(passwordEncoder());
        return authProvider;
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }

但在中我们将重点介绍authProvider()方法和configure()
根据最近的新闻,WebSecurityConfigurerAdapter被弃用了。在研究了一下之后,我发现了一件糟糕的事情:

@Configuration
@EnableWebSecurity
public class SpringSecurity {
    @Resource
    private UsersService usersService;

    @Bean
    public Argon2PasswordEncoder passwordEncoder() {
        return new Argon2PasswordEncoder();
    }

    @Bean
    public DaoAuthenticationProvider authProvider() {
        DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider();
        authProvider.setUserDetailsService(usersService);
        authProvider.setPasswordEncoder(passwordEncoder());
        return authProvider;
    }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.authenticationProvider(authProvider());
    }
.......

所以我也不得不做

@EnableAsync(proxyTargetClass = true)
@EnableCaching(proxyTargetClass = true)

这解决了另一个问题。但是继续,我现在得到了这个错误。

org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
...
Caused by: org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
...
Caused by: org.springframework.beans.factory.BeanCurrentlyInCreationException: Error creating bean with name 'springSecurity': Requested bean is currently in creation: Is there an unresolvable circular reference?

这也是我的用户服务

@Service
public class UsersService implements UserDetailsService {
    @Autowired
    private UsersRepository usersRepository;
...

你们介意帮我解决这个问题吗?谢谢:)
我还想提一下我还没有在网上找到答案

8nuwlpux

8nuwlpux1#

This question讨论了WebSecurityConfigurerAdapter现在已被弃用的事实-同时还使用了UserDetailsService。
也许能帮上忙。

相关问题