Spring Security Starter Project

Krishnkant Jaiswal
3 min readFeb 7, 2021

--

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:

My Initializer Setup

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:

Auto-generated Project Structure
  1. Click on: File>New>Project From existing Sources
  2. Select your unzip SecurityDemo folder
  3. Select: Import project from external model
  4. Select: Maven
  5. Click on: Finish Button
  6. Wait for some time required dependencies will be downloaded.

Your pom.xml file should look like below file this is auto-generated.

pom.xml

Now we will create three pages inside resoucers>template home.html, hello.html and login.html pages.

home.html
hello.html
login.html

Update application.properties file.

application.properties

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:

Final file structure

This is the User class which will be used by Hibernate for ORM.

User.class

Now add front Controller

Controller.class

Add repository interface. Its implementation will be provided by the spring.

UserReposiotry.class

Provide implementation to UserDetails and it should have your User. Override the methods and done.

UserPricipal.class

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.

WebsecurityConfig.class

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.

MyUserDetailsService.class

Spring boot is a stand-alone program. Create the below class and run it as a java application.

SpringSecurityApplication.class

Now open any browser and enter “localhost:8080/home” below show screen will appear. click on here it redirects you to the login page.

home page

You will see the below page. Enter the credentials as stored in a database.

login page

Now you will be redirected to hello page.

Hello page

Greate Job you have created a stand-alone secured Spring boot web application.

--

--

No responses yet