0

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?

dur
  • 15,689
  • 25
  • 79
  • 125
doublemc
  • 3,021
  • 5
  • 34
  • 61
  • 1
    Apart from intellij intellisense error. What error do you get when you try to run the code ? Sometimes, intellij gets it wrong. – coding_idiot Jan 31 '17 at 22:25
  • I'm not getting any error but my whole authentication just doesn't work and I thought it's just because of that. I don't know if I can edit this post with more details about my authentication not workign or I should create a new one. – doublemc Jan 31 '17 at 22:29
  • 1
    I fixed my other issue and this seems like just IntelliJ bug, thank you. – doublemc Feb 01 '17 at 10:54

1 Answers1

2

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?

Community
  • 1
  • 1
coding_idiot
  • 13,526
  • 10
  • 65
  • 116
  • Still doesn't work, now just says "Could not autowire. No beans of "UserAuthService type found" – doublemc Jan 31 '17 at 23:42
  • 1
    It's `UserDetailsService` not `UserAuthService`. Make sure you made the right change (2 changes). Also, post the error stacktrace for a better understanding. – coding_idiot Feb 01 '17 at 00:06