Spring boot 2.1.x how to secure Actuator end points with basic auth

狂风中的少年 提交于 2021-01-28 04:26:45

问题


I am trying to build a spring boot application and wanted to leverage the Actuator features, but I want to secure the end points of Actuator /health,/shutdown etc. I have the below configurations which does not seem to work. I.e., application never prompts for credentials or when hit from post man does not give 403. I tried various ways, even the one from spring documentation. Can any one please help with this. Again this is spring boot 2.1.x. I know there is a configuration that can be made in application.yml in the previous version of spring

@Configuration
public class ActuatorSecurity extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.authorizeRequests()
                .requestMatchers(EndpointRequest.to(ShutdownEndpoint.class, InfoEndpoint.class, HealthEndpoint.class,
                        MetricsEndpoint.class))
                .hasRole("ENDPOINT_ADMIN").requestMatchers(PathRequest.toStaticResources().atCommonLocations())
                .authenticated().and().httpBasic();
    }

application.yml

spring:
  security:
    user:
      name: admin
      password: admin
      roles:
      - ENDPOINT_ADMIN

management:   
  endpoints:  
    web:
      exposure:
        include: "*"
  endpoint:
    shutdown:
      enabled: true
    health:
      show-details: when-authorized
      roles:
      - ENDPOINT_ADMIN
    mappings:
      enabled: true

回答1:


This code can serve you as a reference to achieve BasicAuth for Actuator Endpoints Spring Boot 2.X. This is not the exact code. While Extending WebSecurityConfigurerAdapter you have to configure AuthenticationManagerBuilder to assign roles and passwords for the roles. Here I am using "noop" password encoder you can use a different one to provide more security.

protected void configure(AuthenticationManagerBuilder auth) throws Exception {
                            
                auth.inMemoryAuthentication().withUser("ROLE_USER").password("{noop}" + "USER_PWD").roles("USER").and()
                .withUser("ROLE_ADMIN").password("{noop}" + "ADMIN").roles("ADMIN", "USER");
    }

Once AuthenticationManagerBuilder is configured now configure HttpSecurity by disabling csrf. Below code requires authentication for metrics alone using any role. You can customize according to the end points you need to authenticate. Make sure you exclude base url of Rest Controller from this Authentication. You can insert authorizeRequests().antMatchers("/baseurl").permitAll().and() in the below configuration code to achieve that. Below is an example to configure HttpSecurity.

protected void configure(Httpsecurity http) {
    http.csrf().authorizeRequests().requestMatchers(EndpointRequest.to(MetricsEndpoint.class))
    .hasANyRole("ADMIN","USER").and().authorizeRequests().and().httpBasic();
}


来源:https://stackoverflow.com/questions/54716532/spring-boot-2-1-x-how-to-secure-actuator-end-points-with-basic-auth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!