doublemc
doublemc

Reputation: 3311

Can't autowire Spring Security implementation class

I'm trying to make BasicAuth for my REST API and here is my problem.

AccountConfiguration

// Spring Security uses accounts from our database
@Configuration
public class AccountConfiguration extends GlobalAuthenticationConfigurerAdapter {

    private UserAuthService userAuthService;

    @Autowired
    public AccountConfiguration(UserAuthService userAuthService) {
        this.userAuthService = userAuthService;
    }

    @Override
    public void init(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userAuthService);
    }
}

In constructor IntelliJ tells me

Could not autowire. No beans of "UserAuthService type found

but I have that bean in the same package, here it is:

@Service
@Transactional
public class UserAuthService implements UserDetailsService {

    private UserRepository userRepository;

    @Autowired
    public UserAuthService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userRepository.findByUsername(username);

        if (user == null) {
            throw new UsernameNotFoundException("Could not find the user: " + username);
        }

        return new org.springframework.security.core.userdetails.User(
                user.getUsername(),
                user.getPassword(),
                true,
                true,
                true,
                true,
                AuthorityUtils.createAuthorityList("USER"));
    }
}

Here is my 3rd configuration file for Spring Security:

@EnableWebSecurity
@Configuration
public class WebConfiguration extends WebSecurityConfigurerAdapter{

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // allow everyone to register an account; /console is just for testing
        http
            .authorizeRequests()
                .antMatchers("/register", "/console/**").permitAll();

        http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated();

        // making H2 console working
        http
            .headers()
                .frameOptions().disable();

        /*
        https://docs.spring.io/spring-security/site/docs/current/reference/html/csrf.html#when-to-use-csrf-protection
        for non-browser APIs there is no need to use csrf protection
        */
        http
            .csrf().disable();
    }
}

So how can I fix that? What's the problem here? Why it can't autowire UserAuthService?

Upvotes: 0

Views: 520

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Try changing code to inject the interface, rather than the implementation. It's a transactional proxy.

private UserDetailsService userAuthService;

@Autowired
public AccountConfiguration(UserDetailsService userAuthService) {
    this.userAuthService = userAuthService;
}

Spring Autowiring class vs. interface?

Upvotes: 2

Related Questions