Last active
February 1, 2018 13:53
-
-
Save chris-carneiro/96a4dbfb45b0f90c83c4e8f95481aeca to your computer and use it in GitHub Desktop.
Example of how to protect actuator endpoints with a basic auth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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