无法找到所需的类型为“org.springframework.security.core.userdetails.UserDetailsService”的Bean

7vhp5slm  于 2023-11-17  发布在  Spring
关注(0)|答案(4)|浏览(191)

当使用mvn spring-boot:run或gradle启动时,会返回该问题。

  1. ***************************
  2. APPLICATION FAILED TO START
  3. ***************************
  4. Description:
  5. Field userDetailsService in webroot.websrv.auth.config.WebSecurityConfiguration required a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' that could not be found.
  6. Action:
  7. Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetailsService' in your configuration.
  8. BUILD SUCCESSFUL
  9. Total time: 19.013 secs

字符串
这里是主要的类,所有的要求看起来都很好,我使用的是org.springframework. Boot release 1.5.7.RELEASE

  1. package webroot.websrv.auth.config;
  2. @Configuration
  3. @EnableWebSecurity
  4. @EnableGlobalMethodSecurity(prePostEnabled = true)
  5. public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
  6. @Autowired
  7. private JwtAuthenticationEntryPoint unauthorizedHandler;
  8. @Autowired
  9. private UserDetailsService userDetailsService;
  10. @Autowired
  11. public void configureAuthentication(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {
  12. authenticationManagerBuilder
  13. .userDetailsService(userDetailsService)
  14. .passwordEncoder(passwordEncoder());
  15. }
  16. @Bean
  17. public PasswordEncoder passwordEncoder() {
  18. return new BCryptPasswordEncoder();
  19. }
  20. @Bean
  21. public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception {
  22. return new JwtAuthenticationTokenFilter();
  23. }
  24. @Override
  25. protected void configure(HttpSecurity httpSecurity) throws Exception {
  26. httpSecurity
  27. .csrf().disable()
  28. .exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
  29. .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
  30. .authorizeRequests()
  31. .antMatchers(
  32. HttpMethod.GET,
  33. "/",
  34. "/**/*.html",
  35. "/**/*.{png,jpg,jpeg,svg.ico}",
  36. "/**/*.css",
  37. "/**/*.js"
  38. ).permitAll()
  39. .antMatchers("/api/auth/**").permitAll()
  40. .anyRequest().authenticated();
  41. httpSecurity
  42. .addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class);
  43. httpSecurity.headers().cacheControl();
  44. }
  45. }


以及:

  1. package webroot.websrv;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class WebcliApplication {
  6. public static void main(String[] args) {
  7. SpringApplication.run(WebcliApplication.class, args);
  8. }
  9. }


使用Maven或Gradle返回相同的问题。所有注解和包名称似乎都是必需的。

7gs2gvoe

7gs2gvoe1#

UserDetailsService添加bean

  1. @Autowired
  2. private UserDetailsService userDetailsService;
  3. @Bean
  4. public UserDetailsService userDetailsService() {
  5. return super.userDetailsService();
  6. }

字符串

628mspwn

628mspwn2#

我也遇到了这个错误。在我的例子中,我有一个类JwtUserDetailsService,我忘记了实现UserDetailsService。在添加implements UserDetailsService后,错误消失了。
注意:如果你也有自己的UserDetailsService和你使用Munna的答案,比你得到错误StackoverflowError这意味着你也重复我的错误。

b1payxdu

b1payxdu3#

Service class中做注解

  1. @Service

字符串

  1. @Service("userDetailsService")

7nbnzgx9

7nbnzgx94#

在SecurityConfig类中为UserDetailsService添加@Autowired和@Bean注解

  1. @Configuration
  2. @EnableWebSecurity
  3. public class SecurityConfig extends WebSecurityConfigurerAdapter {
  4. @Autowired
  5. private UserDetailsService userDetailsService;
  6. @Bean
  7. public UserDetailsService userDetailsService() {
  8. return super.userDetailsService();
  9. }
  10. }

字符串

在application.properties中允许循环引用

  1. spring.main.allow-circular-references:true

展开查看全部

相关问题