Build a secured EJB Module with role-based access control, deploy to GlassFish, and configure users in the File Realm.
Set up the project, add the sample entity/bean files, and wire in the security descriptor.
If you have forgotten how to create an EJB Module, refer back to Week 5 Lab — Part 2.
EJB security roles must be mapped to GlassFish groups using a server-specific descriptor file.
glassfish-ejb-jar.xml Not VisibleThis is the same family of issue as when persistence.xml doesn't show in the Projects tab. The file exists on disk — NetBeans just isn't displaying it in that view.
Open glassfish-ejb-jar.xml and switch to the Security tab in the editor. Add two role mappings:
| EJB Security Role | GlassFish Group Name | Who goes in this group? |
|---|---|---|
student | Enterprise | Users with read-only access (e.g., Mike) |
staff | StaffEnterprise | Users with full/admin access (e.g., Stephen) |
"student" and "staff". GlassFish uses its own group system to hold real users. This XML file is the bridge — it tells GlassFish: "when my code asks for role student, check GlassFish group Enterprise."
staffStaffEnterprisestudent → group Enterprise<glassfish-ejb-jar>
<security-role-mapping>
<role-name>student</role-name>
<group-name>Enterprise</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>staff</role-name>
<group-name>StaffEnterprise</group-name>
</security-role-mapping>
</glassfish-ejb-jar>
Check the descriptor is correct, then compile the project.
Open the XML tab of glassfish-ejb-jar.xml. Confirm it looks exactly like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-ejb-jar PUBLIC
"-//GlassFish.org//DTD GlassFish
Application Server 3.1...//EN"
"...">
<glassfish-ejb-jar>
<security-role-mapping>
<role-name>student</role-name>
<group-name>Enterprise</group-name>
</security-role-mapping>
<security-role-mapping>
<role-name>staff</role-name>
<group-name>StaffEnterprise</group-name>
</security-role-mapping>
</glassfish-ejb-jar>
In the Output panel, you should see:
BUILD SUCCESSFUL (total time: X seconds)
& not & in the JDBC URL, and that it is inside
src/conf/META-INF/.
Create the MySQL database and JDBC resources, then deploy the built JAR.
Open MySQL Workbench and run the following:
-- Create the database for this project
CREATE DATABASE w7p1db;
USE w7p1db;
-- (Tables will be created automatically by JPA / GlassFish on first run)
Book.java and CD.java are JPA entities. GlassFish will map them to database tables via the JDBC data source. Without the database, deployment will fail.
Go to the GlassFish Admin Console at http://localhost:4848
W7P1Pooljava.sql.Drivercom.mysql.jdbc.Driverurl, user, password, DatabaseName=w7p1dbjdbc/W7P1DSW7P1Pool (the one just created)<jta-data-source> value must exactly match: jdbc/W7P1DS
BUILD SUCCESSFUL and deploy successfulW7P1EJBModule listed with engine type ejb and a green tick under Enabled.
persistence.xml is at src/conf/META-INF/persistence.xmlAdd the real users that GlassFish will authenticate. These users are matched to EJB roles via the group names in glassfish-ejb-jar.xml.
GlassFish's File Realm stores usernames and passwords in a flat file on the server. We use it in development — real systems use database or LDAP realms.
In File Users → click New and fill in each user:
| Field | User 1 | User 2 |
|---|---|---|
| User ID | Mike | Stephen |
| Group List | Enterprise | StaffEnterprise |
| Password | (set any password) | (set any password) |
Enterprise → glassfish-ejb-jar.xml maps Enterprise → role student → @RolesAllowed("student") methods are accessible.
Look at how ItemEJB.java uses security annotations. Here is a simplified version:
@Stateless
@RolesAllowed("student")
public class ItemEJB
implements ItemRemote,
ItemLocal {
// Any student or staff can list
public List<Book> findAllBooks() {
return em.createQuery(
"SELECT b FROM Book b",
Book.class
).getResultList();
}
// Only staff can create items
@RolesAllowed("staff")
public Book createBook(Book b) {
em.persist(b);
return b;
}
}
| Annotation | Effect |
|---|---|
@RolesAllowed("student")on the class | Default: all methods are accessible to student AND staff roles. |
@RolesAllowed("staff")on a method | Overrides class default — only staff can call this specific method. |
Click each item as you confirm it. All boxes must be ticked before starting Part 2.
student → Enterprise and staff → StaffEnterpriseBUILD SUCCESSFULEnterprise createdStaffEnterprise createdYou will build a client application (a standalone Java app) that:
Mike (student role) or Stephen (staff role)ItemEJB remotely@RolesAllowed("staff") method, he will get an EJBAccessException| What you did | Why it matters |
|---|---|
| Created EJB Module with security annotations | Defines the access rules in code |
| Added glassfish-ejb-jar.xml | Maps code roles to server groups |
| Built & deployed to GlassFish | Makes the EJB callable over the network |
| Created users in File Realm | Gives GlassFish real identities to authenticate |