UnsatisfiedDependencyException: Error creating bean with name 'securityConfig'
Пытаюсь втроить Spring Security в springMVC-приложение, но вылетает ошибка создания бина, хотя вроде все должно быть нормально.
Полный текст ошибки
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImp': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.spring.repository.UserRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Упоминаемые в ошибке классы: SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserServiceImp userService;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.formLogin()
// указываем страницу с формой логина
.loginPage("/login")
//указываем логику обработки при логине
.successHandler(new LoginSuccessHandler())
// указываем action с формы логина
.loginProcessingUrl("/login")
// Указываем параметры логина и пароля с формы логина
.usernameParameter("j_username")
.passwordParameter("j_password")
// даем доступ к форме логина всем
.permitAll();
http
.logout()
// разрешаем делать логаут всем
.permitAll()
// указываем URL логаута
.logoutRequestMatcher(new AntPathRequestMatcher("/logout", "POST"))
.invalidateHttpSession(true)
.clearAuthentication(true)
.deleteCookies("JSESSIONID")
.logoutSuccessUrl("/login")
//выклчаем кроссдоменную секьюрность (на этапе обучения неважна)
.and().csrf().disable();
http
// делаем страницу регистрации недоступной для авторизированных пользователей
.authorizeRequests()
//страницы аутентификаци доступна всем
.antMatchers("/login", "/").anonymous()
// защищенные URL
.antMatchers("/users/**").access("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
.antMatchers("/user/**").access("hasAnyRole('ROLE_ADMIN', 'ROLE_USER')")
.antMatchers("/admin/**").access("hasAnyRole('ROLE_ADMIN')")
.anyRequest().authenticated();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Autowired
protected void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userService).passwordEncoder(bCryptPasswordEncoder());
}
}
UserService.java
public interface UserService {
Optional<User> getUserById(Long id);
void deleteUser(Long id);
void updateUser(User user);
List<User> listUsers();
void createUser(User user);
}
UserServiceImp.java
@Service
@Transactional
public class UserServiceImp implements UserDetailsService, UserService{
@Autowired
UserRepository userRepository;
@Autowired
RoleRepository roleRepository;
@Autowired
BCryptPasswordEncoder bCryptPasswordEncoder;
@Override
public Optional<User> getUserById(Long id) {
return userRepository.findById(id);
}
@Override
public void deleteUser(Long id) {
userRepository.deleteById(id);
}
@Override
public void updateUser(User user) {
userRepository.save(user);
}
@Override
public List<User> listUsers() {
return userRepository.findAll();
}
@Override
public void createUser(User user) {
user.setRoles(Collections.singleton(new UserRole(1L, "ROLE_USER")));
user.setPassword(bCryptPasswordEncoder.encode(user.getPassword()));
userRepository.save(user);
}
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User user = userRepository.findByUsername(username);
if (user == null) {
throw new UsernameNotFoundException("User not found");
}
return user;
}
}
UserRepository.java
public interface UserRepository extends JpaRepository<User, Long> {
User findByUsername(String username);
}
Ответы (1 шт):
Спасибо комментаторам, благодаря вам я смог найти за что зацепиться и в итоге решить вопрос. изменения, которые я внес в программу:
- Добавил аннотации @Repository к классу UserRepository
- Добавил аннотацию @EnableJpaRepositories("com.spring") класс конфигурации БД
- Изменил аннотацию @Bean в методе entityManager'а на @Bean(name = "entityManagerFactory")
- Изменил аннотацию @EnableTransactionManagement в конфиге БД на @EnableTransactionManagement(proxyTargetClass = true)