Nearby lessons
34 of 34Servlet - Examples: Security
- 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...
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>
Declarative Security(web.xml declarations)
This tag defines the following 3 child tags
Declarative Security(web.xml declarations)
Defines the resource which has to be protected
Declarative Security(web.xml declarations)
Authorization constraint which determines what roles are allowed to access the resource.
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.
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
Declarative Security(web.xml declarations)
It specifies the location where we are storing authentication information.
It is required only for basic authentication.
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:
Declarative Security(web.xml declarations)
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:
Demo Program for Basic Authentication
FirstServlet.java
Demo Program for FORM-BASED Authentication:
login.html:
FirstServlet.java
<h1>Your credentials are not correct. Please provide valid credentials</h1>
web.xml:
FirstServlet.java
FirstServlet.java
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.
Programmatic Security
Returns the authenticated user name(login name)
If the user has not been authenticated then this method returns null.
Programmatic Security
Returns java.security.Principal object which contains user name.
Returns null if the user has not been authenticated.
Eg:
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.
Programmatic Security
where admin is logical role name and durgaadmin is original role name.
Demo Program for programmatic Security
FirstServlet.java
OCWCD
Question Bank
- Every example is complete and compiles as-is
- Examples are grouped by topic
- Typing programs is the fastest way to learn servlets