Nearby lessons

34 of 34

Servlet - Examples: Security

📌 What You Will Learn
  • See complete runnable servlet programs
  • Understand the output of each program
  • Copy and deploy programs in Tomcat

Declarative and programmatic security examples including Basic Authentication.

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...

Example01
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>

Example02
Loading editor…

Declarative Security(web.xml declarations)

This tag defines the following 3 child tags

Example03
Loading editor…

Declarative Security(web.xml declarations)

Defines the resource which has to be protected

Example04
Loading editor…

Declarative Security(web.xml declarations)

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

Example05
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.

Example06
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

Example07
Loading editor…

Declarative Security(web.xml declarations)

It specifies the location where we are storing authentication information.

It is required only for basic authentication.

Example08
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:

Example09
Loading editor…

Declarative Security(web.xml declarations)

Example10
Loading editor…

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:

Example11
Loading editor…

Demo Program for Basic Authentication

Example12
Loading editor…

FirstServlet.java

Demo Program for FORM-BASED Authentication:

login.html:

Example13
Loading editor…

FirstServlet.java

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

web.xml:

Example14
Loading editor…

FirstServlet.java

Example15
Loading editor…

FirstServlet.java

Example16
Loading editor…

Programmatic Security

If the authenticated user belongs to the specified role then this method returns "true"

If the authenticated user not belongs to the specified role or if the user not authenticated then

this method returns false.

Example17
Loading editor…

Programmatic Security

Returns the authenticated user name(login name)

If the user has not been authenticated then this method returns null.

Example18
Loading editor…

Programmatic Security

Returns java.security.Principal object which contains user name.

Returns null if the user has not been authenticated.

Eg:

Example19
Loading editor…

Programmatic Security

The main problem in this approach is we are hard coding the role names in the servlet.

If there is any change in the role-name, modifying servlet code is costly and creates maintanence

problems.

To overcome this problem we have to use <security-role-ref> tag. By using this tag we can map

hard coded role names with original role name.

Example20
Loading editor…

Programmatic Security

where admin is logical role name and durgaadmin is original role name.

Example21
Loading editor…

Demo Program for programmatic Security

Example22
Loading editor…

FirstServlet.java

OCWCD

Question Bank

Example23
Loading editor…
📝 Key Takeaways
  • Every example is complete and compiles as-is
  • Examples are grouped by topic
  • Typing programs is the fastest way to learn servlets

Finished this lesson?

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