Skip to content
Advertisement

Add database authentication to Spring Data Rest application

I’m creating an application using Spring Data REST with Thymeleaf.

Initially I created my models, controllers, dao and services. All worked fine. I’m now trying to add security to my application. Right now I’m just focused on the login/logout.

I’ve been able to create an in memory authentication as below:

JavaScript

I want to change this to database authentication though. I’m pretty sure I can create a jdbc connection and change my config method to something like this:

JavaScript

My problem is that I’m already accessing the database through my DAO interfaces. E.g:

JavaScript

My users table has an email and password column which will be used as the username/password.

Is it possible to also authenticate by using this in some way? I can provide additional information but am reluctant to just post everything and hope somebody will write it for me.

Advertisement

Answer

Since you’ve already created the DAO interfaces, it may be easier to create a UserDetailsService implementation:

JavaScript

and wire it in with:

JavaScript

For some additional clarity, here is the complete context of my implementation:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement