-1

We are deploying two war files in tomcat's webapps folder, let's say public.war and private.war

We'd like to setup HTTP Basic Authentication only for private.war (not for all deployed war files) but it seems we would have to configure this in the exploded /webapps/private/WEB-INF/web.xml (or modify the war itseld), which we'd like to avoid.

The reason for this is we are retrieving private.war from a 3rd party source and if someone for instance updates it to the latest version our changes to web.xml would be overwritten (right?).

Is there a way to enable Basic Authentication in Tomcat just for one specific context-path from let's say - something like conf/web.xml or another file?

We would especially like to avoid modifying the deployed (or exploded) war file.

  • @Jarrod The answer you are suggesting is about securing all deployed war files using Basic Authentication, my question is about only securing one specific war file or context path, not all. (I tried to do this using '/private/*' in conf/web.xml but this doesn't seem to work. '/* works fine. – Tadeus Senf Mar 23 '18 at 21:41
  • it is all or nothing, you either do it globally or you edit the single war file, or you dive into a reverse proxy like @gusto2 answered –  Mar 25 '18 at 00:25

2 Answers2

2

Is there a way to enable Basic Authentication in Tomcat just for one specific context-path

indeed for that you'd need to modify the application configuration (effectively the war file or deployed files).

We would especially like to avoid modifying the deployed (or exploded) war file

I'd suggest you to enforce the basic authentication on reverse proxy (apache httpd, nginx) if you have one

gusto2
  • 11,210
  • 2
  • 17
  • 36
-1

You can add a config in $CATALINA_HOME/conf/context.xml and add an environment in it and use it in your source or config files in war files as a ref bean, for example:

context.xml:

<Context>
  ...
  <Environment name="username" value="admin"
      type="java.lang.String"/>
  ...
</Context>

security.xml(in your war file):

<bean>
id="AuthenticationService"
    class="com.core.auth.AuthenticationService">
    <property name="username" >
        <ref bean="username"/>
    </property>

</bean>