相关疑难解决方法(0)

如何告诉spring security仅为特定端口应用authorizeRequests?

我们以官方API在端口8080(在我们的虚拟网络之外映射到端口443上的普通HTTPS)的方式配置我们的新微服务(使用Spring-Boot),而一些管理功能在辅助HTTP上端口7979.这些只在虚拟网络中使用,用于监控,负载均衡等.

所有API访问都需要使用OAuth保护,而管理功能应该可以在网络内自由访问.所以我们以这种方式配置Spring安全性(http是一个HttpSecurity对象):

    http
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.NEVER)
        .and()
        .authorizeRequests()
            .antMatchers("/info").anonymous()
            .antMatchers("/health").anonymous()

            .antMatchers(HttpMethod.GET, "/warehouses/**").access(oauthScopeRead)
            .antMatchers(HttpMethod.PUT, "/warehouses/**").access(oauthScopeWrite)

            .anyRequest().denyAll();
Run Code Online (Sandbox Code Playgroud)

这对两个端口都有影响:/info并且/health是未经授权的,同时/warehouses需要身份验证,其他一切也需要身份验证(返回401,但是在使用身份验证调用时,它返回403).

由于没有/info/health在公共端口,这些返回404为未经授权的用户,而其他一切返回401.我对此不满意并希望有

  • 在公共端口上,要求对所有内容进行身份验证(并且仅在经过身份验证后返回404或403)
  • 在管理端口上,根本不需要身份验证(对于不是其中一个配置端点的所有内容,返回404).

我在Spring Security Javadocs或参考文档中找不到任何关于端口的信息.

我能在这做什么?

java port spring spring-security

6
推荐指数
1
解决办法
8846
查看次数

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

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

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

server.port = 8080     # -> secure
management.port = 8081 # -> unsecure
Run Code Online (Sandbox Code Playgroud)

但是,一旦我添加 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

java spring-security spring-boot

4
推荐指数
1
解决办法
3911
查看次数

标签 统计

java ×2

spring-security ×2

port ×1

spring ×1

spring-boot ×1