保护执行器端点,除了/health之外,可以使用Spring Security进行配置。下面是一个示例代码:
@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/health").permitAll()
.anyRequest().authenticated()
.and()
.httpBasic()
.and()
.csrf().disable();
}
}
在上述代码中,我们通过configure(HttpSecurity http)
方法配置了Spring Security的行为。antMatchers("/health").permitAll()
指定了/health端点可以被所有人访问,.anyRequest().authenticated()
则要求对其它所有端点的访问都需要进行认证。
此外,.httpBasic()
配置了基本认证方式,.csrf().disable()
禁用了跨站请求伪造保护。
最后,将上述代码配置为一个Spring配置类,让它被Spring容器扫描到即可生效。