Understanding Web Security Using web.xml Via Use Cases
The deployment descriptor, web.xml is the most important Java EE configuration piece of Java EE Web applications.
Join the DZone community and get the full member experience.
Join For FreeThe deployment descriptor, web.xml is the most important Java EE configuration piece of Java EE Web applications. The security configuration in this descriptor drives the semantics and operation of web container security. Hence it is very critical that web developers and administrators understand the various combinations possible in the security configuration in this descriptor.
Security Constraints
Security Constraints are least understood by web developers, even though they are critical for the security of Java EE Web applications. Specifying a combination of URL patterns, HTTP methods, roles and transport constraints can be daunting to a programmer or administrator. It is important to realize that any combination that was intended to be secure but was not specified via security constraints, will mean that the web container will allow those requests. Security Constraints consist of Web Resource Collections (URL patterns, HTTP methods), Authorization Constraint (role names) and User Data Constraints (whether the web request needs to be received over a protected transport such as TLS).
In this section, we will look at specifying the security constraints for multiple use cases.
Use Case: We would like to define a set of web resources that will have unchecked access. We will achieve this by omitting the authorization constrainsts (auth-constraint element).
<security-constraint> <web-resource-collection> <web-resource-name>All Access</web-resource-name> <url-pattern>/unchecked/*</url-pattern> <http-method>DELETE</http-method> <http-method>PUT</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint></security-constraint>
Use Case: HTTP GET operation on a set of web resources should be accessible only by an user with the role "Employee". We will achieve this with the specification of authorization constraints (auth-constraint element with the role-name element).
<security-constraint> <display-name>Restricted GET To Employees</display-name> <web-resource-collection> <web-resource-name>Restricted Access - Get Only</web-resource-name> <url-pattern>/restricted/employee/*</url-pattern> <http-method>GET</http-method> </web-resource-collection> <auth-constraint> <role-name>Employee</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint></security-constraint>
CAUTION: Remember that in this example, you have specified a http-method of GET. This implies that security constraint is applied to GET. All the other http methods (POST, PUT,TRACE,HEAD etc) are open. If you do not want the vulnerability, then do not specify a http method.
Use Case: We would like to exclude a set of web resources from any access. This can arise when a certain portion of the web application needs to undergo some form of maintenance or is not applicable for a particular physical deployment of a generic web application. We will achieve this with authorization constraints that specify no roles.
<security-constraint> <display-name>excluded</display-name> <web-resource-collection> <web-resource-name>No Access</web-resource-name> <url-pattern>/excluded/*</url-pattern> <url-pattern>/restricted/employee/excluded/*</url-pattern> <url-pattern>/restricted/partners/excluded/*</url-pattern> </web-resource-collection> <web-resource-collection> <web-resource-name>No Access</web-resource-name> <url-pattern>/restricted/*</url-pattern> <http-method>DELETE</http-method> <http-method>PUT</http-method> <http-method>HEAD</http-method> <http-method>OPTIONS</http-method> <http-method>TRACE</http-method> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint /> <user-data-constraint> <transport-guarantee>NONE</transport-guarantee> </user-data-constraint></security-constraint>
Authentication
A web container can authenticate a web client/user using either HTTP BASIC, HTTP DIGEST, HTTPS CLIENT or FORM based authentication schemes.
Use Case: We would like to utilize the browser authentication mechanism, HTTP BASIC as defined in the HTTP 1.0 specification.
The login-config.xml element in web.xml would look like the following:
<login-config> <auth-method>BASIC</auth-method> <realm-name>default</realm-name></login-config>
Use Case: We would like to utilize HTTPS Client authentication mechanism that is based on digital certificates. The authentication is based on the user's X509 certificate.
The login-config.xml element in web.xml would look like the following:
<login-config> <auth-method>CLIENT-CERT</auth-method> <realm-name>JMX Console</realm-name></login-config>
Use Case: We would like to utilize FORM based authentication mechanism.
FORM based mechanism provides flexibility in defining a custom jsp/html page for login and another page to direct for errors during login.
The login-config xml element in web.xml would look like the following:
<login-config> <auth-method>FORM</auth-method> <form-login-config> <form-login-page>/login.html</form-login-page> <form-error-page>/error.html</form-error-page> </form-login-config></login-config>
In this case, the html page for login is the login.html and if any errors are encountered (including failed login), the user is directed to error.html
Reference
About the Author: Anil Saldhana is the Lead Security Architect at JBoss. He blogs at http://anil-identity.blogspot.com
Opinions expressed by DZone contributors are their own.
Comments