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

7vhp5slm  于 7个月前  发布在  Spring
关注(0)|答案(4)|浏览(71)

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


以及:

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返回相同的问题。所有注解和包名称似乎都是必需的。

7gs2gvoe

7gs2gvoe1#

UserDetailsService添加bean

@Autowired
private UserDetailsService userDetailsService;

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

字符串

628mspwn

628mspwn2#

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

b1payxdu

b1payxdu3#

Service class中做注解

@Service

字符串

@Service("userDetailsService")

7nbnzgx9

7nbnzgx94#

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

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

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

字符串

在application.properties中允许循环引用

spring.main.allow-circular-references:true

相关问题