Hi I develop a Spring Boot app (v1.5.18.BUILD-SNAPSHOT), but this fails when I try to start the app....
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 1 of constructor in com.eficacia.security.WebSecurity 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.
The problem is that I have the bean configured:
@Configuration
public class AppConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
In my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
WebSecurity:
@Configuration
@EnableWebSecurity
@ComponentScan({"com.eficacia.security"})
public class WebSecurity extends WebSecurityConfigurerAdapter {
public static final String USER_REGISTRATION_URL = "/v1/user";
private UserDetailsService userDetailsService;
private BCryptPasswordEncoder bCryptPasswordEncoder;
public WebSecurity(UserDetailsService userDetailsService, BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.cors().and().csrf().disable()
.authorizeRequests().antMatchers(HttpMethod.POST, USER_REGISTRATION_URL).permitAll()
.anyRequest().authenticated()
.and().addFilter(new JWTAuthenticationFilter(authenticationManager(), getApplicationContext()))
.addFilter(new JWTAuthorizationFilter(authenticationManager(), getApplicationContext()))
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS);
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Bean
CorsConfigurationSource corsConfigurationSource() {
CorsConfiguration configuration = new CorsConfiguration();
//configuration.setAllowedOrigins(Arrays.asList("http://localhost:4200","http://opusclick.com","https://gateway2.tucompra.com.co"));
configuration.addAllowedOrigin("*");
configuration.addAllowedHeader("*");
configuration.setAllowedMethods(Arrays.asList("GET","POST","PUT","PATCH","DELETE"));
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", configuration);
return source;
}
}
WebConfigurer:
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
private final ApplicationContext applicationContext;
private final EntityManager entityManager;
@Autowired
public WebMvcConfiguration(ApplicationContext applicationContext, EntityManager entityManager) {
this.applicationContext = applicationContext;
this.entityManager = entityManager;
}
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json().applicationContext(this.applicationContext).build();
argumentResolvers.add(new DTOModelMapper(objectMapper, entityManager));
}
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
JacksonMapperConfiguration jacksonMapperConfiguration= new JacksonMapperConfiguration();
converters.add(jacksonMapperConfiguration.mappingJackson2HttpMessageConverter());
super.configureMessageConverters(converters);
}
}
My question is very simple, and that is why the definition of the class 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder ' is not found if there really exists a configuration class for this.
Many Thanks!
Looks like your web configurer class isn't in the package com.eficacia.security
or one of its subpackages.
Move WebMvcConfiguration
in a place within the @ComponentScan
ed packages.
You are creating bean of type PasswordEncoder and try to autowire BCryptPasswordEncoder
@Configuration
public class AppConfiguration {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
}
Change this:
private BCryptPasswordEncoder bCryptPasswordEncoder;
public WebSecurity(UserDetailsService userDetailsService,
BCryptPasswordEncoder bCryptPasswordEncoder) {
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
to this : private PasswordEncoder passwordEncoder ;
public WebSecurity(UserDetailsService userDetailsService,
PasswordEncoder passwordEncoder ) {
this.userDetailsService = userDetailsService;
this.passwordEncoder = PasswordEncoder ;
}
Spring could not found the BcryptPasswordEncoder type as you are returning PasswordEncoder
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.