使用Spring Security + Spring Data + MongoDB进行身份验证
2022-09-02 11:56:47
我想在MongoDB中使用Spring安全性(使用Spring数据),并从我自己的数据库中检索用户以实现spring安全性。但是,我不能这样做,因为我的用户服务类型似乎不受支持。
这是我的用户服务类:
public class UserService {
private ApplicationContext applicationContext;
private MongoOperations mongoOperations;
public UserService() {
applicationContext = new AnnotationConfigApplicationContext(MongoConfig.class);
mongoOperations = (MongoOperations) applicationContext.getBean("mongoTemplate");
}
public User find(String username) {
return mongoOperations.findOne(Query.query(Criteria.where("username").is(username)), User.class);
}
}
还有我的安全配置类:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
UserService userService;
@Autowired
public void configAuthBuilder(AuthenticationManagerBuilder builder) throws Exception {
builder.userDetailsService(userService); //THIS DOES NOT WORK
builder.inMemoryAuthentication().withUser("username").password("password").roles("USER");
}
}
我评论的那行说:
The inferred type UserService is not a valid substitute for the bounded parameter <T extends UserDetailsService>.
如何修复它,以便从我自己的数据库中检索用户?