Secure Employee Management System

Learn via video courses
Topics Covered

Overview

The employee management system we have is running with all the functional features; however, it needs one important aspect; Security.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Dependency

The only dependency we need is starter-security.

Secure It

Step 1 - Create User and Roles Entities and Tables.

To work with spring security, we need two entities, User and Roles, and corresponding tables. We need many to many relationships between users and roles. Create user and roles entities and tables

User Entity

Role Entity

To represent many to many relationships, we need three tables in the database.

Sharpen Your Fundamentals with Free Learning

Step 2 - Repository

We need UserRepository to interact with the User entity.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Step 3 - UserDetailService

We need a concrete class that implements UserDetailsService

Step 4 - Controller to Register and Login User

AuthenticationController

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Step 5 - Security Configuration

We need to configure spring security to tie up different classes to make it work for us.

With this configuration done, all our endpoints are secured by spring security except /register and /login.

We are using JWT-based authentication in our code.

Verification

  • Let's try to invoke any employee API.

The response is 403 - Forbidden because we haven't authenticated yet.

  • Register a new user Let's try registering a new user with the required details.
  • Login with the registered user

A successful login returns a jwt token in the response, which should be carried with subsequent requests.

Let's try to create a new employee using the acquired token.

Likewise, all the other employee-related APIs can be invoked by providing the acquired JWT token.

Secure Admin Endpoints

Everything to this point is fine, but our application has one serious security issue. An employee can also invoke admin endpoints, but not the behavior we want. Only the admin users are allowed to access admin-related endpoints.

We will use method-level security to fix the problem. Simply put, spring security supports authorization at the method level. Putting the role on the method with the annotation @Secured will secure our method from unauthorized access.

Let's secure our admin endpoints.

1. Enable method-level security.

2. Secure methods

Verification

1. Let's create another user in the system with the role of admin

Now we have two users in the database.

Let's try to delete an employee using t.stark@gmail.com

As the expected user is not allowed to act.

Let's try with user s.rogers@gmail.com

Full source code is available at GitHub location.

Conclusion

In this article, we have

  • Secured employee management system using spring security.
  • Used JWT token for authentication.
  • Implemented spring security method level security based on user role.