Problem z @Autowired w CustomUserDetailsService --- Spring + Spring Security + Tomcat

0

Witam,

W klasie CustomUserDetailsService implementującej interfejs UserDetailsService wstrzykuje setterem klasę UserService.
Korzystam z serwera Tomcat. Przy uruchamianiu wywala błąd:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.primesystems.clientsystem.user.UserServiceImpl]

Link do repo: https://github.com/GSwidnicki/pl.primesystems.clientsystem

Doszedłem do tego, że najpierw jest podejmowana próba utworzenia Bean'a CustomUserDetailsService, a następnie dopiero te z adnotacjami @Service, @Controller, @Repository.

Proszę o wyjaśnienie i pomoc.

1

A jesteś pewien że można już defaultowo wstrzykiwać przez Springa z użyciem cgliba? Bo próbujesz wstrzykiwać beana który ma założone AOP (w twoim przypadku @Transactional), czyli musi zostać utworzone dla niego proxy, a jednocześnie w miejscu wstrzyknięcia używasz konkretnej klasy a nie interfejsu, co oznacza że to proxy to musi być class-proxy.
Spróbuj zamiast wstrzykiwać UserServiceImpl wstrzyknąć UserService i to powinno załatwić sprawę. I generalnie zalecałbym wszędzie wstrzykiwać interfejs a nie konkretne klasy ;)

0

Niestety nawet po usunięciu adnotacji @Transactional oraz wstrzykiwaniu zarówno samego interfejsu nadal wyskakuje taki sam błąd.
Już 2 dzień się z tym męczę... Przeanalizowałem aplikację i klasę czy interfejs bez problemu wstrzykuję do @Controller'ów i wszystko działa.
Konfigurację też sprawdzałem i wygląda, że powinno być okej.
A przy wstrzykiwaniu do CustomUserDetailsService wywala błąd i nie widzi Bean'a UserService.

0

W klasie SecurityConfig
Zmień to:

     @Bean
    public UserDetailsService createCustomUserDetailsService() {
        return new CustomUserDetailsService();
}

Na to:

     @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(new CustomUserDetailsService());
}

Daj znać czy działa.

0

Niestety nic z tego, to też nie pomaga. Tak jak napisałem wcześniej: Doszedłem do tego, że najpierw jest podejmowana próba utworzenia Bean'a CustomUserDetailsService, a następnie dopiero te z adnotacjami @Service, @Controller, @Repository.

Dodając nad SpringConfig adnotację @ComponentScan to wtedy aplikacja startuje ale 2 razy wywołuje i buduje cały context a Hibernate wywala:
HHH000436: Entity manager factory name (default) is already registered.

0

Hmm.
To może przekaż te repository jako argument konstruktora?

 @Configuration
@EnableWebSecurity
public class SLBWebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    UserDao userDao;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                    .antMatchers("/add*" + "/save*"+ "/edit*"+ "/remove*"+ "/competitors*"+ "/seasons*" + "/userList")
                .hasRole("ADMIN")
                .antMatchers("/competitors*" + " /seasons* ").hasRole("USER")
                .and()
                .formLogin().loginPage("/login")
                .and().logout().logoutSuccessUrl("/").logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
                .permitAll();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(new DefaultSecurityUserService(userDao));
    }

}

Całość tutaj: https://github.com/bartlomiej-gora/slb-web/blob/master/src/main/java/pl/bgora/slb/config/SLBWebSecurityConfig.java

0

Niestety to także nic nie daje, bo wtedy wyskakuje błąd, że nie może znaleźć kolejnej warstwy powiązania.

0

A usunąłeś settera z @Autowired?

0

Zarówno z setterem, jak i bez niego nadal to samo: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [pl.primesystems.clientsystem.user.UserRepository]

0

A spróbuj wstrzyknąć do CustomUserDetailsService repozytoria zamiast tego UserService.

1

Trochę tam jest przekombinowane ale na szybko:

  • Z SecurityConfig usuń metodę tworzącą beana: createCustomUserDetailsService()
  • W klasie CustomUserDetailsService dodaj adnotacje @Service nad klasą i wstrzyknij UserServiceImpl przez konstruktor urzywając Autowired:
@Service
public class CustomUserDetailsService implements UserDetailsService {

    private UserServiceImpl userService;

    @Autowired
    public CustomUserDetailsService(UserServiceImpl userService) {
        this.userService = userService;
    }

Twój problem pojawił się, ponieważ w klasie SecurityConfig stworzyłeś beana za pomocą metody createCustomUserDetailsService w której utworzyłeś obiekt klasy CustomUserDetailsService. W ten sposób Spring nie wstrzyknie pól do tej klasy...

title d

1 użytkowników online, w tym zalogowanych: 0, gości: 1