Web Application Security
Basic Terminology :Based on Servlet specification compare the following Security mechanisms
* what is difference between Authorization and Confidentiality ? Authorization prevents information reaching and unintended users at beginning only , where as Confidentiality ensure that even though information falls in wrong hand it stills remains unreachable. Ex:Beer website application
Types of Authentication Mechanism :According to servlet specification 4 types of authentication mechanism possible.
DECLARATIVE SECURITY:
objective: In the deployment descriptor declare ......
All the above 3 tags are direct child tags of < web-app > hence we can take any where with in the < web-app > tag but it is convention to take the above order only. < security-constraint >It defines the following 3 child tags.
<web-resource-collection >It contains there are 4 child tags.
< auth-constraint >It defines the following 2 child tags.
< user-data-constraint >It defines the following 2 child tags.
The allowed values for these tags are..
note : we have only one < user-data-constraint > per < security-constraint > < login-config >This tag specifies what type of authentication , we are using it contain the following 3 child tags .
< security-role >It can be used to defines security roles in web-applicationsThis tag contains the following 2 child tags.
Example: < security-role > < role-name > jobs4times < / role-name > < / security-role > < security-role > < role-name > ashok < / role-name > < / security-role > Write a program BASIC authentication mechanism: http://localhost:7001/scwcd/login.jsp login.jsp <form action="/BasicAuthentication" method="post"> Enter name : <input type="text" name="uname"><br> <input type="submit" value="submit"> </form> BasicAuthentication package com.jobs4times; public class BasicAuthentication implements HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doGet( ) method "); System.out.println("Get: After Authentication only we can access this servlet " ); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doPost( ) method "); System.out.println("Post: After Authentication only we can access this servlet " ); } } web.xml <web-app> <servlet> <servlet-name>BasicAuthentication</servlet-name> <servlet-class>com.jobs4times.BasicAuthentication</servlet-class> </servlet> <servlet-mapping> <servlet-name>BasicAuthentication</servlet-name> <url-pattern>/BasicAuthentication</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>basic</web-resource-name> <url-pattern>/BasicAuthentication</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>adminrole</role-name> <role-name>ashokrole</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method>BASIC </auth-method> </login-config> <security-role> <role-name>ashokrole</role-name> </security-role> <security-role> <role-name>adminrole</role-name> </security-role> </web-app> tomcat-users.xml <tomcat-users> <role rolename="adminrole"/> <role rolename="ashokrole"/> <user name="admin" password="scjp" roles="adminrole" /> <user name="ashok" password="scwcd" roles="ashokadmin" /> <!-- <user name="admin" password="scjp" roles="adminrole,ashokarole" /> //both roles same username , pwd --> </tomcat-users> Rules for auth-constraint :
Rules for < role-name > tag :
Key points for <web-resource-collection> :
Writa a Program for FORM-BASED AUTHENTICATION MECHANISM: login.html <h3>Form Based Authentication Example :</h3> <form action="j_security_check" method="post"> <table border="2" bgcolor="lightgrey"><tr><td> UserName: <input type="text" name="j_username"><br></td></tr><tr><td> Password:<input type="password" name="j_password"><br></td></tr> <tr><td align="center"><input type="submit" value="Submit"></td></tr> </table> </form> error.html <body> <pre>Your credentials are not correct<br> please provide valid credentials</pre> <body> post.html <form method="post" action="./post"> <input type="submit" > </form> FormServlet.java package com.jobs4times; public class FormServlet implements HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doGet( ) method "); System.out.println("Get: After Authentication only we can access this servlet " ); } public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doPost( ) method "); System.out.println("Post: After Authentication only we can access this servlet " ); } } web.xml <web-app> <servlet> <servlet-name>FormServlet</servlet-name> <servlet-class>com.jobs4times.FormServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FormServlet</servlet-name> <url-pattern>/FormServlet</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>form</web-resource-name> <url-pattern>/FormServlet</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>ashokrole</role-name> </auth-constraint> </security-constraint> <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> <security-role> <role-name>ashokrole</role-name> </security-role> </web-app> tomcat-users.xml <tomcat-users> <role rolename="ashokrole"/> <user name="ashok" password="scwcd" roles="ashokadmin" /> </tomcat-users> Programatic Security:Some times declarative security may not enough to meet programming requirement.Ex: Based on the user role we have to provide customized response , if the user is admin then we have to provide admin related response. If the user is manager , then we have to provide manager related response. To meet this requirement compulsary we should go for programmatic security and Declarative security is notenough. We can implement programmatic security by using the following methods of HttpServletRequest(I).
Example: public class FirstServlet implements HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doGet( ) method "); if(request.isUserInRole("admin")) { //this is admin related response }else { //this is general response } } } The main problem is this approach is ........ we are hardcode in servlet , there is any change in the role-name modify the servlet code is costly operation and creates maintanability problems , to resolve this we should go for <security-role-ref> in web.xml, by using this tag we can configure/map hardcoded role-name with original value. Write a demo program for programmatic security: login.html <h3>programmatic security:</h3> <form action="/test" method="get"> <table border="2" bgcolor="lightgrey"><tr><td> UserName: <input type="text" name="uname"><br></td></tr><tr><td> Password:<input type="password" name="pwd"><br></td></tr> <tr><td align="center"><input type="submit" value="Submit"></td></tr> </table> </form> FirstServlet.java public class FirstServlet implements HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException { System.out.println("We are in doGet( ) method "); if(request.isUserInRole("hero")) { out.println("this is hero home page"); }else { out.println("this is others home page"); } } } web.xml <web-app> <servlet> <servlet-name>first</servlet-name> <servlet-class>com.jobs4times.FirstServlet</servlet-class> <security-role-ref> <role-name>hero</role-name> // hardcoded rolename in servlet <role-link>adminrole</role-link> //original roleName </security-role-ref> </servlet> <servlet-mapping> <servlet-name>first</servlet-name> <url-pattern>/test</url-pattern> </servlet-mapping> <security-constraint> <web-resource-collection> <web-resource-name>checked</web-resource-name> <url-pattern>/test</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>adminrole</role-name> <role-name>ashokrole</role-name> </auth-constraint> </security-constraint> <login-config> <auth-method>BASIC </auth-method> </login-config> <security-role> <role-name>adminrole</role-name> </security-role> </web-app> tomcat-users.xml <tomcat-users> <role rolename="adminrole"/> <role rolename="ashokrole"/> <user name="admin" password="scjp" roles="adminrole" /> <user name="ashok" password="scwcd" roles="ashokadmin" /> </tomcat-users> Multiple Securuty constraints elements with same name url-pattern ( or partially matching url-patrtern and http-method elements ) web.xml <web-app > ................... <security-constraint> <web-resource-collection> <web-resource-name>display</web-resource-name> <url-pattern>/beer/updateReceipts/*</url-pattern> <url-pattern>/beer/displayReceipts/*</url-pattern> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> //Authorization constraints with different roles </auth-constraint> </security-constraint> <security-constraint> <web-resource-collection> <web-resource-name>update</web-resource-name> <url-pattern>/beer/updateReceipts/*</url-pattern> <url-pattern>/beer/updateUser/*</url-pattern> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> //Authorization constraints with different roles </auth-constraint> </security-constraint> </web-app> How should container handle authorization when the same resource used by more than one < security-constraint >
note: When 2 different non-empty auth-constraint elements apply to the same constraint resource access is granted to the union of all roles from both of the auth-constraint elements . Implementing Authentication : 4 login-config Examples : web.xml <web-app> <login-config> <auth-method>BASIC</auth-method> </login-config> ----------------OR---------------------- <login-config> <auth-method>DIGEST</auth-method> </login-config> ----------------OR--------------------- <login-config> <auth-method>CLIENT-CERT</auth-method> </login-config> ----------------OR--------------------- <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> </web-app> Except for form once we have declared login-config element in the deployment description(web.xml) , implementing authenticatiuon is done(assume we have already configured userName or password or roles configured at server level). web.xml <web-app> <security-constraint> <web-resource-collection> <web-resource-name> <url-pattern> <http-method> <description> </web-resource-collection> <auth-constraint> <description> <role-name> </auth-constraint> <user-data-constraint> <transport-guarantee> </user-data-constraint> </security-constraint> <login-config> <auth-method> <relian-name> <form-login-config> <form-login-page> <form-error-page> </form-login-config> </login-config> <security-role> <role-name> </security-role> </web-app> |