当前位置: 首页 > 知识库问答 >
问题:

需要一个无法找到org.springframework.security.core.userdetails.UserDetailsService类型的bean

毛成济
2023-03-14

使用mvn spring boot启动时:run或甚至使用gradle返回该问题。

***************************
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

这里是主要的类,在我看来所有的需求都可以,我使用的是org.springframework.boot版本1.5.7

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();
    }
}

以及:

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);
    }
} 

使用Maven或Gradle会返回相同的问题。所有注释和包名称似乎都符合要求。

共有3个答案

帅锦
2023-03-14

在服务类中生成批注

@Service

@Service("userDetailsService")
常自怡
2023-03-14

我也遇到了这个错误。在我的例子中,我有一个类JwtUserDetailsService,我忘记了实现UserDetailsService。添加实现UserDetailsService后,错误消失。

请注意:如果您也有自己的UserDetailsService并且您使用Munna的答案,那么您得到的错误Stackoverflow Error表示您也犯了我的错误。

臧弘和
2023-03-14

为UserDetailsService添加bean

@Autowired
private UserDetailsService userDetailsService;

@Bean
public UserDetailsService userDetailsService() {
    return super.userDetailsService();
}
 类似资料: