required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found
Asked Answered
P

5

12

When launching with mvn spring-boot:run or even with gradle returns that issue.

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userDetailsService in webroot.websrv.auth.config.WebSecurityConfiguration required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.


BUILD SUCCESSFUL

Total time: 19.013 secs

Here are the main classes, all the requirements looks ok to me, I am using the org.springframework.boot release 1.5.7.RELEASE

package webroot.websrv.auth.config;

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private JwtAuthenticationEntryPoint unauthorizedHandler;

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
        authenticationManagerBuilder
                .userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationTokenFilter();
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity
                .csrf().disable()

                .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .antMatchers(
                        HttpMethod.GET,
                        "/",
                        "/**/*.html",
                        "/**/*.{png,jpg,jpeg,svg.ico}",
                        "/**/*.css",
                        "/**/*.js"
                ).permitAll()
                .antMatchers("/api/auth/**").permitAll()
                .anyRequest().authenticated();

        httpSecurity
                .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);

        httpSecurity.headers().cacheControl();
    }
}

and:

package webroot.websrv;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class WebcliApplication {

    public static void main(String[] args) {
        SpringApplication.run(WebcliApplication.class, args);
    }
} 

Using Maven or Gradle it returns the same issue. All annotations and packages names seems to be as required.

Phenacite answered 19/9, 2017 at 10:15 Comment(0)
O
31

Add a bean for UserDetailsService

@Autowired
private UserDetailsService userDetailsService;

@Bean
public UserDetailsService userDetailsService() {
    return super.userDetailsService();
}
Omnibus answered 19/9, 2017 at 11:2 Comment(0)
L
3

I also come accross this error. In my case, I have a class JwtUserDetailsService and I have forget implement UserDetailsService. After adding implements UserDetailsService the error was disappered.

Note that: if you also have own UserDetailsService and you use Munna's answer, than you got error StackoverflowError it mean you also repited my mistake.

Lepper answered 5/6, 2020 at 5:43 Comment(0)
G
2

In Service class make annotation

@Service

to

@Service("userDetailsService")
Glacialist answered 20/1, 2018 at 14:44 Comment(0)
C
1

Add @Autowired and @Bean Annotations for UserDetailsService in SecurityConfig class

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Bean
    public UserDetailsService userDetailsService() {
        return super.userDetailsService();
    }
}

and

To allow circular references in application.properties

spring.main.allow-circular-references:true
Coltun answered 1/11, 2023 at 8:48 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Longboat
L
0

I also got the same error. I fixed it by creating a bean that returns UserDetailsService.

@Configuration
@RequiredArgsConstructor
public class ApplicationConfig {

    @Bean
    public UserDetailsService userDetailsService() {
        return username -> userRepository.findByEmail(username).orElseThrow(() -> new UsernameNotFoundException(username));
    }
}
Linker answered 24/5, 2024 at 4:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.