Spring Cloud--Eureka註冊中心安全認證

前言

Github:https://github.com/yihonglei/thinking-in-springcloud

Eureka安全認證註冊中心:eureka-server-security

客戶端通過安全認證方式進行服務註冊:eureka-provider-order

一 eureka安全認證

eureka安全中心可以增加用戶名和密碼進行安全驗證訪問。

通過spring-boot-starter-security模塊實現。

二 eureka-server-security

1、項目結構

2、application.properties配置

spring.security.user.name=root
spring.security.user.password=123456

# 服務名稱和端口
spring.application.name=eureka-server-security
server.port=9000

# 定義Instance ID 的hostname
eureka.instance.hostname=localhost

# 自我註冊禁用(在默認情況下,服務註冊中心也會將自己作爲客戶端來嘗試註冊它自己,設置爲false)
eureka.client.register-with-eureka=false

# 是否檢索服務(由於註冊中心的職責就是維護服務實例,所以就不需要去檢索服務,設置爲false)
eureka.client.fetch-registry=false

# 安全認證,客戶端註冊也需要用戶名和密碼
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/

# 日誌文件
# logging.file=${spring.application.name}.log

# 是否開啓註冊中心自我保護機制(true開,false關,默認爲開)
eureka.server.enable-self-preservation=true

通過eureka.client.service-url.defaultZone

=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/

設置認證的用戶名和密碼。

 3、WebSecurityConfig

關閉csrf,開啓認證。

package com.lanhuigu.eureka.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * 安全認證
 *
 * @auther: yihonglei
 * @date: 2019-06-23 10:47
 */
@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.csrf().disable(); // 關閉csrf
        http.authorizeRequests().anyRequest().authenticated().and().httpBasic(); // 開啓認證
    }
}

4、EurekaServerApplication

應用啓動。

package com.lanhuigu.eureka;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
 * @SpringBootApplication 啓動一個Spring Boot應用程序
 * @EnableEurekaServer 註解啓動一個服務註冊中心提供給其他應用進行會話
 *
 * @auther: yihonglei
 * @date: 2019-06-17 21:03
 */
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}

5、啓動訪問

 http://localhost:9000回車,然後就進入認證登錄頁面,輸入用戶名和密碼訪問。

三 eureka-provider-order

客戶端安全認證註冊需要指定用戶名和密碼,源碼參考github。

application.properties配置。

spring.security.user.name=root
spring.security.user.password=123456

# 註冊到eureka服務端的微服務名稱
spring.application.name=eureka-provider-order

# 服務提供端口
server.port=8001
# 註冊到eureka服務端的地址
#eureka.client.service-url.defaultZone=http://localhost:9000/eureka/

# 安全認證註冊地址,eureka服務端設置了用戶名和密碼,客戶端註冊時需要設置對應的用戶名和密碼
eureka.client.service-url.defaultZone=http://${spring.security.user.name}:${spring.security.user.password}@localhost:9000/eureka/

# 顯示指定微服務的名稱,默認ip:應用名稱:端口(192.168.1.7:eureka-provider-order:8001)
eureka.instance.instance-id=eureka-provider-order-8001
eureka.instance.prefer-ip-address=true

訪問註冊中心,可以看到服務已經註冊成功。 

四 總結

1、如果在eureka服務端增加上安全認證,客戶端無法註冊成功,先看看有沒有WebSecurityConfig。

2、客戶端也需要用戶名和密碼認證註冊的,服務端改成安全認證,客戶端不要忘了改。

3、如果服務端是安全認證的集羣服務,客戶端註冊時每個地址都需要用戶名和密碼安全認證。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章