【问题标题】:Unsatisfied dependency expressed through field 'userDetailsService'; NoSuchBeanDefinitionException: 'org.springframework.securityUserDetailsService'通过字段“userDetailsS​​ervice”表示的不满足的依赖关系; NoSuchBeanDefinitionException:'org.springframework.securityUserDetailsS​​ervice'
【发布时间】:2020-07-08 14:54:39
【问题描述】:

Spring 似乎无法自动装配UserDetailsService。我不明白为什么。

WebConfig.java

@Configuration
@ComponentScan("testproject")
@EnableWebMvc
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "testproject",
        entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
 public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void configureDefaultServletHandling(
                DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".html");


        return bean;
    }

    @Bean
    public UserDetailsService userDetailsService()  {
        UserDetailsService userDetailsService =
                new UserDetailsServiceImpl();
        return userDetailsService;
    }
}

MyAppInitializer.java

public class MyAppInitializer extends
                AbstractAnnotationConfigDispatcherServletInitializer {
    @Override
    public void onStartup(final ServletContext sc) throws ServletException {
        System.out.println("onStartup!");

        AnnotationConfigWebApplicationContext root =
                new AnnotationConfigWebApplicationContext();

        root.register(WebConfig.class);
        root.setServletContext(sc);

        root.scan("testproject");
        sc.addListener(new ContextLoaderListener(root));

        ServletRegistration.Dynamic appServlet =
                sc.addServlet("dispatcher", new DispatcherServlet(new GenericWebApplicationContext()));
        appServlet.setLoadOnStartup(1);
        appServlet.addMapping("/");
    }

        @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {SecurityConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[]{WebConfig.class};
    }

    @Override
    protected String[] getServletMappings() {
        return new String[]{"/"};
    }
}

SecurityConfig.java

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter  {
    @Autowired
    private UserDetailsService userDetailsService;
    @Autowired
    private JwtRequestFilter jwtRequestFilter;
    @Autowired
    private JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;


    @Bean
    DaoAuthenticationProvider authenticationProvider(){
        DaoAuthenticationProvider daoAuthenticationProvider =
                new DaoAuthenticationProvider();
        daoAuthenticationProvider.setPasswordEncoder(passwordEncoder());
        daoAuthenticationProvider.setUserDetailsService(this.userDetailsService);
        return daoAuthenticationProvider;
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring();
    }
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    @Override
    @Autowired
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
                .passwordEncoder(passwordEncoder());
    }

    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {
        httpSecurity.csrf().disable()
                // dont authenticate this particular request
              //  .authorizeRequests().antMatchers("/login").permitAll()
                // all other requests need to be authenticated
                .authorizeRequests().anyRequest().authenticated().and()
                .exceptionHandling().authenticationEntryPoint(jwtAuthenticationEntryPoint).and().sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

        // Add a filter to validate the tokens with every request
        httpSecurity.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
    }


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

SecurityWebApplicationInitializer.java


public class SecurityWebApplicationInitializer extends
                    AbstractSecurityWebApplicationInitializer {
    public SecurityWebApplicationInitializer() {
        super(SecurityConfig.class);
    }
}

完全错误:

Error creating bean with name 'securityConfig': 
Unsatisfied dependency expressed through field 'userDetailsService'; nested exception is 
org.springframework.beans.factory.NoSuchBeanDefinitionException:
 No qualifying bean of type 
'org.springframework.security.core.userdetails.UserDetailsService' 
available: expected at least 1 bean which qualifies as autowire candidate. 
Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

感谢任何帮助!

编辑

UserDetailsS​​erviceImpl.java

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService  {
    @Autowired
    private UserRepository userRepository;

    @Override
    public UserDetails loadUserByUsername(String username)
            throws UsernameNotFoundException  {
        User user = userRepository.findByUsername(username);
        if (user != null) {
            return new UserDetailsImpl(user);
        } else {
            throw new UsernameNotFoundException("User not found.");
        }
    }
}

【问题讨论】:

  • 你真的在使用 Spring Boot,因为从你正在做的配置量来看你不是吗?
  • @M.Deinum 我正在使用 Spring MVC
  • 我刚刚删除了spring-boot 标签。
  • MyAppInitializer 中删除ContextLoaderListener 的引导,并将WebConfig 添加到SecurityWebApplicationInitializer,就像SecurityConfig 一样。两者都加载了ContextLoaderLIstener,但只剩下一个。
  • 我从 2003 年左右开始使用 Spring,参与并见证了不同项目的兴起。我基本上深入研究了代码(尤其是 Spring、Spring Boot 但 Security、Batch 也有一些小秘密)。使用它并阅读文档,我建议现在使用 Spring Boot。

标签: java spring spring-mvc spring-security


【解决方案1】:

你的@ComponentScan 应该指向类的包,我假设testproject 不是包名。

【讨论】:

  • testproject src/main/java文件夹中的根包
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-14
  • 2019-11-09
  • 2018-11-02
  • 2019-08-03
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
相关资源
最近更新 更多