-
Security 官方文档
- 前言
Spring Cloud Config分成Spring Cloud Config Server 和 Spring Cloud Config Client
-
Spring Cloud Config Server 安全
-
前言
非常类似eureka server安全,也就是用户认证,之前也提过eureka的安全认证的教程
- 操作
复制microserver-config-server<最简单的demo>重命名为microserver-config-server-security
- 按照文档添加依赖
To use the default Spring Boot configured HTTP Basic security, just include Spring Security on the classpath (e.g. through
spring-boot-starter-security
). The default is a username of "user" and a randomly generated password, which isn’t going to be very useful in practice, so we recommend you configure the password (viasecurity.user.password
) and encrypt it (see below for instructions on how to do that).
pom.yml<用户名是user,密码是随机值,然后继续访问http://localhost:8080/master/foobar-production.yml>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
- 主启动类AuthcApplication.java
@EnableConfigServer
@SpringBootApplication
public class AuthcApplication {
public static void main(String[] args) {
SpringApplication.run(AuthcApplication.class, args);
}
}
- 动态操作图如下
- 复制eureka的加密格式,到新创建的项目文件
问:商品图片配置的访问结果,访问失败,为什么呢?
-
config server如何连接config client
- microserver-config-client-authc之pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
</dependencies>
复制microserver-config-client,重命名为microserver-config-client-authc,那么他的applicaiton.yml是怎么写呢?也跟别的服务连接eureka一样!!
- microserver-config-client-authc之bootstrap.yml
spring:
application:
name: foobar
cloud:
config:
uri: http://user:[email protected]:8080 # 仅仅增加部分“user:password123”
profile: dev
label: master # 当configserver的后端存储是Git时,默认是master
- microserver-config-client-authc之applicaiton.yml
server:
port: 8081
profile: abc
- microserver-config-client-authc之主启动类
@SpringBootApplication
public class ConfigClientAuthcApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientAuthcApplication.class, args);
}
}
-
ConfigClientController.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by Administrator on 2019/3/29.
*/
@RestController
public class ConfigClientController {
@Value("${profile}")//注意不会被覆盖,以远程为主
private String profile;
@GetMapping("/profile")
public String getProfile() {
return profile;
}
}
- 启动microserver-config-server-authc和microserver-config-client-authc服务,访问localhost:8081/profile
属性的优先级比uri的优先级高,那么为什么提供两种方式呢?岂不是很多余,在下一节中springcloud与eureka配合使用就知道了!!