【发布时间】:2019-05-10 21:58:39
【问题描述】:
我正在尝试保护我的 Spring 云配置服务器。作为第一步,我遵循了非常简单的教程。配置服务器无需授权即可正常工作。但是当我想使用非常简单的基本身份验证时,问题就开始了。我在属性 spring.security.user.name 和 spring.security.user.password 中定义了我自己的用户名和密码,但该服务似乎不适用于这些.
当我离开这些属性并使用默认的“用户”和生成的密码时,一切都很好。我现在被这个问题困扰了一段时间,并试图解决。
这是我的配置服务器 pom.xml 文件的重要部分:
<artifactId>tao-elszamolas-config</artifactId>
<packaging>jar</packaging>
<name>tao-elszamolas-config</name>
<description>Some config service</description>
<parent>
<groupId>com.mycompany.tao</groupId>
<artifactId>tao-elszamolas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- Spring Cloud config server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- Spring Cloud Eureka client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Spring Security -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
这是配置服务器application.yml:
server:
port: 9001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: file://${user.home}/application-config
clone-on-start: true
security:
user:
name: configUser
password: configPassword
eureka:
client:
serviceUrl:
defaultZone: http://localhost:9002/eureka/
配置客户端基本上是发现服务。 (我想实现其他微服务使用的发现优先模型。)这是配置客户端服务(在我的例子中是发现服务)的 pom.xml 的重要部分:
<name>tao-elszamolas-discovery</name>
<description>Some service discovery</description>
<parent>
<groupId>com.mycompany.tao</groupId>
<artifactId>tao-elszamolas</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<!-- Spring Cloud Netflix Eureka service registry server -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<!-- Spring Cloud config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
这是配置客户端服务的 bootstrap.yml:
spring:
application:
name: discovery-server
cloud:
config:
name: discovery-server
uri: http://localhost:9001
username: configUser
password: configPassword
所以,如果我从配置服务器的 application.yml 中删除用户名和密码,并使用默认的 user 作为用户名,并在 bootstrap.yml,然后一切正常。
有人知道为什么自定义用户名和密码不起作用吗?
任何帮助将不胜感激。我可能犯了一个非常微不足道的错误,只是没有真正认识到它。
【问题讨论】:
-
更新这一行 -> 密码:{noon}configPassword @bkk
-
我试过这个,但没有成功。我很不确定我应该怎么做,所以我尝试了所有可能的组合。但仍然没有成功。只有生成的密码有效。
-
分享你的回购...
-
您的第一个问题和答案做得很好!欢迎并继续努力。
-
@JonathanJohx:这个软件不在 Git 中。然而。当我谈到可以分享的质量时,它将被推送到 GitHub。 :-)
标签: spring spring-boot basic-authentication spring-cloud-config