Access to secure Admin page here!
ConceptsAccess to secure User page here!
V 1.0 © Aptech Limited Session 15 Java Security The roles of the application should be defined on the Web server In order to define the users and roles on the application access the Domain Admin Console as shown in figure 15.4 Figure 15.4: Accessing the Domain Admin Console In order to define the roles for the intended files in the application, follow the given path Configurations → server-config → Security → Realms → file ‘File’ is selected as security is defined at the file level here in the application The selection appears as shown in figure 15.5 Concepts Figure 15.5: Selecting the Component for Security Configuration V 1.0 © Aptech Limited Session 15 Java Security Click the Manage Users button in the server configuration screen This will lead to an interface where users for the application can be defined as shown in figure 15.6 Figure 15.6: Adding New Users ClickNew The New File Realm User screen is displayed as shown in figure 15.7 Here, a new user and the validation credentials can be defined Figure 15.7: Defining User Credentials Concepts Create two users, admin and user (specify the names in the User ID box) as per the application requirement Provide password as admin123 for admin and user123 for user V 1.0 © Aptech Limited Session 15 Java Security Figure 15.8 shows creation of the admin user Figure 15.8: Creating the admin User Once the users are created they will appear in the user table as shown in figure 15.9 Figure 15.9: Users in the File Users Table The File Users table contains all the users defined on the Web server Concepts Once the users are defined, the developer has to define the authentication mechanism to login and access the resources This is done in the application deployment descriptor web.xml V 1.0 © Aptech Limited Session 15 Java Security Click the Security tab in the deployment descriptor as shown in figure 15.10 Figure 15.10: Selecting the Login Mechanism Define the login mechanism in the Login Configuration section; here the Basic login mechanism is selected Add security roles to the application by clicking Add in the Security Roles section It will lead to the screen as shown in figure 15.11 where the security roles can be added to the application Concepts Figure 15.11: Adding Security Roles V 1.0 © Aptech Limited Session 15 Java Security Figure 15.12 shows the state of the security roles table after the roles are added with corresponding description Figure 15.12: Roles Added to the Deployment Descriptor Once the roles are added, define the security constraints in the deployment descriptor by clicking Add Security Constraint It leads to a screen as shown in figure 15.13 Figure 15.13: Defining Security Constraint Concepts Specify UserConstraint in the Display Name box and click Add This will lead to screen as shown in figure 15.14 Figure 15.14: Mapping the User to the Access Domain V 1.0 © Aptech Limited Session 15 Java Security In this wizard, map the user to the folder which the user can access The role ‘user’ can access all the files in the folder secureUser according to the given URL pattern Click OK The user to resource mapping will be displayed in the Web Resource Collection section Similarly, add the AdminConstraint for admin with respect to the secureAdmin folder Select the checkbox ‘Enable Authentication Constraint’ while defining the UserConstraint as well as the AdminConstraint Click Edit to set the Role Name as user for UserConstraint and admin for AdminConstraint Figure 15.15 shows the final state of the User Constraint after all the values are configured Figure 15.15: User Constraint Created Concepts It is essential to select ‘Enable Authentication Constraint’ while defining the Security Constraint This choice will prompt for the username and password credentials while accessing the resource The access to these resources is allowed only for the value in the ‘Role Name(s)’ field V 1.0 © Aptech Limited Session 15 Java Security Once the application’s deployment descriptor is configured, configure the Web server deployment descriptor In case of GlassFish server it is GlassFish-web.xml If the deployment descriptor is not already present in the application, right-click the project and select New → Other → GlassFish → GlassFish Descriptor as shown in figure 15.16 Concepts Figure 15.16: Adding GlassFish Deployment Descriptor V 1.0 © Aptech Limited Session 15 Java Security Once the Web server deployment descriptor is created, open it and click the Security tab This will lead to the screen as shown in figure 15.17 The roles created earlier in the server’s Admin Console are seen listed here Figure 15.17: Mapping the Security Roles On expanding the security roles, the screen shown in figure 15.18 appears Click Add Principal to add users to the role For admin role, add ‘admin’ as the Principal Similarly, add ‘user’ as Principal for the user role Figure 15.18: Adding Principals Figure 15.19: Executing the Secure Web Application V 1.0 © Aptech Limited Concepts After all the configurations are completed, deploy and run the application The application execution will lead to the index page as shown in figure 15.19 Session 15 Java Security Click the hyperlink It will prompt for user name and password as shown in figure 15.20 Figure 15.20: Prompting for User Name and Password On providing the appropriate credentials, it will lead to the Web page as shown in figure 15.21 Figure 15.21: Accessing the Web Page after Authentication Concepts Note: The application may not behave as expected at the first execution It may give a ‘403:Forbidden page’ error The roles and respective credentials have to be properly deployed onto the application/ Web server This may require refreshing/restarting the Web server V 1.0 © Aptech Limited Session 15 Java Security 15.10 Check Your Progress (A) Deployment descriptor (C) HttpServletRequest interface (B) (D) None of these Annotations Which of the following authentication mechanisms uses public key cryptography? (A) Basic authentication (C) Digest authentication (B) Form authentication (D) Client authentication Which of the following can be a sub element of security-constraint element? (A) web-resource-constraint (C) user-data constraint (B) auth-constraint (D) All of these Which of the following annotations are not used for security mechanisms in Java EE applications? (A) @Stateless (C) @RolesAllowed (B) @DenyAll (D) None of these Identify the method which is used to define the security mechanism programmatically in Web applications (A) authenticate (C) getUserName (B) logout (D) All of these Concepts Which of the following is used for defining the security mechanism programmatically for Web applications? V 1.0 © Aptech Limited Session 15 Java Security 15.10.1 Answers C D D A D Concepts V 1.0 © Aptech Limited Session 15 Java Security Summary S ecurity mechanisms in both enterprise and Web applications are specified both declaratively and programmatically ỴỴ Security mechanisms are declaratively specified through annotations and deployment descriptors ÎÎ Programmatically security mechanisms are specified through Java security APIs such as JAAS ỴỴ S ecurity mechanisms in Web applications are implemented at three levels: application level security, transport level security, and message level security ỴỴ ethods of HttpServletRequest interface are used to programmatically define security M mechanisms for Web applications ỴỴ T he security roles and mapping can be done on the application deployment descriptor and Web server deployment descriptor Concepts ẻẻ V 1.0 â Aptech Limited