chamikathereal / J2EE-Security

A simple Java EE project demonstrating role-based authentication and authorization using EJBs, servlets, and declarative security. Ideal for learning how to secure enterprise applications with modern Java EE best practices

Repository from Github https://github.comchamikathereal/J2EE-SecurityRepository from Github https://github.comchamikathereal/J2EE-Security

πŸš€ J2EE-Security

πŸ“ Project Overview

J2EE-Security is a simple yet comprehensive Java EE project that demonstrates role-based security, authentication, and authorization in enterprise web applications. The project uses EJBs, servlets, and declarative security in web.xml to provide protected admin and user areas, secure login/logout, and fine-grained access control at both the web and business logic layers. This project is ideal for learning how to implement robust security in Java EE applications using modern best practices.


πŸ“½οΈ Demo Video

J2EE-Interceptors Project Demo


πŸ—‚οΈ Project Structure

J2EE-Security/
β”œβ”€β”€ src/
β”‚   └── main/
β”‚       β”œβ”€β”€ java/
β”‚       β”‚   └── lk.jlat.web/
β”‚       β”‚       β”œβ”€β”€ ejb/
β”‚       β”‚       β”‚   β”œβ”€β”€ AdminSessionBean
β”‚       β”‚       β”‚   └── UserSessionBean
β”‚       β”‚       β”œβ”€β”€ model/
β”‚       β”‚       └── servlet/
β”‚       β”‚           β”œβ”€β”€ Admin
β”‚       β”‚           β”œβ”€β”€ AutoLogin
β”‚       β”‚           β”œβ”€β”€ Logout
β”‚       β”‚           └── User
β”‚       β”œβ”€β”€ resources/
β”‚       └── webapp/
β”‚           β”œβ”€β”€ admin/
β”‚           β”‚   └── index.jsp
β”‚           β”œβ”€β”€ WEB-INF/
β”‚           β”‚   └── web.xml
β”‚           β”œβ”€β”€ error.jsp
β”‚           β”œβ”€β”€ index.jsp
β”‚           └── login.jsp
β”œβ”€β”€ pom.xml

πŸ“¦ Module Details

🟦 ejb

  • AdminSessionBean
    Stateless EJB for admin-specific business logic and operations.

  • UserSessionBean
    Stateless EJB with methods demonstrating role-based access using annotations like @DenyAll, @PermitAll, and @RolesAllowed.

🟦 servlet

  • Admin
    Servlet for handling admin profile actions and access.

  • AutoLogin
    Servlet that demonstrates programmatic login for testing or automation.

  • Logout
    Servlet to securely invalidate sessions and handle user logout.

  • User
    Servlet for user-specific actions, invoking secured EJB methods.

🟦 webapp

  • admin/index.jsp
    Admin dashboard, accessible only to users with the admin role.

  • WEB-INF/web.xml
    Security configuration, defining protected URL patterns, authentication methods, and role mappings.

  • login.jsp
    Custom login page for form-based authentication.

  • error.jsp
    Error page for failed login attempts.

  • index.jsp
    Public landing page.


βš™οΈ Key Features

  • βœ… Role-based access control for admin and user areas
  • βœ… EJB method-level security using @DenyAll, @PermitAll, and @RolesAllowed
  • βœ… Secure login and logout with session management
  • βœ… Declarative security configuration via web.xml
  • βœ… Custom error and login pages for enhanced user experience

πŸ’‘ How It Works

  • Authentication:
    Users log in via a form-based authentication mechanism. Credentials are validated and roles are assigned based on configuration.

  • Authorization:
    Access to servlets and EJB methods is controlled using both declarative (web.xml) and programmatic (EJB annotations) security.

  • Session Management:
    Secure session handling ensures users can log out safely, and unauthorized access is prevented.

  • Admin and User Separation:
    Admin and user functionalities are strictly separated, with dedicated servlets, EJBs, and protected JSPs.


πŸ› οΈ Technologies Used

  • Java 11
  • Jakarta EE 10 (EJB, Servlet, Security)
  • Maven

πŸ“š Learning Outcomes

  • βœ… Understand Java EE security concepts and best practices
  • βœ… Implement role-based access control in both web and EJB layers
  • βœ… Configure authentication and authorization declaratively and programmatically
  • βœ… Build secure, maintainable enterprise applications

πŸ§‘β€πŸ’» Author

Chamika Gayashan
Undergraduate Software Engineer | Sri Lanka
Linkedin: @chamikathereal
Current date: Sunday, June 22, 2025, 1:30 AM +0530

About

A simple Java EE project demonstrating role-based authentication and authorization using EJBs, servlets, and declarative security. Ideal for learning how to secure enterprise applications with modern Java EE best practices


Languages

Language:Java 100.0%