How to access endpoint without token in spring boot?

Viewed 154

I am using an actuator for getting server health, also I am doing validation on the token on each request, now I need to put a token to access actuator health but I want to access actuator health without using the token and without affecting API endpoint with token!

Note: My Actuator working fine with the token.

also, I implement the OncePerRequestFilter class for validating the firebase token for each request.

1 Answers

You can create a custom SecurityConfiguration where you permit access to actuator requests:

@Configuration
@EnableWebSecurity
class SecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity http) throws Exception {
        http     
        .authorizeRequests()
        .requestMatchers("/actuator/**").permitAll()
        .anyRequest().authenticated();
        
    }

}

You may want to read the spring-boot-docs for more information.


EDIT: When using OncePerRequestFilter you could implement the shouldNotFilter method and check for actuator paths there:

@Override
protected boolean shouldNotFilter(HttpServletRequest request)
  throws ServletException {
    String path = request.getRequestURI();
    return path.startsWith("/actuator");
}
Related