For this studio, you’ll be tasked with adding simple user authentication to your
techjobs
application. The steps to do this will match what you have already done
in coding-events
. You should refer back to the tutorial starting
here.
Fork and clone the starter code for TechJobs (Auth Edition).
The dependencies for the database connection and hashing function
are already added for you in build.gradle
. You will need to
do some work to ensure that the schema, user, and database password
match your own local MySQL setup.
Open application.properties
and view the first three statements:
# Database connection settings
spring.datasource.url=jdbc:mysql://localhost:3306/techjobs_auth
spring.datasource.username=techjobs_auth
spring.datasource.password=auth
You likely do not already have a schema named techjobs_auth
or
this combination of username and password so you must create them.
Tip
To create a new schema in your current connection, refer back to the instructions in SQL Part 1 Exercises.
To create a new user with permissions, refresh your memory in Setting up a Persistent Database - Video.
Note
We’ve greatly reduced the functionality of the app so you can focus
on the work to set up authentication. Running the application now
gives you a familiar-looking navbar with two menu options, Add Jobs and Logout.
You can add jobs right away and an astute observer of the starter code and
schema tables will notice that the fields on Job
are only strings, not
complex objects. Logout functionality is not yet implemented, but you’ll get there by the end of
this studio.
User
model identical to that in coding-events
. The class needs:User
password field.BCryptPasswordEncoder
variable.UserRepository
.Tip
At this point, re-starting your application will not change the view
at localhost:8080
, but you can confirm you have done everything correctly if you see a user
table in MySQL Workbench.
AuthenticationController
.UserRepository
.dto
under models
.GET
handler in AuthenticationController
to display a registration form.POST
handler in AuthenticationController
to process the form.POST
handler for the login form will
have some different checks from that of the registration form:AuthenticationController
, create a GET
handler method for a path to logout.Tip
Now, clicking the Logout navbar link will result in a redirect to the login page. You can also now create
a brand new user through the link to register as one, and confirm the object’s existence in your user
table.
Create an AuthenticationFilter
class in the javawebdevtechjobsauthentication
package.
HandlerInterceptorAdapter
.UserRepository
and AuthenticationController
.preHandle
method.Create a whitelist.
AuthenticationFilter
, add a whitelist variable containing the paths that can be
accessed without a user session.preHandle
with a call to this method.Register the filter with Spring.
Create a class called WebApplicationConfig
at the same
level as AuthenticationFilter
with the following:
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @Configuration
public class WebApplicationConfig implements WebMvcConfigurer {
// Create spring-managed object to allow the app to access our filter
@Bean
public AuthenticationFilter authenticationFilter() {
return new AuthenticationFilter();
}
// Register the filter with the Spring container
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor( authenticationFilter() );
}
}
|
Tip
You’ll know your filter setup works when you re-start your application and attempt to get to
localhost:8080
but instead get redirected to /login
.
You’ll also know that your filters are working if hitting your login and and register forms now renders them without any styling. Bonus points if you can determine why this is.
That’s it, that’s all. You’re done. Go forth and test the auth flow. Then add this to any other Spring project you’re working on!