Skip to content
Advertisement

Spring Boot with container security

I’ve been using spring boot for some projects lately and I really like it. For a new project, we’d like to use tomcat-users.xml for really basic authentication, but I can’t figure out how to use the mechanism without a web.xml file. Most people using spring boot seem to be using spring security.

Is it possible to use tomcat container security with the spring boot java config model? I understand this breaks the runnable jar paradigm but we’re planning to deploy this as a war anyway.

Advertisement

Answer

As the Spring Security Project, I’d recommend using Spring Security as it does more than container Security (i.e. protection against common exploits, method security to provide defense in depth, etc). However, if you want to use container security you can as shown below:

You can use the following to use container managed security a Spring Boot 2.7.x or 3.0.0 Application:

Create a file at src/main/resources/tomcat-users.xml:

JavaScript

Create the following Spring Configuration:

JavaScript

Here is an automated test you can use:

JavaScript

No XML

If you do not want to use the xml file you can also manage your users pragmatically. First create a Tomcat Realm with the following:

JavaScript

Now in ContainerSecurityConfiguration instead of using MemoryRealm use ProgramaticMemoryRealm:

JavaScript

NOTE: We use UserInformation because in Spring Boot 3+ the GenericPrincipal password is not accessible and the constructor with the password is deprecated. In Spring Boot 2.7 UserInformation is not really necessary since the password is fully available on GenericPrincipal.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement