这是错误消息:
org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“securityConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?
这是SecurityConfig.java文件:
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
import org.springframework.beans.factory.annotation.Autowired;
import com.example.demo.service.UserService;
@Configuration
public class SecurityConfig {
@Autowired
private UserService userService;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService)
.passwordEncoder(bCryptPasswordEncoder());
}
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.requestMatchers(new AntPathRequestMatcher("/register")).permitAll()
.requestMatchers(new AntPathRequestMatcher("/admin/**")).hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.formLogin().loginPage("/login").permitAll()
.and()
.logout().permitAll();
return http.build();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
当我运行应用程序时
@SpringBootApplication(exclude = { SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class })
这不允许检查引用,我收到另一个错误:
Parameter 0 of method configureGlobal in com.example.demo.config.SecurityConfig required a bean of type 'org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder' in your configuration.
请指导一下,我是 Spring Boot 和自学的新手。
您不需要排除 SecurityAutoConfiguration,只需将静态修饰符添加到 bCryptPasswordEncoder 方法