SecurityConfiguration 需要 BCryptPasswordEncoder 类型的 bean
SecurityConfiguration required bean of type BCryptPasswordEncoder
我试着查看了几个类似的问题,但是它们似乎使用了不同的 methods/class 结构
我也在使用 Lombok(不确定这是否是一个问题)
这是错误:
Parameter 1 of constructor in com.example.javaspringboot.Security.SecurityConfiguration required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.
它引用的class如下:
@Configuration @EnableWebSecurity @RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final UserDetailsService uds; // VERIFY LOGIN ATTEMPTS
private final BCryptPasswordEncoder bcpe; // ENCODING PASSWORDS
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(uds).passwordEncoder(bcpe);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// super.configure(http); //USES COOKIES LOOK MORE INTO THIS
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().anyRequest().permitAll();
http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception{ // NO IDEA
return super.authenticationManagerBean();
}
}
它正在使用的唯一其他 class 是在主应用程序 class 中,如图所示:
@SpringBootApplication
public class JavaSpringBootApplication {
// this runs on init
public static void main(String[] args) {
SpringApplication.run(JavaSpringBootApplication.class, args);
}
@Bean
PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
return new BCryptPasswordEncoder();
}
};
}
}
考虑到它指定的 class 已经概述了 @Bean,这有点令人困惑。
如有任何建议,我们将不胜感激。
在您的主 class 中,return 类型错误(或者您需要更改安全配置中的 class)。
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
private final BCryptPasswordEncoder bcpe;
...
}
需要 BCryptPasswordEncoder
。不过
public class JavaSpringBootApplication {
@Bean
PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
return new BCryptPasswordEncoder();
}
}
return PasswordEncoder
.
因此,要么将您的 bean 的 return 类型更改为 BCryptPasswordEncoder
,要么在您的配置中自动装配 PasswordEncoder
。
我试着查看了几个类似的问题,但是它们似乎使用了不同的 methods/class 结构
我也在使用 Lombok(不确定这是否是一个问题)
这是错误:
Parameter 1 of constructor in com.example.javaspringboot.Security.SecurityConfiguration required a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' that could not be found.
Action:
Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration.
它引用的class如下:
@Configuration @EnableWebSecurity @RequiredArgsConstructor
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private final UserDetailsService uds; // VERIFY LOGIN ATTEMPTS
private final BCryptPasswordEncoder bcpe; // ENCODING PASSWORDS
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(uds).passwordEncoder(bcpe);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
// super.configure(http); //USES COOKIES LOOK MORE INTO THIS
http.csrf().disable();
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
http.authorizeRequests().anyRequest().permitAll();
http.addFilter(new CustomAuthenticationFilter(authenticationManagerBean()));
}
@Bean
@Override
public AuthenticationManager authenticationManagerBean() throws Exception{ // NO IDEA
return super.authenticationManagerBean();
}
}
它正在使用的唯一其他 class 是在主应用程序 class 中,如图所示:
@SpringBootApplication
public class JavaSpringBootApplication {
// this runs on init
public static void main(String[] args) {
SpringApplication.run(JavaSpringBootApplication.class, args);
}
@Bean
PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
return new BCryptPasswordEncoder();
}
};
}
}
考虑到它指定的 class 已经概述了 @Bean,这有点令人困惑。
如有任何建议,我们将不胜感激。
在您的主 class 中,return 类型错误(或者您需要更改安全配置中的 class)。
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
...
private final BCryptPasswordEncoder bcpe;
...
}
需要 BCryptPasswordEncoder
。不过
public class JavaSpringBootApplication {
@Bean
PasswordEncoder passwordEncoder(){ // NEEDED TO ALLOW PASSWORD ENCODER INSIDE SECURITY
return new BCryptPasswordEncoder();
}
}
return PasswordEncoder
.
因此,要么将您的 bean 的 return 类型更改为 BCryptPasswordEncoder
,要么在您的配置中自动装配 PasswordEncoder
。