Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Connecting Spring Security to user data can be accomplished through several means. In this workshop, we'll take the approach of implementing a UserDetailsService
, which will supply Spring Security with authentication and authorization data.
Git Command to Sync Your Code to the Start of this Video
git checkout -f v4
Using Github With This Course
You can complete this course entirely using code created by you on your local machine. However, if you choose to use the code I've made available to you, you have two options:
- Use the project files linked at the bottom of this page, or
- Use the Github repository I've made available (recommended)
If you choose the recommended option of using the Github repository, it can be found at
https://github.com/treehouse/todotoday
To utilize Github with this course, you can download the Github desktop client for your system or use the command line interface provided with Git.
Clone this repository to your machine using the Github desktop client, or using the following command:
git clone git@github.com:treehouse/todotoday.git
To update your local repository to match a video's starting point, you can use the git checkout
command in combination with the stage and video number. For example, to update your local repository to match the starting point of Video 4, you'd use the following:
git checkout -f v4
Notice the use of the -f option. This forces Git to override all local changes, so be aware: this will cause any changes you made to be lost.
-
0:00
Our next order of business is to integrate spring security with our user and
-
0:04
role entities.
-
0:05
In general what we'll need to do is to implement the user details service
-
0:08
interface.
-
0:09
If we open this interface in the spring docs when we go there now,
-
0:15
we open this interface we'll see by scrolling down,
-
0:18
that there is exactly one method to implement and it's this load ByUsername.
-
0:24
Method. It locates the user based on
-
0:27
the user name.
-
0:29
So when we implement this method we'll call upon our user DAO which we
-
0:33
haven't written yet to grab the user entity from the database via hibernate and
-
0:38
use it as the methods return value.
-
0:40
Let's start coding our service in dao now.
-
0:42
I'll switch back to IntelliJ and first I'll create the user service interface.
-
0:48
So in my service package I will create a new, I'll choose Java class,
-
0:54
user service and all select interface.
-
0:57
There we go, and I'll make this service,
-
1:00
extend user, details, service.
-
1:06
So that my implementation class will need to include that load by user name method.
-
1:12
And the one additional method that all include here is a find by user name
-
1:15
method, which is a method that's completely for our application purposes
-
1:20
and that doesn't have anything To do with spring security so let me add that now.
-
1:24
I'll say, findByUsername(String username).
-
1:30
And let's make sure to import the right user here.
-
1:34
Now, this is our users so if you choose one of the ones up top,
-
1:38
you're gonna be importing the wrong user.
-
1:41
It's the user that comes from com.teamtree house.
-
1:44
Right there, cool.
-
1:47
And that's it for our interface.
-
1:48
Let's now create the implementation.
-
1:50
So again in the service package I'll right-click New Java Class and
-
1:54
I'll call it UserServiceImpl.
-
1:59
And let me include that service annotation here.
-
2:03
As well as indicate that it should be implementing the user service.
-
2:11
And in doing so, we'll need to add all unimplemented methods.
-
2:19
There should be two, one that comes from that user detail service that
-
2:24
the user service extends, and one that comes from user service that
-
2:28
we explicitly coded in the user service interface.
-
2:33
Now before we dive into this class we'll first need a way to grab
-
2:36
user data from the database.
-
2:38
Of course this will require a da o.
-
2:40
for the user entity.
-
2:41
Let's go ahead and create that interface in the dao package right click.
-
2:47
New job a class I'll call it user dao and
-
2:50
I want this to be an interface there it is right there.
-
2:54
The cool thing here is how little work we have to do.
-
2:57
If you recall, we are using spring data JPA.
-
3:00
So all we have to do here is extend CrudRepository.
-
3:06
And when you extend CrudRepository,
-
3:09
you tell it the kind of entity that you're after in the database.
-
3:12
As well as the class used for the identifier again.
-
3:17
Make sure you are importing the correct
-
3:19
User com.teamtreehouse.todotoday.model.User.
-
3:23
And after this we intelligently code our query methods.
-
3:29
In our case it will suffice to include a findByUsername method.
-
3:35
So let's do that now here's how that looks.
-
3:38
I want it to return a single user and
-
3:40
will say findByUsername(String username).
-
3:47
And finally let's apply the repository annotation to this interface.
-
3:55
And this is to make sure spring picks this up as a DAO and
-
3:59
now when we boot the app spring data will generate the implementation of the DAO so
-
4:03
we don't have to code it that is.
-
4:05
It will see a method name FindByUsername.
-
4:09
And it includes a string parameter so
-
4:11
it will generate a method that will find a user in the database
-
4:16
that has a user name column that uses the method name right here.
-
4:21
It has a user name column equal to whatever value was passed in so
-
4:27
cool okay back to the user service amperes class.
-
4:33
We can now auto wire a user dao into our service which will
-
4:37
need to start combining our user database data that is users and rows So
-
4:41
that we can integrate it with spring security.
-
4:44
So lets auto wire that up top.
-
4:47
Auto wired, private, user DAO, user DAO.
-
4:52
Cool.
-
4:54
Let me make sure to import that class.
-
4:58
There we go.
-
4:58
User DAO is imported.
-
5:01
The find my username method will be fairly quick here since it only
-
5:04
involves calling upon the DAOs method of the same name.
-
5:08
So let's return whatever value.
-
5:10
The DAO returns.
-
5:11
By calling its method of.
-
5:13
The same name.
-
5:17
As for the load by user name method this one will involve barely more work
-
5:22
Here's what will need to do.
-
5:24
I'm gonna add some comments in here.
-
5:28
First we'll need to load the user from the database.
-
5:31
And we'll throw an exception if not found.
-
5:39
And then we'll return the user object.
-
5:45
So really, we're just doing one extra thing here.
-
5:49
And that is that we are throwing an exception if that specific username
-
5:53
is not found.
-
5:54
And that is expected by the load by user name method which, again, comes from that
-
5:59
user details service interface, which our user interface extended.
-
6:05
Let me go back and remind you of that.
-
6:07
So the load by user name method comes from the user details service interface.
-
6:15
Okay, so back to these comments.
-
6:18
For loading the user from the database we use the same approach we did
-
6:22
in the findByUsername method.
-
6:23
That is will use that userDao so let's create a user object.
-
6:27
And call upon the DAO to find the user by the username and
-
6:32
we'll pass in the username that we received here in this method.
-
6:37
Now, because this method should throw the username not found exception,
-
6:43
which will be detected by Spring Security,
-
6:46
if upon authenticating, a user is not found with the provided username.
-
6:51
Then we should do that now if the value that stored in user at this point
-
6:56
happens to be no.
-
6:57
So will say if the user is null then we'll throw that exception.
-
7:06
UsernameNotFoundException.
-
7:09
And will throw a message in there too.
-
7:11
User not found.
-
7:15
Cool, and let's not forget to return that user object instead of returning null.
-
7:22
All finished.
-
7:24
So we finished our service and DAO layers.
-
7:27
Next we'll add some application level configuration to our Spring app
-
7:31
that switches on all of our Spring security functionality.
You need to sign up for Treehouse in order to download course files.
Sign up