Spring Security Whitepaper
Spring Security Whitepaper
By Richard Freedman
Overview
By default, Spring Security 2.0 (formerly ACEGI) bases
it's authorization decisions on user roles. In some
situations, it is preferable to base authorizations on
fine-grained permissions, and to treat roles as “groups
of permissions”.
System Requirements
The requirements for running the included examples are an
installed JDK (1.5 or later), and an installation of Maven. If you
need to install Maven, see http://maven.apache.org/
2
Role-Based Security Example
The Role-Based Security example is the starting point, and uses
a straightforward, “standard” Spring Security configuration. The
example includes no Java code, just configuration and JSP
pages. There are three distinct areas of the application - “public”
(available to all clients, whether logged in or not), “secured”
(available to all logged-in users), and “admin” (available only to
administrators).
The three areas of the application are protected via the following
Spring Security configuration in applicationContext-security.xml:
<http auto-config='true'>
<anonymous/>
<intercept-url pattern="/public/**"
access="IS_AUTHENTICATED_ANONYMOUSLY"/>
<intercept-url pattern="/secure/**" access="ROLE_USER, ROLE_ADMIN"/
>
<intercept-url pattern="/admin/**" access="ROLE_ADMIN"/>
<form-login
login-page='/login.jsp'
authentication-failure-url="/login.jsp?error=true"
/>
</http>
3
Links appropriate to the user's role are created in the JSP pages
using the Spring Security taglibs. For instance,
<sec:authorize ifAllGranted="ROLE_ADMIN">
<p>You are logged in as an admin</p>
<a href="<c:url value='/public'/>">public folder</a>
<br/>
<a href="<c:url value='/secure'/>">secure folder</a>
<br/>
<a href="<c:url value='/admin'/>">admin folder</a>
<br/>
</sec:authorize>
In this scenario, the groups are the roles, and the authorities are
the permissions. We create the same roles as before,
ROLE_USER and ROLE_ADMIN, but this time in the groups
table. We also create permissions for these roles – the
ROLE_USER role gets the permission
PERM_ACCESS_SECURED (permission to access “secured”
URLs), and the ROLE_ADMIN role gets this permission and the
PERM_ACCESS_ADMIN permission (permission to access
“admin” URLs). See the schema.sql file in the permission-based
security example for the modified schema and insert statements.
4
Configuration XML
There are three changes required in
applicationContext-security.xml to effect the change to
permission-based security.
The first change is to tell Spring Security that we're using the
new schema with “groups”. We add the following properties to
the “userDetailsManager” bean:
<!-- disable direct lookup of user's permissions (require lookup via group) -->
<beans:property name="enableAuthorities" value="false"/>
5
To change the authority name prefix, we have to configure a
number of beans, so that we can specify the prefix, including an
accessDecisionManager bean, a roleVoter bean, and an
authenticatedVoter bean. The authority name prefix is set as the
“rolePrefix” property of the roleVoter bean. See the
applicationContext-security.xml file for details.
JSP Taglibs
Now that the rest of the application has been converted to
permission-based security, the JSPs must be updated to check
for the permissions instead of the roles. So, for instance, the link
to the “admin” page should be changed from
<sec:authorize ifAllGranted="ROLE_ADMIN"> to
<sec:authorize ifAllGranted="PERM_ACCESS_ADMIN">
6
Summary
Spring Security 2.0 provides the ability, with just a few
configuration changes, to use fine-grained, permission-based
authorization. This fits better with most corporate security
policies, makes authorization management much easier, and
abstracts application code from individual user rights.