Spring Security Starter Project
In this post, we will be developing a simple web application which has an industry level security.
Prerequisites:
- Spring MVC
- Spring JPA
- Spring boot
Tools used :
- IntelliJ IDEA Community Edition(IDE)
- Spring JPA
- Thymeleaf
- Spring Security
- Spring Web
- Postgres SQL (Backend Database)
- JDK 8
For starting the project will be using Spring initializer:
To get the same setup use this link:
Click on the GENERATE button. A SecurityDemo.zip file will be downloaded then extract it. Now we are free to use any IDE, I will be using IntelliJ IDEA Community Edition. I did the following steps:
- Click on: File>New>Project From existing Sources
- Select your unzip SecurityDemo folder
- Select: Import project from external model
- Select: Maven
- Click on: Finish Button
- Wait for some time required dependencies will be downloaded.
Your pom.xml file should look like below file this is auto-generated.
Now we will create three pages inside resoucers>template home.html, hello.html and login.html pages.
Update application.properties file.
You have to enter your own credentials in the above file.
Now we will be adding some layers and after that, your file would look this:
This is the User class which will be used by Hibernate for ORM.
Now add front Controller
Add repository interface. Its implementation will be provided by the spring.
Provide implementation to UserDetails and it should have your User. Override the methods and done.
Create a WebsecurityConfig class which extends WebSecurrutyConfiguration adaptor and annotate with @Configuration and @EnableWebSecurity.
Override configure method and define the login page and logout URL and other required properties.
Create a MyUserDetailsService which implements UserDetailsService and annotate it with @Service annotation. This service is responsible for fetching data from the Database and returning an object which has a UserDetails.
Spring boot is a stand-alone program. Create the below class and run it as a java application.
Now open any browser and enter “localhost:8080/home” below show screen will appear. click on here it redirects you to the login page.
You will see the below page. Enter the credentials as stored in a database.
Now you will be redirected to hello page.
Greate Job you have created a stand-alone secured Spring boot web application.