Java 使用mongodb进行Spring登录
我将spring与mongodb和thymeleaf一起使用。我的问题是,我不知道如何将登录查询与我的用户数据库连接起来。我已经检查了密码(使用哈希),但我只能查询在中初始化的用户Java 使用mongodb进行Spring登录,java,spring,mongodb,login,thymeleaf,Java,Spring,Mongodb,Login,Thymeleaf,我将spring与mongodb和thymeleaf一起使用。我的问题是,我不知道如何将登录查询与我的用户数据库连接起来。我已经检查了密码(使用哈希),但我只能查询在中初始化的用户 @Override public void init (AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .withUser("user").password("passwor
@Override
public void init (AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
方法。有人能帮我吗?最简单、最常见的方法可能是实现您自己的
UserDetails服务
,它将有一个loadUserByUsername()
方法,该方法将从MongoDB获取UserDetails对象
是一个很好的教程,基于XML配置。您可能还想查看有关AuthenticationProviders如何工作的信息。终于有了!这和@helmy的帖子是一个很好的帮助。额外的一点是写作
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(mongoSecurityService);
}
如果不使用.xml配置并添加CustomMongoService
。谢谢
编辑:
在项目文件夹中应该有类WebSecurityConfiguration extensed-WebSecurityConfigurerAdapter
。在这节课上写下:
@Configuration
protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Autowired
public CustomMongoSecurityService mongoSecurityService;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(mongoSecurityService).and()
.inMemoryAuthentication()
.withUser("user").password("password").roles("USER").and()
.withUser("admin").password("1234").roles("ADMIN");
}
}
希望有帮助。好的,我会试试。谢谢但不幸的是,我仍然不知道应该将.xml放在我的项目中的什么位置。我没有任何.jsp文件,有关于thymeleaf+spring+mongodb的教程吗?我应该把这个init放在哪里?