如何禁用Spring Boot应用程序的管理端口安全性?

4
我正在设置一个Spring Boot 1.3安全应用程序,但带有一个对公众不可见的管理端口,因此我不需要在该端口上进行任何安全控制。
这就是我想要实现的简单目标:
server.port = 8080     # -> secure
management.port = 8081 # -> unsecure

但是一旦我添加WebSecurityConfigurerAdapter,它就会自动对两个端口生效。如果管理端口不同,设置“management.security.enabled=false”没有效果,这是一个bug吗?我怎样才能只为管理端口禁用安全性呢?
我的简单安全配置:
@Configuration
@EnableWebSecurity
static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }
}

我知道一个可能的解决方法是设置自定义上下文路径,例如/manage,并忽略此路径的安全性,但使用非标准路径似乎不是理想的选择,而且在安全配置中解析路径时需要进行调整而不是硬编码,因此我想找到是否有一种标准方法来解决这个问题。

2个回答

6

您可以始终添加请求匹配器并跳过管理端口的安全检查。Spring Boot 2 的解决方法如下。这可能对旧版本的 Spring Boot 也适用。请尝试并查看。

public class AppSecurityConfig extends WebSecurityConfigurerAdapter {

   // this is the port for management endpoints
   @Value("${management.server.port}")
   private int managementPort;

   @Override
   protected void configure(HttpSecurity http) throws Exception {
       http
            .authorizeRequests()
            .requestMatchers(checkPort(managementPort)).permitAll()
            .anyRequest().authenticated();

   }

  /**
   * This method verifies whether a request port is equal to the provided method parameter
   *
   * @param port Port that needs to be checked with the incoming request port
   * @return Returns a request matcher object with port comparison
   */
   private RequestMatcher checkPort(final int port) {
       return (HttpServletRequest request) -> port == request.getLocalPort();
   }
}

这个答案的启发。



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