Using Programmatic Security with Web Applications
Start course
1h 6m

In this course, we will learn the concepts of Java EE 7 with a focus on Secure Java EE 7 Applications with Security Mechanism, Web Application Security, and Programmatic Security.

Learning Objectives

  • Secure Java EE 7 Applications

Intended Audience

  • Anyone looking to earn Oracle Java Certification
  • Those who want to improve their Java EE 7 knowledge
  • Java developers


  • Have at least 2 years of Java development experience 

Hello there. In this lesson, we'll take a look at programmatic security with web applications. So, let's start. Programmatic security is used by security aware applications when declared of security alone is not sufficient to express the security model of the application. Some methods of the HttpServletRequest interface enable you to authenticate users for a web application programmatically. Authenticate, which allows an application to instigate authentication of the request caller by the container from within an unconstrained request context. A login dialog box displays and collects the username and password for authentication purposes. Login, which allows an application to collect username and password information as an alternative to specifying form-based authentication in an application deployment descriptor. Logout, which allows an application to reset the caller identity of a request. Here's the code to see how to use login and logout methods. I hope it's clear. In general, security management should be enforced by the container in a manner that is transparent to the web component.

Servlet 3.0 specifies the following methods that enable you to access security information about the components caller. getRemoteUser, which determines the username with which the client authenticated. The getRemoteUser method returns the name of the remote user, the caller associated by the container with the request. If no user has been authenticated, this method returns null. isUserInRole which determines whether a remote user is in a specific security role. If no user has been authenticated, this method returns false. This method expects a string user role name parameter. The security role ref element should be declared in the deployment descriptor with a role name sub element containing the role name to be passed to the method. getUserPrincipal, which determines the principal name of the current user and returns a object. If no user has been authenticated, this method returns null. Calling the get name method on the principal returned by getUserPrincipal returns the name of the remote user. Your application can make business logic decisions based on the information obtained using these APIs. Here's the code to see the use of programmatic security for the purposes of programmatic login. This servlet does the following. It displays information about the current user.

It prompts the user to log in. It prints out the information again to demonstrate the effect of the login method. It logs the user out. It prints the information again to demonstrate the effect of the logout method. I hope it's clear. Declaring and linking role references. A security role reference is a mapping between the name of a role that is called from a web component using isUserInRole string role in the name of a security role that has been defined for the application. If no security role ref element is declared in a deployment descriptor and the userInRole method is called, the container defaults to checking the provided role name against the list of all security roles to find for the web application. Using the default method instead of using the security role ref element limits your flexibility to change role names in an application without also recompiling the servlet making the call. The security role ref element is used when an application uses the HttpServletRequest.isUserInRole string role.

The value passed to the isUserRole method is a string representing the role name of the user. The value of the role name element must be the string used as the parameter to the HttpServletRequest.isUserInRole string role. The role link must contain the name of one of the security roles defined in the security-role elements. The container uses the mapping of the security role ref to security role when determining the return value of the call. For example, to map the security role reference cast to the security role with the role name bankCustomer, the syntax would be... If the servlet method is called by a user in the bankCustomer security role, isUserInRole cast returns true. The role link element in the security role ref element must match a role name defined in the security-role element of the same web.xml deployment descriptor as shown here. A security role reference including the name defined by the reference is scoped to the component whose deployment descriptor contains the security role ref deployment descriptor element. So, that's it. Hope to see you in our next lesson. Have a nice day.


About the Author
Learning Paths

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics