Giter VIP home page Giter VIP logo

spring-registry-server's Introduction

Authenticated SpringBoot App with AppInsights

App Service with "AlwaysOn" feature will ping application ROOT every 5 min, which will cause a lot of "401" failures for application with enabled authentication.

metrics

In our example we use simple Eureka Server SpringBoot application with Basic Auth. User is set in WebSecurityConfig class and password in App Service Application Settings in variable EUREKA_PASSWORD.

Unauthenticated Healthcheck endpoint

We need endpoint without authentication enabled to serve as a healthcheck url. Actuator /actuator/health endpoint is good for this purpose. To disable authentication only on this url for the application add following to WebSecurityConfig:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http
        .authorizeRequests()
             // Permit health endpoint only for local invocation
            .antMatchers("/actuator/health").hasIpAddress("127.0.0.1")
            // require AUTH for all others 
            .anyRequest().authenticated()
            .and()
        .csrf().ignoringAntMatchers("/eureka/**")
            .and()
        .httpBasic();

}

Point AlwaysOn to custom URL

By default AlwaysOn hits ROOT url of the application, it does not have configuration that could be changed. 401

We could implement a redirect that will happen only for the AlwaysOn User-Agent requests by adding a rule to web.config that redirects only those request that come from localhot ::1 and with required User-Agent:

metrics

Add rule:

<rewrite>
    <rules>
    <rule name="Rewrite AlwaysOn" stopProcessing="true">
        <match url="^$" />
        <conditions>
        <add input="{HTTP_USER_AGENT}" pattern="^AlwaysOn$" />
        <add input="{REMOTE_ADDR}" pattern="::1" />
        </conditions>
        <action type="Rewrite" url="/actuator/health" />
    </rule>
    </rules>
</rewrite>

Point Application Initialization module to custome URL

Once we repointed AlwaysOn the only 401 errors we still see in log are from the application initialization performed by IIS server

metrics

To repoint add following

<applicationInitialization remapManagedRequestsTo="/hostingstart.html"
                           skipManagedModules="true" >
      <add initializationPage="/actuator/health" />
</applicationInitialization>

While application is being initialized (and java boot app might take a while) the splash screen that app init access is set in remapManagedRequestsTo url. We need this URL to be as fast as possible and preferably just static file not coming from Java application. By default when App Service is created it creates file hostingstart.html under wwwroot and it's exactly what we could leverage. Last step is to make sure we have handler that would serve this staticfile as we configured all the urls to be server by java engine. To add static handler add following to web.config:

   <add name="StaticFileStart" path="hostingstart.html" verb="*" modules="StaticFileModule" resourceType="Either" requireAccess="Read" />

It will handle and serve the html file. And now we do not have any 401 errors in the logs and Application Insights.

metrics

spring-registry-server's People

Contributors

lenisha avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.