0

Добрый день. Пытаюсь разобраться с Spring Security. Выскакивает такой Exception:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerDetailsServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private ua.kiev.izodrom.egorov.implementServices.CustomerServiceImplement ua.kiev.izodrom.egorov.detailServices.CustomerDetailsServiceImpl.service;

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 ответ 1

1

Согласно сообщению об ошибке, Spring не может заинжектить бин в поле CustomerDetailsServiceImpl.service. У вас в application context есть бин типа CustomerServiceImplement? Надо либо добавить, либо, если есть, привести код его объявления, чтобы можно было дальше разбираться.

1
  • Я уже разобрался...Спасибо за беспокойство) 17 июл 2016 в 20:09

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.