SpringCloud配置服務端的ConfigServer設置安全認證
一、大致介紹
1、前面提到的加密內容,雖然說對內容進行了加密,但是為了更安全的安全隔離,服務與服務之間也需要設置簡單的安全認證;
2、那么在本章節(jié)我們講解下如何配置服務端之間的簡單認證,Springcloud 的強大之處在于對認證這塊僅僅配置一下即可;
3、然后后續(xù)我們還會講解 SpringCloud 的 auth2 等認證機制,后續(xù)有待繼續(xù)講解;
4、這里還順便列舉下配置路徑的規(guī)則:
/****************************************************************************************
* 配置服務的路勁規(guī)則:
*
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
****************************************************************************************/
二、實現(xiàn)步驟
2.1 添加 maven 引用包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>springms-config-server-authc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.springms.cloud</groupId>
<artifactId>springms-spring-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 服務端配置模塊 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 服務端登錄驗證模塊 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>2.2 添加應用配置文件
(springms-config-server-authcsrcmainresourcesapplication.yml)
#配置登錄密碼
security:
basic:
enabled: true
user:
name: admin
password: admin
server:
port: 8275
spring:
application:
name: springms-config-server-authc
cloud:
config:
server:
git:
uri: https://gitee.com/ylimhhmily/OpenSource_CustomCircleLineProgressBar2.3 添加微服務啟動類
(springms-config-server-authc/src/main/java/com/springms/cloud/MsConfigServerAuthcApplication.java)
package com.springms.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* 配置服務端ConfigServer設置安全認證。
*
* @author hmilyylimh
*
* @version 0.0.1
*
* @date 17/10/18
*
*/
@SpringBootApplication
@EnableConfigServer
public class MsConfigServerAuthcApplication {
public static void main(String[] args) {
SpringApplication.run(MsConfigServerAuthcApplication.class, args);
System.out.println("【【【【【【 ConfigServerAuthc微服務 】】】】】】已啟動.");
}
}三、測試
一、配置服務服務端Server應用入口(正常測試):
1、注解:EnableConfigServer
2、編輯 application.yml 文件,配置登錄密碼;
3、啟動 springms-config-server-authc 模塊服務,啟動1個端口;
4、在瀏覽器輸入地址 http://localhost:8275/abc-default.properties 正常情況下會輸出配置文件的內容;
5、在瀏覽器輸入地址 http://localhost:8275/abc-default.yml 正常情況下會輸出配置文件的內容;
6、在瀏覽器輸入地址 http://localhost:8275/abc-hehui.yml 正常情況下會輸出配置文件的內容;
7、在瀏覽器輸入地址 http://localhost:8275/aaa-bbb.yml 正常情況下會輸出配置文件的內容;
8、在瀏覽器輸入地址 http://localhost:8275/aaa-bbb.properties 正常情況下會輸出配置文件的內容;
9、在瀏覽器輸入地址 http://localhost:8275/master/abc-default.properties 正常情況下會輸出配置文件的內容;
10、在瀏覽器輸入地址 http://localhost:8275/master/abc-default.yml 正常情況下會輸出配置文件的內容;
11、在瀏覽器輸入地址 http://localhost:8275/master/abc-hehui.yml 正常情況下會輸出配置文件的內容;
12、在瀏覽器輸入地址 http://localhost:8275/master/aaa-bbb.yml 正常情況下會輸出配置文件的內容;
13、在瀏覽器輸入地址 http://localhost:8275/master/aaa-bbb.properties 正常情況下會輸出配置文件的內容;
14、在瀏覽器輸入地址 http://localhost:8275/springms-config-server-dev.yml 正常情況下會輸出配置文件的內容;
總結:按照配置服務的路徑規(guī)則配置,基本上都可以訪問得到結果數(shù)據(jù)。
下載地址 https://gitee.com/ylimhhmily/SpringCloudTutorial.git
以上就是SpringCloud配置服務端的ConfigServer設置安全認證的詳細內容,更多關于SpringCloud ConfigServer安全認證的資料請關注腳本之家其它相關文章!
相關文章
SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼
這篇文章主要介紹了SpringCloud之分布式配置中心Spring Cloud Config高可用配置實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
詳解SpringBoot修改啟動端口server.port的四種方式
這篇文章主要介紹了詳解SpringBoot修改啟動端口server.port的四種方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07

