Rework security autoconfiguration
This commit combines security autoconfigurations for management endpoints and the rest of the application. By default, if Spring Security is on the classpath, it turns on @EnableWebSecurity. In the presence of another WebSecurityConfigurerAdapter this backs off completely. A default AuthenticationManager is also provided with a user and generated password. This can be turned off by specifying a bean of type AuthenticationManager, AuthenticationProvider or UserDetailsService. Closes gh-7958
This commit is contained in:
@@ -18,10 +18,21 @@ package sample.actuator.log4j2;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.security.core.userdetails.User;
|
||||
import org.springframework.security.core.userdetails.UserDetailsService;
|
||||
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleActuatorLog4J2Application {
|
||||
|
||||
@Bean
|
||||
public UserDetailsService userDetailsService() throws Exception {
|
||||
InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
|
||||
manager.createUser(User.withUsername("user").password("password").roles("USER").build());
|
||||
return manager;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SampleActuatorLog4J2Application.class, args);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,2 @@
|
||||
endpoints.shutdown.enabled=true
|
||||
|
||||
management.security.enabled=false
|
||||
endpoints.all.web.enabled=true
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package sample.actuator.log4j2;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.junit.Rule;
|
||||
@@ -63,10 +65,17 @@ public class SampleActuatorLog4J2ApplicationTests {
|
||||
|
||||
@Test
|
||||
public void validateLoggersEndpoint() throws Exception {
|
||||
this.mvc.perform(get("/application/loggers/org.apache.coyote.http11.Http11NioProtocol"))
|
||||
this.mvc.perform(get("/application/loggers/org.apache.coyote.http11.Http11NioProtocol")
|
||||
.header("Authorization", "Basic " + getBasicAuth()))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().string(equalTo("{\"configuredLevel\":\"WARN\","
|
||||
+ "\"effectiveLevel\":\"WARN\"}")));
|
||||
}
|
||||
|
||||
private String getBasicAuth() {
|
||||
return new String(Base64.getEncoder()
|
||||
.encode(("user:password").getBytes()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user