Authorization with Resin
Resin

Change Log
Documentation

Orientation
Features
Installation
Configuration
Web Applications
JSP
Servlets and Filters
Databases
Admin (JMX)
Security
XML and XSLT
XTP
Resources (JNDI)
Performance
Protocols
Third-party
Troubleshooting

Authentication
Digest Passwords
Authorization
SSL
Security Manager
Malicious Attacks
FAQ
Scrapbook
Tutorials
Digest Passwords
Security
SSL

Authorization is used to mark sections and resources of a web site that have limited access. Constraints are used to indicate the criteria for access, typically the constraint is based on a user login, but it can also include such things as limiting access to clients from a certain ip address and requiring that a secure transport such as SSL is in use.

  1. security-constraint
  2. web-resource-collection
  3. auth-constraint
  4. ip-constraint
  5. user-data-constraint
  6. transport-guarantee
  7. constraint
  8. Custom Security Constraints
  9. Protecting static files from viewing by anyone
    1. Place files in WEB-INF
    2. Security constraint requiring role nobody
    3. A servlet that returns a 403 error

security-constraint

Selects protected areas of the web site. Sites using authentication as an optional personalization feature will typically not use any security constraints. Sites using authentication to limit access to certain sections of the website to certain users will use security constraints.

Security constraints can also be custom classes.

Protecting all pages for logged-in users
<security-constraint>
  <web-resource-collection>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
  <auth-constraint role-name='user'/>
</security-constraint>

web-resource-collection

Specifies a collection of areas of the web site.

url-patternurl patterns describing the resource
http-methodHTTP methods to be restricted.

auth-constraint

Requires that authenticated users fill the specified role. In Resin's JdbcAuthenticator, normal users are in the "user" role. Think of a role as a group of users.

role-nameRoles which are allowed to access the resource.

Protecting webdav for webdav users
<security-constraint>
  <auth-constraint role-name='webdav'/>

  <web-resource-collection>
    <url-pattern>/webdav/*</url-pattern>
  </web-resource-collection>
</security-constraint>

ip-constraint2.0.6

Requires that the remote address is in an IP network. ip-constraint is very useful for protecting administration resources to an internal network.

Admin pages allowed in 192.168.17.0/24
<security-constraint>
  <ip-constraint>192.168.17.0/24</ip-constraint>

  <web-resource-collection>
    <url-pattern>/admin/*</url-pattern>
  </web-resource-collection>
</security-constraint>

user-data-constraint

Restricts access to secure transports, i.e. SSL.

transport-guaranteeRequired transport properties. NONE, INTEGRAL, and CONFIDENTIAL are allowed values.

<security-constraint>
  <user-data-constraint>
    <transport-guarantee>CONFIDENTIAL</transport-guarantee>
  </user-data-constraint>

  <web-resource-collection>
    <url-pattern>/*</url-pattern>
  </web-resource-collection>
</security-constraint>

transport-guarantee

Restricts access to secure transports, i.e. SSL.

constraintResin 2.0.1

Defines a custom constraint. The custom constraint specifies a <bean-class> which extends class com.caucho.server.security.AbstractConstraint . Bean-style initialization is used to initialize the constraint.

...
<security-constraint>
  <constraint>
    <bean-class>example.CustomSecurity</bean-class>
    <init>
      <policy>strict</policy>
    </init>
  </constraint>
  <web-resource-collection url-pattern='/*'/>
</security-constraint>
...

Custom Security Constraints

Any custom security constraint is checked after any authentication (login) but before any filters or servlets are applied. The security constraint will return true if the request is allowed and false if it's forbidden. If the request is forbidden, it's the constraint's responsibility to return an error page.

package qa;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.caucho.server.security.*;

public class CustomSecurity extends AbstractConstraint {
  private String foo = "false";

  public void setFoo(String foo)
  {
    this.foo = foo;
  }

  public boolean needsAuthentication()
    return false;
  }

  public boolean isAuthorized(HttpServletRequest request,
                              HttpServletResponse response,
                              ServletContext application)
    throws ServletException, IOException
  {
    if (foo.equals(request.getParameter("test")))
      return true;

    response.sendError(response.SC_FORBIDDEN);

    return false;
  }
}

The needsAuthentication method tells Resin that it needs to log in the user before checking the authorization. This would allow the custom authorizer to check user roles or the user principle for the proper permissions.

<constraint resin:type="qa.CustomSecurity">
  <foo>test-value</foo>
</constraint>

Protecting static files from viewing by anyone

Sometimes it is necessary to protect files from being viewed by anyone, such as configuration files used in your code but not meant to be served to a browser.

Place files in WEB-INF

Place files in WEB-INF or a subdirectory of WEB-INF. Any files in WEB-INF or it's subdirectories will automatically be protected from viewing.

Security constraint requiring role nobody

Use a security constraint that requires a role that nobody will ever have.

security-constraint to protect static files
<web-app>
  ...
  <!-- protect all .properties files -->
  <security-constraint>
    <web-resource-collection>
      <url-pattern>*.properties</url-pattern>
    </web-resource-collection>
    <auth-constraint role-name='nobody'/>
  </security-constraint>

  <!-- protect the config/ subdirectory -->
  <security-constraint>
    <web-resource-collection>
      <url-pattern>/config/*</url-pattern>
    </web-resource-collection>
    <auth-constraint role-name='nobody'/>
  </security-constraint>
  ...
</web-app>

A servlet that returns a 403 error

Use a simple servlet that returns a 403 error, which means "Forbidden".

servlet to protect static files - WEB-INF/web.xml
<web-app>
  ...
  <servlet>
    <servlet-name>forbidden</servlet-name>
    <servlet-class>example.servlets.Forbidden</servlet-class>
  </servlet>

  <servlet-mapping url-pattern="*.properties" servlet-name="forbidden"/>
  <servlet-mapping url-pattern="/config/*" servlet-name="forbidden"/>
  ...
</web-app>

servlet to protect static files - WEB-INF/classes/example/servlets/Forbidden.java
package example.servlets;

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.IOException;

/**
 * Respond with a 403 error
 */
public class Forbidden extends GenericServlet {
  public void service(ServletRequest request, ServletResponse response)
    throws ServletException, IOException
  {
    HttpServletResponse res = (HttpServletResponse) response;
    res.sendError(403);
  }
}


Digest Passwords
Security
SSL
Copyright © 1998-2003 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.