【问题标题】:Spring Cloud Config Client not fetching config when Spring Security is active on Spring Cloud Config Server当 Spring Security 在 Spring Cloud Config Server 上处于活动状态时,Spring Cloud Config Client 未获取配置
【发布时间】:2020-06-03 12:32:39
【问题描述】:

当我在没有 spring security 的情况下运行 spring cloud config server 时,服务会毫无问题地获取配置,但是当我激活 Spring security 时,它不会获取配置文件。它似乎引发了 401 http 错误。我检查了用户名和密码是否正确,我也尝试了 user:password@url 的身份验证方式,同样的问题。

如果我直接在浏览器中访问 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"}]

安全性被禁用/全部允许时记录

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 被隐藏了,连接到私有 repo 不是问题。

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'

服务应用程序.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();
    }
}

【问题讨论】:

标签: java spring spring-security spring-cloud spring-cloud-config


【解决方案1】:

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

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

【讨论】:

  • 你的回答太棒了。我是 Spring Cloud 的新手,所以在教程中没有提到 bootsrap.yml。为遇到此问题的其他任何人添加指向此答案的链接以获取更多信息。 stackoverflow.com/questions/32997352/…
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-02-18
    • 1970-01-01
    • 2017-06-23
    • 2018-03-22
    • 1970-01-01
    • 2019-03-20
    • 2021-05-25
    • 2016-07-06
    • 1970-01-01
    相关资源
    最近更新 更多