【发布时间】: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();
}
}
【问题讨论】:
-
移除启用网络安全
-
@spencergibb 我试过了,还是不行
-
这篇文章可以提供帮助 - stackoverflow.com/questions/65063402/…
标签: java spring spring-security spring-cloud spring-cloud-config