0

In our application we need to let user select the desired datasource when logging in with form-based authentication, and I'm not sure if it's at all possible when using standard form-based authentication. I heard it was possible using TextInputCallback, but have no idea how (and where) to implement it.

Sergey
  • 2,880
  • 3
  • 19
  • 29

1 Answers1

0

In the web.xml there could be maximum one <login-config> tag. It means that you cannot use more than one realm in one web application. So, you need a more or less container specific solution.

In Tomcat there is a CombinedRealm which can uses other realms.

Realm implementation that contains one or more realms. Authentication is attempted for each realm in the order they were configured. If any realm authenticates the user then the authentication succeeds. When combining realms usernames should be unique across all combined realms.

Maybe it matches with your requirements. If not and users exist in more than one realm (with the same username) you could use prefixes. For example set "domain\myuser" as the username.

If you use nested JDBCRealms you could create a database view which contains the prefixed usernames (just concat the prefix with the username) and use this view as the user table.

Another approach is removing the prefix in a custom realm and call the container's JDBCRealm (or its other realms) but it needs some coding. Anyway, it shouldn't be too hard, already existed realms probably can be used with the delegate design pattern.

palacsint
  • 28,416
  • 10
  • 82
  • 109
  • If I got your idea correctly, I need to modify the database to do this. But how about using Spring's AbstractRoutingDataSource to switch between them? It's a migration/integration project, so we can't alter the database settings nor add anything new to it. – Sergey Nov 05 '11 at 13:08
  • If you use the `CombinedRealm` and the databases don't contain the same username you don't have to create the new view. If you use the `CombinedRealm` and the databases contain the same username you can write a custom realm (without modifying the databases). I don't know anything Spring's `AbstractRoutingDataSource`. – palacsint Nov 06 '11 at 19:56