【发布时间】:2017-04-21 20:08:48
【问题描述】:
如何为/env, /health, /metrics 等管理端点使用基本安全性?
与其他应用程序控制器端点安全性相比,我想为上述端点使用不同的用户凭据。
在我的 application.properties 文件中,我在下面指定了应用程序控制器的安全性
但我希望管理端点使用不同的用户名/密码。找不到management.security.user.name 属性。
【问题讨论】:
标签: spring-boot
如何为/env, /health, /metrics 等管理端点使用基本安全性?
与其他应用程序控制器端点安全性相比,我想为上述端点使用不同的用户凭据。
在我的 application.properties 文件中,我在下面指定了应用程序控制器的安全性
但我希望管理端点使用不同的用户名/密码。找不到management.security.user.name 属性。
【问题讨论】:
标签: spring-boot
要实现端点基本安全,您需要使用以下代码
security.user.name=user
security.user.password=password
并且在配置文件中应该如下所示
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable().authorizeRequests().anyRequest().authenticated().and().httpBasic();
}
}
仍然无法正常工作,希望这会成功
【讨论】:
Spring 安全性在 @Bean 类型为 GlobalAuthenticationConfigurerAdapter 的实例中配置了一个“全局”AuthenticationManager。这个AuthenticationManager 是由security.user.* 属性配置的,除非您设置security.basic.enabled=false。全局AM也默认附加到管理端点,它是WebSecurityConfigurationAdapters中定义的任何“本地”AuthenticationManagers的父级(它们都是ProviderManagers)。
因此,如果您希望管理端点和应用程序端点使用不同的用户帐户,您有(至少)两种选择:
在WebSecurityConfigurationAdapter 中为您的应用程序端点定义一个本地AM,并确保该过滤器不涵盖管理端点。这很容易,因为您无需多加思考,只需将AuthenticationManagerBuilder 添加到您的WebSecurityConfigurationAdapter 即可(只要根据filter that secures the management endpoints 仔细订购)。
对应用程序端点使用全局 AM(或实际上是另一个本地)并重新配置管理端点的安全性(例如,设置 security.basic.enabled=false 并添加您自己的 WebSecurityConfigurerAdapter 覆盖管理端点)。这可能需要更多的工作,并且复制了一些引导默认值,但至少你会知道你得到了什么。
【讨论】:
Dave 已经解释得很好,但这里有一些使用 WebSecurityConfigurerAdapter 和数据库作为身份验证源的完整示例。
SecurityConfig.java
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private DataSource dataSource;
@Override
public void configure(WebSecurity web) throws Exception {
// Ignore any request that starts with /resources or /webjars
web.ignoring()
.antMatchers("/resources/**")
.antMatchers("/webjars/**");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
// for app access
http.authorizeRequests()
.antMatchers("/configuration").hasRole("ADMIN")
.antMatchers("/user").hasRole("ADMIN")
.anyRequest().fullyAuthenticated()
.and()
.exceptionHandling().accessDeniedPage("/auth_error")
.and()
.formLogin().loginPage("/login").failureUrl("/login?error").permitAll()
.and()
.logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout")).logoutSuccessUrl("/").invalidateHttpSession(true);
// for management access with basic auth
http.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/management/**").hasRole("ADMIN");
}
@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.jdbcAuthentication().dataSource(dataSource)
.passwordEncoder(new BCryptPasswordEncoder());
}
}
这是我的 application.properties
application.properties
# MANAGEMENT HTTP SERVER (ManagementServerProperties)
management.port=8081
management.address=127.0.0.1
management.context-path=/management
management.security.enabled=true
# MVC ONLY ENDPOINTS
endpoints.jolokia.path=/jolokia
endpoints.jolokia.sensitive=true
endpoints.jolokia.enabled=true
# JMX ENDPOINT (EndpointMBeanExportProperties)
endpoints.jmx.enabled=true
endpoints.jmx.domain=org.springboot
endpoints.jmx.unique-names=false
# ENDPOINT
endpoints.enabled=true
endpoints.shutdown.id=shutdown
endpoints.shutdown.sensitive=true
endpoints.shutdown.enabled=true
# HYPERMEDIA ENDPOINTS
endpoints.actuator.enabled=true
endpoints.actuator.path=/actuator
endpoints.actuator.sensitive=false
您可以从spring application properties查看更多端点属性
管理请求示例
数据库中已经添加了 ADMIN 角色用户(用户名:admin,密码:password)。
关机管理请求示例
$ curl -u admin:password -X POST http://127.0.0.1:8081/management/shutdown
{"message":"Shutting down, bye..."}
通过jolokia检查HeapMemoryUsage和ThreadCount的管理请求示例
$ curl -u admin:password http://127.0.0.1:8081/management/jolokia/read/java.lang:type=Memory/HeapMemoryUsage
{"request":{"mbean":"java.lang:type=Memory","attribute":"HeapMemoryUsage","type":"read"},"value":{"init":268435456,"committed":829947904,"max":3817865216,"used":466033000},"timestamp":1444167809,"status":200}
$ curl -u admin:password http://127.0.0.1:8081/management/jolokia/read/java.lang:type=Threading/ThreadCount
{"request":{"mbean":"java.lang:type=Threading","attribute":"ThreadCount","type":"read"},"value":47,"timestamp":1444174639,"status":200}
健康检查管理请求示例
$ curl -u admin:password http://127.0.0.1:8081/management/health
{"status":"UP","diskSpace":{"status":"UP","free":163634987008,"threshold":10485760},"db":{"status":"UP","database":"H2","hello":1}}
【讨论】: