We're building a web application that is available to both authenticated and anonymous users. If you decide not to register/login you only have a limited set of features. User authentication is done over OpenID with Spring Security. That works fine.
However, the application also comes with an admin UI that is deployed at <host>/<context-root>/admin
. Can we have two separate realms with Spring Security (e.g. basic auth for /admin/**
)? How does that have to be configured?
Spring Security has added support for this scenario in version 3.1, which is currently available as a Release Candidate. It was implemented by SEC-1171 and details of the syntax are in the manual included with 3.1.
However it's pretty simple to use. Basically you just define multiple
http
elements in your Spring Security configuration, one for each realm. We're using it like this:The key thing to note is the
pattern="/admin/**"
on the firsthttp
element. This tells Spring that all URLs under/admin
are subject to that realm instead of the default realm — and thus URLs under/admin
use basic authentication instead.Possible solution:
/admin
the requires "ROLE_ADMIN"org.springframework.security.web.authentication.www.BasicAuthenticationFilter
to intercept the/admin
URL and authenticate user as ROLE_ADMIN if it provides the appropriate credentialsSample configuration:
Note: default implementation of BasicAuthenticationFilter is a passive filter, i.e. it just looks for a basic auth header in the request and if it is not present - does nothing. If you want the filter to explicitly require the basic authentication from the client, you need to extend the default implementation to commence to authentication entry point:
In addition, you need to tweak the filter to apply to
/admin
URL only - either by hard-coding this indoFilter
method or by providing an appropriate wrapper bean.I can't think of a straight forward way to have two realms (and I didn't try it myself):
you may define two filters in your web.xml where each of those has a different spring configuration and ergo an own environment. The global things go into the app config, the realm-specific in the filter config.
if it's only for a different auth method, you could write your own filter which then decides which filter to call.