Category Archives: How To

User Impersonation with Spring Security SwitchUserFilter

A common requirement for secured applications is that admin / super users are able to login as any other user. For example, it may be helpful for a customer support analyst to access a system as if they were a real specific customer. The obvious way to do this is for the admin user to ask for the customer’s password or look it up in the password database. This is usually an unacceptable security compromise – no one should know a customer’s password except for the customer. And if the password database is implemented correctly it should be technically impossible for anyone – not even a system admin or DBA – to discover a user’s password.

An alternative solution is to allow admin users to login with their own unique username and password but allow them to then impersonate any other user. After the admin user has logged in, they can enter the username of another user and then view the application as if they were logged in as that user. Implementing user impersonation in this way also has the advantage that the system knows who has really logged in. If the system has an audit log, we can audit actions against the real admin user, rather than the impersonated user.

Testing with mock users in Spring / Spring MVC

A common unit test scenario for Spring / Spring MVC applications is to verify behavior when logged in as a particular user. The new spring-security-test library available with Spring Security version 4 makes testing user access controls in Spring and Spring MVC applications far simpler.

Building Unit Test Data

In the book Growing Object-Oriented Software, Guided by Tests, authors Steve Freeman and Nat Pryce suggest a neat pattern for cleanly creating test data for unit tests. They suggest using the builder pattern to build test objects which are as simple or as complicated as necessary for the test. The builder can set default data in fields meaning that only data significant to the result of the test needs to be set.

Deploying to Tomcat 7 with Maven

It may be desirable to have Maven automatically redeploy the application(s) to a test server on builds. This may be useful in a Continuous Integration (CI) scenario where we want some system running the latest build.
In this example, we want Tomcat updated with the latest build whenever we put an updated build in the Maven repository.

MockMVC to test Spring MVC form validation

The MockMVC class allows tests to be run against a real Spring application context without actually having to run the complete application in a Servlet container

Spring MVC Maven Archetype

In less than five minutes and just three Maven commands, I was able to create, compile and run a fully working example Spring MVC application.

Migrating from SVN to Git

This post documents the steps to migrate from SVN files and revision history to Git, specifically in a Windows environment.

Integration Tests with Selenium and tomcat7-maven-plugin

To create an automated integration test of my spanners-struts webapp, I need a way of launching it as part of the Maven build process.

Generate database schema DDL from Hibernate hbm mappings

The hibernate3-maven-plugin can be used to create a schema DDL from Hibernate mapping files