春天用mongodb登录

时间:2014-10-30 11:57:48

标签: java spring mongodb login thymeleaf

我将 spring mongodb thymeleaf 一起使用。我的问题是我不知道如何将登录查询与我的用户数据库连接起来。我已经检查了密码(使用散列),但我只能查询在

中初始化的用户
@Override
public void init (AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication()
        .withUser("user").password("password").roles("USER");
}

方法。有人能帮助我吗?

2 个答案:

答案 0 :(得分:1)

可能最简单也是最常见的方法是实现自己的UserDetailsService,它将有loadUserByUsername()方法从MongoDB获取UserDetails对象。

Here是一个很好的教程,基于XML配置。您可能还想查看有关AuthenticationProviders工作方式的Spring Security docs

答案 1 :(得分:1)

最后我拥有它!这篇guide和@helmy的帖子很有帮助。额外的一点是写

@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(mongoSecurityService);
}

如果您不使用.xml配置并添加CustomMongoService。谢谢!

编辑:

您应该在项目文件夹中拥有类WebSecurityConfiguration extends 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");
    }

}

希望有所帮助。

相关问题