【问题标题】:How to disable security on management port for Spring Boot app?如何禁用 Spring Boot 应用程序管理端口的安全性?
【发布时间】:2015-12-15 13:17:24
【问题描述】:

我正在设置一个 Spring Boot 1.3 安全应用程序,但有一个公众无法访问的管理端口,因此我不需要在此端口上进行任何安全保护。

这正是我想要实现的目标:

server.port = 8080     # -> secure
management.port = 8081 # -> unsecure

但只要我添加了 WebSecurityConfigurerAdapter,它就会自动对两个端口生效。如果管理端口不同,设置management.security.enabled=false 无效,这是一个错误吗?我如何才能仅禁用管理端口的安全性?

我的简单安全配置:

@Configuration
@EnableWebSecurity
static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().anyRequest().authenticated();
    }
}

我知道可能的解决方法是设置自定义上下文路径,例如。 /manage 并从安全性中忽略此路径,但是使用非标准路径加上摆弄以将路径解析到安全配置中而不对其进行硬编码似乎并不理想,所以我想知道是否有一个标准的方法。

【问题讨论】:

    标签: java spring-security spring-boot


    【解决方案1】:

    您始终可以添加请求匹配器并跳过管理端口的安全检查。 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();
       }
    }
    

    灵感来自this answer

    【讨论】:

      【解决方案2】:

      看起来它实际上是一个错误:https://github.com/spring-projects/spring-boot/issues/4624

      【讨论】:

        猜你喜欢
        • 2018-12-05
        • 2017-01-13
        • 1970-01-01
        • 2011-09-17
        • 2014-07-16
        • 2020-06-11
        • 2021-07-24
        • 2016-05-03
        相关资源
        最近更新 更多