Spring Security


Fabric3 includes an extensible security framework that supports authentication and authorization. Authentication is typically specified as a policy intent on a binding to perform client, server, or mutual authentication. When a client is authenticated, a security subject is associated with request messages sent by the client. This security subject can then be used to authorize access to service operations based on roles.
The security provider varies by runtime. The Standalone, Maven, and Ant runtimes are configured by default to use a basic security provider. The Tomcat runtime is configured with a provider that delegates to Tomcat security realms. There is also a Fabric3 extension that uses Spring Security, which can be installed in any of the Fabric3 runtimes.

The Basic Security Provider


The Standalone runtime includes a basic security provider that allows users and roles to be statically defined in a configuration file, security.xml, located in the runtime /config directory. An example file is shown below:
<users>
<user>
<username>foo</username>
<password>bar</password>
<roles>
<role>role1</role>
<role>role2</role>
</roles>
</user>
</users>


In the Maven runtime, the same security information is configured using a systemConfig entry:
<systemConfig>
<![CDATA[
<config>
<users>
<user>
<username>foo</username>
<password>bar</password>
</user>
</users>
</config>
]]>
</systemConfig>

The Tomcat Realms Security Provider


Fabric3 integrates with Tomcat security realms in place of the basic Fabric3 provider. When using authentication and authorization policies in applications, Fabric3 will transparently delegate to Tomcat security.

The Spring Security Provider


Fabric3 includes integration with Spring Security that can be used in place of the basic security provider. The Spring Security extension supports LDAP and JDBC-based access control. Since it is dynamic in nature (users and roles can be added at runtime), it is much more powerful than the basic and Tomcat providers.

Installing the Spring Security Profile


The Spring Security provider is included in the Spring profile. When installing the profile in the standalone runtime, it is important to remove the fabric3-security-impl extension that is configured by default.

Enabling LDAP and JDBC Providers


The Spring Security provider is configured as part of the system configuration using the <spring.security> element. Configuration follows the standard Spring Security configuration format. The following illustrates how JDBC security is configured using a Fabric3 JDBC connection pool:
<datasources>
<datasource name="SecurityDS" .../>
</datasources>
<security>
<spring.security>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="SecurityDS"/>
</authentication-provider>
</authentication-manager>
</spring.security>
</security>
The next example demonstrates configuring LDAP-based security:
<security>
<spring.security>
<authentication-manager>
<ldap-server
url="ldap://localhost:1389/dc=example,dc=com"
manager-dn="cn=Directory Manager"
manager-password="password"/>
<ldap-authentication-provider
user-search-base="ou=people"
user-search-filter="uid={0}"
group-search-filter="member={0}"
group-search-base="ou=groups"/>
</authentication-manager>
</spring.security>
</security>

Custom Security Providers


The basic provider can be replaced by a more capability (and dynamic) alternative by substituting the fabric3-security-impl.jar in the extensions repository. For details on implementing an alternative provider, see the Javadoc for the org.fabric3.spi.security package in fabric3-spi.

Using Authentication


Authentication is typically enabled on a binding configuration. Please refer to the binding chapters for specific examples.

Simulating Authentication in Integration Tests


In integration test environments, it is often required to simulate authentication credentials. For example, a test client may need to supply credentials to authenticate with the secure service it tests. Fabric3 JUnit test components can be configured with authentication credentials, and those credentials propagated over a remote transport such as Web Services. The following shows how to simulate username/password credentials:
<component name="SecurityTest">
<f3:junit
class="org.fabric3.tests.spring.SecurityTest">
<configuration>
<username>scott</username>
<password>wombat</password>
</configuration>
</f3:junit>
<reference name="service" target="SCASecureService"/>
</component>

Using Authorization


The Fabric3 API includes the org.fabric3.api.annotation.security.RolesAllowed annotation, which is used to specify roles required to execute a portion of code. The RolesAllowed annotation can be placed on a method or class (in which case it will be applied to all methods contained in the class) to restrict access to security subjects with certain roles as follows:

public class SecureRolesServiceImpl implements SecureService {
@RolesAllowed({"role1", "role2"})
public void call() {
// ...
}
}

Note that the current security subject can be injected using the SCA @Context annotation on a field or setter method that takes the SCA RequestContext type. Alternatively, additional Fabric3 APIs can be accessed by using the org.fabric3.api.Fabric3RequestContext type in place of the SCA RequestContext type.

Using Authorization With Spring Beans


When installed, Fabric3 transparently associates the Spring Security provider with application contexts that are configured as SCA components. To enable authorization for Spring beans contained in those application contexts, use the Spring Security elements as in any typical Spring application. The following is an example that enables authorization on all Spring beans. Note that namespace declarations have been omitted from the <beans> element:
<beans xmlns=http://www.springframework.org/schema/beans ...>
<security:global-method-security
pre-post-annotations="enabled"/>
<sca:service name="SecureTestService"
target="SpringSecureService"/>
<bean name="SpringSecureService"
class="org.fabric3.sample.SomeSpringBean"/>
</beans>
For more information, refer to the Spring Security reference.