web-dev-qa-db-ja.com

見つからないタイプ「org.springframework.security.core.userdetails.UserDetailsS​​ervice」のBeanが必要でした

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.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);
}
} 
</ code>

MavenまたはGradleを使用しても同じ問題が発生します。すべての注釈とパッケージ名は必要なようです。

ありがとう! :)

ブルーノ

6
b.lopes

serDetailsS​​erviceのBeanを追加します

@Autowired
private UserDetailsService userDetailsService;

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

Service classに注釈を付ける

@Service

@Service("userDetailsService")
1
Deepak Kumar