Spring Boot管理端点基本安全

9

如何为管理端点(例如/env、/health、/metrics)使用基本安全性?我想与其他应用程序控制器端点的安全性不同,为上述端点使用不同的用户凭据。

在我的application.properties文件中,我为应用程序控制器安全性指定了以下内容:

security.user.name=user
security.user.password=password

但是,我希望管理端点有不同的用户名/密码。找不到management.security.user.name属性。

3个回答

3

为实现端点基本安全性,您需要使用以下代码

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();
    }
}

如果还是无法正常工作,请希望这个方法可以解决问题

基本身份验证


2
大卫已经解释得很好了,但是这里有一个使用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应用程序属性中查看更多端点属性。

管理请求示例:

已在数据库中添加了管理员角色用户(用户名:admin,密码:password)。

  • The example management request for shutting down

    $ curl -u admin:password -X POST http://127.0.0.1:8081/management/shutdown
    {"message":"Shutting down, bye..."}
    
  • The example management request for checking HeapMemoryUsage and ThreadCount via jolokia

    $ 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}
    
  • The example management request for checking health

    $ 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}}
    

2
Spring Security有一个“全局”的AuthenticationManager配置在类型为GlobalAuthenticationConfigurerAdapter@Bean实例中。这个AuthenticationManager是由security.user.*属性配置的,除非您设置了security.basic.enabled=false。默认情况下,全局AM也附加到管理端点,并且它是在WebSecurityConfigurationAdapters中定义的任何“本地”AuthenticationManagers(它们都是ProviderManagers)的父级。
因此,如果您想要针对管理端点和应用程序端点使用不同的用户帐户,则至少有两种选择:
在`WebSecurityConfigurationAdapter`中为应用程序端点定义一个本地的`AM`,并确保管理端点不受该过滤器的影响。这很容易实现,只需在`WebSecurityConfigurationAdapter`中添加一个`AuthenticationManagerBuilder`(只要它与保护管理端点的过滤器有正确的顺序)。对于应用程序端点,使用全局的`AM`(或者另一个本地的`AM`),并重新配置管理端点的安全性(例如设置`security.basic.enabled=false`并添加自己的`WebSecurityConfigurerAdapter`来覆盖管理端点)。这可能需要更多的工作,并且会重复一些Boot的默认设置,但至少你会知道你得到了什么。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接