据我所知,Spring Cloud Config Server可以使用用户名和密码进行保护,用户名和密码必须由访问客户端提供。
如何阻止客户端存储这些用户名和 密码作为客户端bootstrap.yml文件中的明文 申请/服务?
答案 0 :(得分:3)
非常基本的“基本身份验证”(来自此处https://github.com/spring-cloud-samples/configserver)
您可以通过在Spring Security上添加额外的依赖项来添加HTTP Basic身份验证(例如,通过spring-boot-starter-security)。用户名是“user”,启动时在控制台上打印密码(标准Spring Boot方法)。如果使用maven(pom.xml
):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
如果您想要自定义用户/密码对,则需要在服务器配置文件中指明
security:
basic:
enabled: false
并在代码中添加此最小类(BasicSecurityConfiguration.java
):
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
//@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class BasicSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Value("#{'${qa.admin.password:admin}'}") //property with default value
String admin_password;
@Value("#{'${qa.user.password:user}'}") //property with default value
String user_password;
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.inMemoryAuthentication()
.withUser("user").password(user_password).roles("USER")
.and()
.withUser("admin").password(admin_password).roles("USER", "ACTUATOR");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.csrf()
.disable()
.httpBasic()
.and()
.authorizeRequests()
.antMatchers("/encrypt/**").authenticated()
.antMatchers("/decrypt/**").authenticated()
//.antMatchers("/admin/**").hasAuthority("ROLE_ACTUATOR")
//.antMatchers("/qa/**").permitAll()
;
}
}
@Value(“#{'$ {qa.admin.password:admin}'}”)允许在属性配置文件,环境变量或命令行中定义密码。
例如(application.yml
):
server:
port: 8888
security:
basic:
enabled: false
qa:
admin:
password: adminadmin
user:
password: useruser
management:
port: 8888
context-path: /admin
logging:
level:
org.springframework.cloud: 'DEBUG'
spring:
cloud:
config:
server:
git:
ignoreLocalSshSettings: true
uri: ssh://git@gitlab.server.corp/repo/configuration.git
这适合我。
编辑:您可以将基本用户配置直接放在application.yaml
:
security:
basic:
enabled: true
path: /**
ignored: /health**,/info**,/metrics**,/trace**
user:
name: admin
password: tupassword
答案 1 :(得分:3)
对我有用的基本身份验证配置。
服务器端:
需要的剂量:org.springframework.boot:spring-boot-starter-security
bootstrap.yml
server:
port: 8888
spring:
cloud:
config:
server:
git:
uri: git@bitbucket.org:someRepo/repoName.git
hostKeyAlgorithm: ssh-rsa
hostKey: "general hostKey for bitbucket.org"
security:
user:
name: yourUser
password: yourPassword
客户端:
bootstrap.yml
spring:
application:
name: config
profiles:
active: dev
cloud:
config:
uri: http://localhost:8888
username: yourUser
password: yourPassword
management:
security:
enabled: false
来源:Spring doc security feautres,Spring cloud config client security
答案 2 :(得分:1)
加密文本可以放在bootstrap.yml。
中检查 - &gt; http://projects.spring.io/spring-cloud/spring-cloud.html#_encryption_and_decryption