简体   繁体   English

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

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

When I run spring cloud config server without spring security the service fetches the config without issue but when I activate Spring security it won't fetch the config files.当我在没有 spring security 的情况下运行 spring cloud config server 时,该服务可以毫无问题地获取配置,但是当我激活 Spring security 时,它不会获取配置文件。 It seems to throw a 401 http error.它似乎引发了 401 http 错误。 I have checked that the username and password is correct, I have also tried the user:password@url way of authenticating with the same issue.我已经检查过用户名和密码是否正确,我也尝试过使用 user:password@url 进行身份验证的方式,遇到同样的问题。

If i access the url http://localhost:8888/service/default directly in browser and enter the username and password the configs are displayed.如果我直接在浏览器中访问 url http://localhost:8888/service/default并输入用户名和密码,则会显示配置。

Any help will be appreciated, I am not sure if there is an issue with my cloud config or my security config.任何帮助将不胜感激,我不确定我的云配置或我的安全配置是否有问题。

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

This config always fails, I tried adding it to existing services I had and it did not work so I created a new config server and a new client via the spring initializer on https://start.spring.io/ with the below config and still does not work.这个配置总是失败,我尝试将它添加到我拥有的现有服务中,但它不起作用,所以我通过https://start.spring.io/上的 spring 初始值设定项创建了一个新的配置服务器和一个新的客户端,并使用以下配置和仍然不起作用。

Log when security is active:安全激活时记录:

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"}]

Log when Security is disabled/permit all当安全被禁用/允许所有时记录

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

config server application.yml file配置服务器 application.yml 文件
github uri is obscured, connection to the private repo is not the issue. 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'

service application.yml file服务应用程序.yml 文件

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

The web security is very basic but below is the security config:网络安全非常基础,但下面是安全配置:

@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();
    }
}

For everyone who is following any outdated cloud tutorials:- from springboot 2.4 bootstrap starter dependency has to be added, to follow along the tutorial to use bootstrap.properties (bootstrap.yml) for external configuration.对于正在关注任何过时云教程的每个人:- 必须添加 springboot 2.4 bootstrap starter 依赖项,按照教程使用 bootstrap.properties (bootstrap.yml) 进行外部配置。

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

You should use bootstrap.yaml (not application.yaml ) for the client app.您应该为客户端应用application.yaml使用bootstrap.yaml (而不是application.yaml )。

It works without security only because your client is using default config, which doesn't have username and password.它在没有安全性的情况下工作,只是因为您的客户端使用的是默认配置,没有用户名和密码。 When you enable security it returns 401 because default username and password are empty.当您启用安全性时,它会返回 401,因为默认用户名和密码为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM