Nearby lessons

23 of 34

Servlet - Declarative Security (web.xml)

📌 What You Will Learn
  • Understand Declarative Security(web.xml declarations)
  • Understand Demo Program for Basic Authentication
  • See complete working code examples

Declarative Security (web.xml) is an essential part of the Java Servlet technology. This lesson explains Declarative Security(web.xml declarations), Demo Program for Basic Authentication and FirstServlet.java with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid.

Declarative Security(web.xml declarations)

In the deployment descriptor declare

1.<security-constraint>

2.<web-resource>

3.<transport-guarentee>

4.<login-config>

5.<security-role>

We can implement web security by using the following 3 tags.

Declarative Security(web.xml declarations)

It defines the resources which have to be protected, which roles are allowed to access and security

constraint is applicable for which type of http methods etc...

Example02
Loading editor…

Declarative Security(web.xml declarations)

It defines the type of authentication what we are using.

3.<security-role>

It defines the security roles which are allowed in the web application.

Note:

The above 3 tags are direct child tags of <web-app> and hence we can place anywhere within the

<web-app>

Example03
Loading editor…

Declarative Security(web.xml declarations)

This tag defines the following 3 child tags

Example04
Loading editor…

Declarative Security(web.xml declarations)

Defines the resource which has to be protected

Example05
Loading editor…

Declarative Security(web.xml declarations)

Authorization constraint which determines what roles are allowed to access the resource.

Example06
Loading editor…

Declarative Security(web.xml declarations)

It specifies what type of protection is required when trasporting the resource accross the

network.

1.<web-resource-collection>:

This tag contains the following 4 child tags

1.<web-resource-name>

2.<description>

3.<url-pattern>

4.<http-method>

It specifies the Http method to which security constraint is applicable.

If we are not using this tag, then security constraint is applicable for all methods.

2.<auth-constraint>:

It specifies which security roles are allowed to access protected resource.

It contains the following 2 child tags.

1.<description>

2.<role-name>

If the security constraint is applicable for all the roles, we have to specify as follows...

<role-name>*</role-name>

3.<user-data-constraint>:

This tag contains the following 2 child tags

1.<description>

2.<transport-guarentee>

This tag specifies what type of guarantee we are providing while transporting the resource

across the network.

The allowed values for this tag are:

1.NONE:

It means the data is transported in plain text form.

It is the default value

2.INTEGRAL:

It means the data should not be changed in trans

  • CONFIDENTIAL:

It means the data is transported in encryption form.

The required priority order is: CONFIDENTIAL,INTEGRAL and NONE.

Example07
Loading editor…

Declarative Security(web.xml declarations)

This tag specifies the type of authentication we are using.

It contains the following child tags

1.<auth-method>

It specifies the authentication method

The allowed values are

BASIC

DIGEST

FORM

CLIENT-CERT

Example08
Loading editor…

Declarative Security(web.xml declarations)

It specifies the location where we are storing authentication information.

It is required only for basic authentication.

Example09
Loading editor…

Declarative Security(web.xml declarations)

This tag is required to specify login page url and error page url in the case of Form based

authentication.

This tag contains the following 2 child tags

1.<form-login-page> /login.html </form-login-page>

2.<form-error-page> /error.html </form-error-page>

3.<security-role>:

It can be used to define security roles in the web application.

This tag contains the following 2 child tags.

1.<description>

2.<role-name>

Summary of all security related tags:

Example10
Loading editor…

Declarative Security(web.xml declarations)

Example11
Loading editor…

Demo Program for Basic Authentication

web.xml:

Demo Program for Basic Authentication

<tomcat-users>

...

<role rolename="durgarole"/>

<user name="durga" password="java" roles="durgarole" />

<user name="ravi" password="scjp" roles="durgarole" />

</tomcat-users>

postreqform.html:

Example13
Loading editor…

Demo Program for Basic Authentication

Example14
Loading editor…

FirstServlet.java

Demo Program for FORM-BASED Authentication:

login.html:

Example15
Loading editor…

FirstServlet.java

<h1>Your credentials are not correct. Please provide valid credentials</h1>

web.xml:

Example16
Loading editor…

FirstServlet.java

Example17
Loading editor…
📝 Key Takeaways
  • Key ideas of Servlet - Declarative Security (web.xml) explained simply
  • Ready-to-use code examples
  • Exam-style questions at the end

🧠 Test Your Knowledge

3 Questions
Progress: 0 / 3

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.