当 Spring Security 在 Spring Cloud Config Server 上处于活动状态时,Spring Cloud Config Client 无法获取配置

Nec*_*rne 3 java spring spring-security spring-cloud spring-cloud-config

当我在没有 Spring Security 的情况下运行 Spring Cloud 配置服务器时,该服务可以毫无问题地获取配置,但是当我激活 Spring Security 时,它不会获取配置文件。似乎抛出了 401 http 错误。我已检查用户名和密码是否正确,我还尝试了 user:password@url 的身份验证方式,但存在同样的问题。

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

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

Spring Boot 版本:'2.2.4.RELEASE'
spring-cloud-config-server 版本:'2.2.1.RELEASE'
构建系统:Gradle
Java 8

此配置总是失败,我尝试将其添加到我拥有的现有服务中,但它不起作用,因此我通过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"}]
Run Code Online (Sandbox Code Playgroud)

当安全性被禁用/允许所有时记录

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
Run Code Online (Sandbox Code Playgroud)

配置服务器 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'
Run Code Online (Sandbox Code Playgroud)

服务application.yml文件

spring:
  application:
    name: service
  cloud:
    config:
      uri: http://localhost:8888
      username: 'root'
      password: '1234'
      fail-fast: true
Run Code Online (Sandbox Code Playgroud)

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

@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();
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 6

对于遵循任何过时云教程的每个人:- 从 springboot 2.4 开始,必须添加 bootstrap starter 依赖项,才能按照教程使用 bootstrap.properties (bootstrap.yml) 进行外部配置。

<dependency>
 <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)