Skip to content

Instantly share code, notes, and snippets.

@chris-carneiro
Last active February 1, 2018 13:53
Show Gist options
  • Save chris-carneiro/96a4dbfb45b0f90c83c4e8f95481aeca to your computer and use it in GitHub Desktop.
Save chris-carneiro/96a4dbfb45b0f90c83c4e8f95481aeca to your computer and use it in GitHub Desktop.
Example of how to protect actuator endpoints with a basic auth
import org.springframework.boot.actuate.autoconfigure.ManagementServerProperties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.core.annotation.Order;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.web.authentication.www.BasicAuthenticationEntryPoint;
/**
* Created by ChrisC on 10/07/17.
*/
@EnableWebSecurity
@Order(ManagementServerProperties.ACCESS_OVERRIDE_ORDER)
public class WebSecurityAuthenticator extends WebSecurityConfigurerAdapter {
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("password").roles("ACTUATOR");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
BasicAuthenticationEntryPoint authenticationEntryPoint = new BasicAuthenticationEntryPoint();
http.csrf().disable(); // remove for any request that could be processed by a browser by normal users
authenticationEntryPoint.setRealmName("/");
http.httpBasic().authenticationEntryPoint(authenticationEntryPoint);
http.authorizeRequests().antMatchers("/actuator/health").permitAll();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment