大数据之eureka 服务注册中心安全通信配置 双向 TLS 认证

大数据阿木 发布于 2025-07-12 12 次阅读


Eureka 服务注册中心安全通信配置:双向 TLS 认证实践

在分布式系统中,服务注册中心(如 Netflix Eureka)是核心组件之一,它负责服务实例的注册和发现。随着网络安全威胁的增加,确保服务注册中心的安全通信变得尤为重要。双向 TLS(也称为双因素认证)是一种增强的安全通信方式,它要求通信双方都提供证书进行验证。本文将围绕 Eureka 服务注册中心的安全通信配置,特别是双向 TLS 认证的实现,进行深入探讨。

双向 TLS 简介

双向 TLS 是一种安全协议,它要求通信双方(客户端和服务器)在建立连接时都提供数字证书,并由对方验证。这种认证方式比单向 TLS(仅服务器验证客户端)提供了更高的安全性,因为它减少了中间人攻击的风险。

Eureka 双向 TLS 配置步骤

以下是使用 Spring Cloud Netflix Eureka 实现双向 TLS 认证的步骤:

1. 准备证书

你需要生成一对证书和私钥。可以使用 OpenSSL 或其他工具来生成证书。

bash

生成 CA 证书


openssl req -x509 -newkey rsa:4096 -keyout ca.key -out ca.crt -days 365 -nodes -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=MyCA"

生成服务器证书


openssl req -newkey rsa:4096 -keyout server.key -out server.csr -days 365 -nodes -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=MyServer"

使用 CA 证书签署服务器证书


openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAserial ca.srl -out server.crt -days 365

生成客户端证书


openssl req -newkey rsa:4096 -keyout client.key -out client.csr -days 365 -nodes -subj "/C=CN/ST=Beijing/L=Beijing/O=MyCompany/CN=MyClient"

使用 CA 证书签署客户端证书


openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAserial ca.srl -out client.crt -days 365


2. 配置 Eureka 服务器

在 Eureka 服务器配置文件中(如 `eureka-server.properties`),添加以下配置:

properties

eureka.client.serviceUrl.defaultZone=https://localhost:8443/eureka/


eureka.server.enable-self-preservation=false


eureka.server.enable-deletion-protection=false


eureka.server.use-dns=false


eureka.server.vip-address=


eureka.server.hostname=localhost


eureka.server.port=8443


eureka.server.ssl-port=8443


eureka.server.keystore-file=/path/to/server.keystore


eureka.server.keystore-password=your_keystore_password


eureka.server.truststore-file=/path/to/ca.crt


eureka.server.truststore-password=your_truststore_password


3. 配置 Eureka 客户端

在 Eureka 客户端配置文件中(如 `eureka-client.properties`),添加以下配置:

properties

eureka.client.serviceUrl.defaultZone=https://localhost:8443/eureka/


eureka.client.secure-port=8443


eureka.client.keystore-file=/path/to/client.keystore


eureka.client.keystore-password=your_keystore_password


eureka.client.truststore-file=/path/to/ca.crt


eureka.client.truststore-password=your_truststore_password


4. 编写代码

在 Eureka 服务器和客户端的 Spring Boot 应用程序中,确保你已经添加了 `spring-boot-starter-security` 和 `spring-boot-starter-tomcat` 依赖。

以下是 Eureka 服务器和客户端的配置代码示例:

java

import org.springframework.boot.SpringApplication;


import org.springframework.boot.autoconfigure.SpringBootApplication;


import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


import org.springframework.context.annotation.Bean;


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;


import org.springframework.security.core.userdetails.User;


import org.springframework.security.core.userdetails.UserDetails;


import org.springframework.security.core.userdetails.UserDetailsService;


import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


import org.springframework.security.crypto.password.PasswordEncoder;


import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@SpringBootApplication


@EnableEurekaClient


@EnableWebSecurity


public class EurekaApplication {

public static void main(String[] args) {


SpringApplication.run(EurekaApplication.class, args);


}

@Bean


public UserDetailsService userDetailsService() {


UserDetails user = User.withUsername("user")


.password(passwordEncoder().encode("password"))


.roles("USER")


.build();


return new InMemoryUserDetailsManager(user);


}

@Bean


public PasswordEncoder passwordEncoder() {


return new BCryptPasswordEncoder();


}

@Bean


public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter() {


return new WebSecurityConfigurerAdapter() {


@Override


protected void configure(HttpSecurity http) throws Exception {


http.csrf().disable();


http.authorizeRequests()


.antMatchers("/actuator/").permitAll()


.anyRequest().authenticated()


.and()


.httpBasic();


}


};


}


}


总结

本文介绍了如何在 Spring Cloud Netflix Eureka 中实现双向 TLS 认证。通过配置证书和修改配置文件,你可以增强 Eureka 服务注册中心的安全性。在实际部署中,请确保你的证书和私钥安全,并定期更新它们以保持系统的安全性。

双向 TLS 认证虽然增加了配置的复杂性,但它为服务注册中心提供了更高的安全性,是保护分布式系统免受网络攻击的重要手段。