Good day. Trying to figure out Spring Security. Such Exception jumps out:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerDetailsServiceImpl': Injection of autowired dependencies failed; nested exception is org.

AppConfig:

@Configuration @PropertySource("classpath:config.properties") @EnableTransactionManagement @EnableWebMvc public class AppConfig extends WebMvcConfigurerAdapter { @Bean public UserDetailsService userDetailsService(){ return new CustomerDetailsServiceImpl(); } } 

SecurityConfig:

 @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private UserDetailsService service; @Autowired public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth .userDetailsService(service) .passwordEncoder(getShaPasswordEncoder()); } @Override protected void configure(HttpSecurity http) throws Exception { http .csrf().disable() .authorizeRequests() .anyRequest().authenticated() .and() .exceptionHandling().accessDeniedPage("/unauthorized") .and() .formLogin() .loginPage("/login") .loginProcessingUrl("/j_spring_security_check") .failureUrl("/login?error") .usernameParameter("j_login") .passwordParameter("j_password") .permitAll() .and() .logout() .permitAll() .logoutUrl("/logout") .logoutSuccessUrl("/login?logout") .invalidateHttpSession(true); } @Bean public ShaPasswordEncoder getShaPasswordEncoder(){ return new ShaPasswordEncoder(); } } 

CustomerDetailsServiceImpl:

 @Service public class CustomerDetailsServiceImpl implements UserDetailsService { @Autowired private CustomerServiceImplement service; @Override public UserDetails loadUserByUsername(String var1) throws UsernameNotFoundException { Customer customer = service.getCustomerByLogin(var1); System.out.println("UserDetailsImpl "+customer.getLogin()+" "+customer.getPassword()+" "+customer.getRole()); if (customer == null) throw new UsernameNotFoundException(var1 + " not found"); Set<GrantedAuthority> roles = new HashSet<>(); roles.add(new SimpleGrantedAuthority(customer.getRole().name())); return new User(customer.getLogin(), customer.getPassword(), roles); } } 

    1 answer 1

    According to the error message, Spring cannot inject a bean into the CustomerDetailsServiceImpl.service field. Do you have a bin of the type CustomerServiceImplement in the application context? It is necessary either to add, or, if there is, give the code of its announcement, so that you can further understand.

    • I have already figured out ... Thank you for your concern) - Nikolay Egorov