Sitemap

Springboot 3 + spring-boot-starter-actuator 3 + Basic Authentication

Aug 17, 2023

--

We are newbies for Springboot. Our requirement is any work service should have basic authentication but actuator/health. Here is our works:-

  • spring.config.location (.yml)
spring:
security:
user:
name: admin
password: admin123

management:
web:
exposure:
include: health
  • SpringSecurityConfig.java
@Configuration
@EnableWebSecurity
public class SpringSecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.csrf(csrf -> csrf.disable())
.authorizeHttpRequests(
authorize -> authorize
.requestMatchers(EndpointRequest.to("health")).permitAll()
.anyRequest().authenticated()
)
.httpBasic(Customizer.withDefaults());

return http.build();
}
}ja

When we request to http://localhost:7001/actuator/health without any authentication. We get HTTP status 200 and {“status”: “UP”}.

Get Paween Pongthong’s stories in your inbox

Join Medium for free to get updates from this writer.

When we request to http://localhost:7001/service1 without any authentication. We get HTTP status 401. But if we put basic authentication “admin:admin123”. The service responds HTTP 200 as we need.

We hope this story will guide you to set Springboot authentication.

Enjoy!

--

--

Paween Pongthong
Paween Pongthong

Written by Paween Pongthong

Software Developer, Gamer, Coffee Brewer