spring-security Java spring Boot 考虑在配置中定义UserService的Bean

e3bfsja2  于 2022-11-11  发布在  Spring
关注(0)|答案(1)|浏览(244)

我正在尝试创建我的第一个JavaSpring安全应用程序,但遇到以下错误:

Consider defining a bean of type 'de.JBR.mongo.models.UserService' in your configuration.

这是我的Sprinboot应用程序类:

@ComponentScan({ "de.JBR.mongo.repositry", "de.JBR.mongo.api.models", "security.config", "security"})
@EnableMongoRepositories("de.JBR.mongo")
@SpringBootApplication
@AllArgsConstructor 
public class EssensManagerApplication {

    private final UserRepositry repositry;

    public static void main(String[] args) {
        SpringApplication.run(EssensManagerApplication.class, args);

    }}

用户服务类:

@AllArgsConstructor
@Service
public class UserService implements UserDetailsService{

    private final static String USER_NOT_FOUND = "Nutzer mit Name %s nicht gefunden";
    private final UserRepositry repositry;

    @Override
    @Bean
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        return repositry.findByUsername(username)
                .orElseThrow(() -> new UsernameNotFoundException(String.format(USER_NOT_FOUND, username)));
    }

}
rqqzpn5f

rqqzpn5f1#

由于您添加了一个@ComponentScan,而de.JBR.mongo.models不在包列表中,因此Spring不会自动创建您的服务。因此,请在配置文件中创建一个@Bean,或者将该包添加到@ComponentScan列表中。

相关问题