我正在尝试在我用 SpringBoot 3 制作的小网站上实现一个登录系统,我正在观看一些 AmigosCode 视频并遵循您的教程。 当您创建 WebSecurityConfig 类时,它会扩展 WebSecurityConfigureradapter 类,该类在当前版本的 SpringBoot 中已弃用。 稍微调查一下,我发现了一些可能的解决方案,并且按照教程,我创建了一个 Bean,它为我带来了问题。 如果您知道如何解决它,或者以其他更好的方式进行登录,那将非常有帮助。我还没有找到太多关于使用 SpringBoot 3 登录系统的信息或教程。
@Bean
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(daoAuthenticationProvider());
}
这是豆子。
@Configuration
@AllArgsConstructor
@EnableWebSecurity
public class WebSecurityConfig {
private final AppUserService appUserService;
private final BCryptPasswordEncoder bCryptPasswordEncoder;
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception{
http
.authorizeHttpRequests((requests) -> requests
.requestMatchers("/", "/productos").permitAll()
.anyRequest().authenticated()
).formLogin((form) -> form
.loginPage("/login")
.permitAll()
)
.logout((logout) -> logout.permitAll());
return http.build();
}
@Bean
protected void configure(AuthenticationManagerBuilder auth) {
auth.authenticationProvider(daoAuthenticationProvider());
}
@Bean
public DaoAuthenticationProvider daoAuthenticationProvider() {
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
provider.setPasswordEncoder(bCryptPasswordEncoder);;
provider.setUserDetailsService(appUserService);
return provider;
}
}
这是班级。
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'configure' defined in class path resource [com/mishop/main/security/config/WebSecurityConfig.class]: Invalid factory method 'configure' on class [com.mishop.main.security.config.WebSecurityConfig]: needs to have a non-void return type!
这是个例外。