Security and Authorization Issues
Security and Authorization Issues
Security and Authorization Issues
issues
4 levels or senses of the security
issue to look at
• Authentication
• Authorization
• Integrity
• Confidentiality
Security
• Client wants to be sure she is talking to a
legitimate server (authentication) and also
wants to make sure information passes is
confidential. The server wants to make
sure the client is who she claims to be,
and that information remains confidential.
Both sides want to be sure the information
passes arrives without tampering.
2.2 security
• Web servers are not required to implement
all servlet 2.2 API security mechanisms to
be 2.2 compliant.
• Some implement just parts of it and some
implement none at all.
• A server must implement all to be J2EE
compliant.
Role-based authentication
• Tags in web.xml and other xml files can
specify roles and restrict resource access
to those in certain roles.
• The deployment descriptor (web.xml)
specifies the type of access granted to
each role but doesn’t map roles to users.
Server-specific tools used during
application deployment assign roles, which
may come from text, db tables, OS, server
files (like tomcat users) and so on.
Salary servlet example
• We wish to restrict access to a servlet that gives out salary information to
managers:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SalaryServer extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/plain");
PrintWriter out = res.getWriter();
out.println("Top-secret information:");
out.println("Everyone else gets paid more than you!");
}
}
Tomcat roles
• On tomcat, roles are assigned in a tomcat-
users file in the conf directory of tomcat.
(This was true at the time the text was
written and seems still to be the case).
• On another server (container), another
sort of mechanism might define these
roles.
tomcat-users.xml file in conf
directory
• This is already defined and has some entries in it
presumably for jsp or servlet examples that come with
Tomcat.
• You should have added yourself to it as admin, or looked
at it already to get an admin pw, to run the manager utility
we looked at earlier.
• I added the tomcat-users from the text chapter 8 example
file to my users.
• The web.xml file security constraint tag then specifies
which security roles may access a resource. In the DTD,
tags are ordered: <security-constraint>, <login-
config>, then <security-role>
• The text acknowledges that the web.xml does get a bit
complicated and that for handling security issues a
graphical manipulation tool (like your proj 2) might be
best.
Security constraint
• The deployment descriptor (next slide) protects the
resource (in this example, a servlet named secret) GET
and POST methods from anyone who hasn’t logged in
as manager using BASIC authentication. The rest of the
site is not restricted.
• The security constraint tag protects a web resource
collection so access is only granted for roles specified in
<auth-constraint>
• Each <web-resource-collection> contains a name, any
number of urls to protect and any number of http
methods for which access is restricted. url patterns may
use wildcard characters.
web.xml file additions for this example
<security-constraint>
<web-resource-collection>
<web-resource-name>
SecretProtection
</web-resource-name>
<url-pattern>
/servlet/SalaryServer
</url-pattern>
<url-pattern>
/servlet/secret
</url-pattern>
<http-method>
GET
</http-method>
<http-method>
POST
</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>
manager
</role-name>
</auth-constraint>
</security-constraint>
web.xml file additions
<login-config>
<auth-method>
BASIC <!– coices are: BASIC, DIGEST, FORM, CLIENT-
CERT -->
</auth-method>
<realm-name>
Default <!-- optional, only useful for BASIC -->
</realm-name>
</login-config>
<security-role>
<role-name>
manager
</role-name>
</security-role>
conf/tomcat-users.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat"/>
<user username="both" password="tomcat" roles="tomcat,role1"/>
<user username="role1" password="tomcat" roles="role1"/>
<user name="Dilbert" password="dnrc" roles="engineer" />
<user name="Wally" password="iluvalice"
roles="engineer,slacker" />
<user name="MrPointyHair" password="MrPointyHair"
roles="manager,slacker" />
</tomcat-users>
Server prompts for user info
SalaryServer gives info
Once logged in
• You may continue to access resources
here after login without having to reenter
pw.
• As mentioned in earlier ppts, the client
browser session hands the server the
name/pw each time a page is requested.
If Dilbert tries to login
Retrieving authorization information
• Servlet API supports two methods getRemoteUser() and
getAuthType() from chapter 4 for getting user
information.
• API 2.2 introduces a new method (on
HttpServletRequest) getUserPrincipal(). A principal is
the entity being authenticated, a group, a login, a
corporation.
• getRemoteUser is basically available for CGI
compatibility and we looked at this briefly in chapter 7.
getUserPrincipal() is the preferred way to authenticate a
user.
• isUserinRole() returns true only if the user is in a
particular role.
Retrieving authorization information
• These methods allow servlets to handle some of
the authentication process. Role aliases can be
used in the deployment descriptor.
• The following excerpt would enable the servlet to
use the alias mgr for the role manager. This
would be useful for integrating servlets from
different web applications. Aliases are
configured per servlet.
<servlet>…</servlet-class><security-role-
ref><role-name>mgr</role-name><role-
link>manager</role-link> </security-role-ref>
</servlet>
Getting client info
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><HEAD><TITLE>AuthenticationSnoop</TITLE></HEAD><BODY
>");
<BODY>
Sorry, your login was denied.
Please hit the Back button to try again.
</BODY></HTML>
Error page
I get null pointer exceptions trying to run the
last example… I have to revisit this example
CustomAuthorization
• A servlet can handle its own custom
authorization procedures, looking for
names/passwords in a database for
example. In a limited context, this servlet
might build a hashtable of priviledged
users:
users.put("Wallace:cheese", "allowed");
users.put("Gromit:sheepnapper", "allowed");
users.put("Penguin:evil", "allowed");
CustomAuth
CustomAuth
An invalid login yields an error
Custom Authorization with html forms
<HTML>
<TITLE>Login</TITLE>
<BODY>
<FORM ACTION=/servlet/LoginHandler METHOD=POST>
<CENTER>
<TABLE BORDER=0>
<TR><TD COLSPAN=2>
<P ALIGN=CENTER>
Welcome!<br>
Please enter your Account Number,<br>
Password, and PIN to log in.
</TD></TR>
<TR><TD>
<P ALIGN=RIGHT><B>Account:</B>
</TD>
<TD>
<P><INPUT TYPE=TEXT NAME="account" VALUE="" SIZE=15>
</TD></TR>
<TR><TD>
<P ALIGN=RIGHT><B>Password:</B>
</TD>
<TD>
<P><INPUT TYPE=PASSWORD NAME="password" VALUE="" SIZE=15>
</TD></TR>
<TR><TD>
<P ALIGN=RIGHT><B>PIN:</B>
</TD>
<TD>
<P><INPUT TYPE=PASSWORD NAME="pin" VALUE="" SIZE=15>
</TD></TR>
<TR><TD COLSPAN=2>
<CENTER>
<INPUT TYPE=SUBMIT VALUE=" OK ">
</CENTER>
</TD></TR>
</TABLE>
</FORM>
</BODY></HTML>
Custom Authorization with html forms
Custom Authorization with html forms