Spring login with mongodb
I am using spring with mongodb and thymeleaf . My problem is that I don't know how I connect the login request with my custom database. I am already checking the password (with hashing), but I can only prompt users who initialize to
@Override
public void init (AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user").password("password").roles("USER");
}
method. Can anyone help me?
source to share
Finally, I have it! This guide and @helmy's post was good help. An additional point is the entry
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(mongoSecurityService);
}
if you are not using .xml config and add CustomMongoService
. Thank you!
EDIT:
You should have a class WebSecurityConfiguration extends WebSecurityConfigurerAdapter
in your project folder. In this class, write this:
@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");
}
}
Hope it helps.
source to share
Probably the easiest and most common approach is to implement your own UserDetailsService
, which will have a method loadUserByUsername()
that will receive a UserDetails object from MongoDB.
Here is a good tutorial based on XML configuration. You can also check Spring Security Docs for how AuthenticationProviders work.
source to share