在springboot304中Consider defining a bean of type orgspringframeworksecurityconfigannotationwebbuildersHttpSecurity in your configuration错误解决方法
这个错误是因为在Spring Boot的配置中,没有定义一个类型为org.springframework.security.config.annotation.web.builders.HttpSecurity的bean。
要解决这个问题,可以在配置类中添加一个方法来定义这个bean,例如:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/admin/**").hasRole("ADMIN")
.antMatchers("/user/**").hasRole("USER")
.antMatchers("/public/**").permitAll()
.and()
.formLogin()
.and()
.logout()
.and()
.csrf().disable();
}
@Bean
public HttpSecurity httpSecurity() throws Exception {
return super.http();
}
}
在这个例子中,我们通过添加一个名为httpSecurity()的方法来定义了一个HttpSecurity类型的bean,并在方法中调用了super.http()来配置HttpSecurity。这样就可以避免出现上述错误。
另外,需要确保在配置类上添加了@EnableWebSecurity注解,以启用Spring Security的Web安全功能。
原文地址: https://www.cveoy.top/t/topic/bqe9 著作权归作者所有。请勿转载和采集!