Spring 当 Spring 安全在 Spring 云配置服务器上处于活动状态时,云配置客户端未获取配置

Spring Cloud Config Client not fetching config when Spring Security is active on Spring Cloud Config Server

当我 运行 spring 没有 spring 安全性的云配置服务器时,该服务可以毫无问题地获取配置,但是当我激活 Spring 安全性时,它不会获取配置文件。它似乎引发了 401 http 错误。我已经检查过用户名和密码是否正确,我也尝试过 user:password@url 方式来验证相同的问题。

如果我直接在浏览器中访问 url http://localhost:8888/service/default 并输入用户名和密码,则会显示配置。

任何帮助将不胜感激,我不确定我的云配置或安全配置是否有问题。

Spring 引导版本:'2.2.4.RELEASE'
spring-cloud-config-server 版本:'2.2.1.RELEASE'
构建系统:Gradle
Java8

这个配置总是失败,我尝试将它添加到我现有的服务中但它不起作用所以我通过 https://start.spring.io/ 上的 spring 初始化程序创建了一个新的配置服务器和一个新的客户端下面的配置仍然不起作用。

启用安全性时记录:

2020-02-19 14:29:16.553  INFO 14996 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-02-19 14:29:16.577 DEBUG 14996 --- [           main] o.s.web.client.RestTemplate              : HTTP GET http://localhost:8888/service/default
2020-02-19 14:29:16.634 DEBUG 14996 --- [           main] o.s.web.client.RestTemplate              : Accept=[application/json, application/*+json]
2020-02-19 14:29:16.647 DEBUG 14996 --- [           main] o.s.web.client.RestTemplate              : Response 401 UNAUTHORIZED
2020-02-19 14:29:16.652  WARN 14996 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: 401 : [{"timestamp":"2020-02-19T12:29:16.642+0000","status":401,"error":"Unauthorized","message":"Unauthorized","path":"/service/default"}]

当安全性为 disabled/permit 全部时登录

2020-02-19 12:43:13.756  INFO 4972 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2020-02-19 12:43:17.563  INFO 4972 --- [           main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=service, profiles=[default], label=null, version=fb9ccb6e46098bfe425130d6447a0797206e5c2f, state=null

配置服务器application.yml文件
github uri 被遮盖,连接到私人仓库不是问题。

server:
  port: 8888

spring:
  application:
    name: config-server
  security:
    user:
      name: 'root'
      password: '1234'
  cloud:
    config:
      server:
        git:
          uri: <github-uri>
          ignore-local-ssh-settings: false
          strict-host-key-checking: false
          private-key: 'classpath:resources/id_rsa'

服务application.yml文件

spring:
  application:
    name: service
  cloud:
    config:
      uri: http://localhost:8888
      username: 'root'
      password: '1234'
      fail-fast: true

网络安全非常基础,但以下是安全配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // Secure the endpoints with HTTP Basic authentication
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").fullyAuthenticated();
        http.httpBasic().and().exceptionHandling();
    }
}

您应该为客户端应用程序使用 bootstrap.yaml(而不是 application.yaml)。

它在没有安全保护的情况下工作,只是因为您的客户端使用的是默认配置,没有用户名和密码。当您启用安全性时,它 returns 401 因为默认用户名和密码为空。

对于所有正在关注任何过时的云教程的人:- 从 springboot 2.4 开始,必须添加 bootstrap 启动器依赖项,以便按照教程使用 bootstrap.properties (bootstrap.yml)用于外部配置。

<dependency>
 <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>