Eureka:安全配置(HTTPS / 令牌验证)最佳实践
Eureka 是 Netflix 开发的一个开源项目,用于在分布式系统中提供服务注册与发现。在微服务架构中,Eureka 作为服务注册中心,扮演着至关重要的角色。随着服务数量的增加,Eureka 的安全性也日益受到关注。本文将围绕 Eureka 的安全配置,特别是 HTTPS 和令牌验证,探讨最佳实践。
Eureka 安全配置概述
Eureka 的安全配置主要包括以下几个方面:
1. HTTPS 配置:通过 HTTPS 协议加密通信,防止数据泄露。
2. 令牌验证:通过令牌验证机制,确保只有授权的服务才能访问 Eureka。
HTTPS 配置
1. 生成 SSL 证书
我们需要生成一个自签名的 SSL 证书。以下是一个使用 OpenSSL 生成自签名证书的示例:
bash
生成私钥
openssl genrsa -out eureka.key 2048
生成 CSR(证书签名请求)
openssl req -new -key eureka.key -out eureka.csr
生成自签名证书
openssl x509 -req -days 365 -in eureka.csr -signkey eureka.key -out eureka.crt
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
3. 配置 HTTPS
在 Eureka 的启动类中,添加以下代码:
java
import javax.net.ssl.SSLContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
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;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.server.SecurityWebFilterChain;
@Configuration
@EnableWebSecurity
public class EurekaSecurityConfig extends WebSecurityConfigurerAdapter {
@Bean
public SecurityWebFilterChain securityWebFilterChain(HttpSecurity http) throws Exception {
http
.addFilterBefore(new BasicAuthFilter(), UsernamePasswordAuthenticationFilter.class)
.authorizeRequests()
.anyRequest().authenticated()
.and()
.formLogin()
.and()
.csrf().disable();
return http.build();
}
@Bean
public UserDetailsService userDetailsService() {
UserDetails user = User.withUsername("admin")
.password(passwordEncoder().encode("admin"))
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}
@Bean
public SSLContext sslContext() throws Exception {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(new ClassPathResource("eureka.p12").getInputStream(), "password".toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(keyStore, "password".toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init((KeyStore) null);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), new SecureRandom());
return sslContext;
}
}
4. 配置 HTTPS 服务器
在 Eureka 的启动类中,添加以下代码:
java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@SpringBootApplication
public class EurekaApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
@Bean
public Resource resource() {
return new ClassPathResource("eureka.crt");
}
}
令牌验证
1. 配置令牌验证
在 Eureka 的配置文件 `eureka-server.properties` 中,添加以下配置:
```properties
eureka.client.serviceUrl.defaultZone=https://localhost:8443/eureka/
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.preferSameZoneInstance=false
eureka.client.filterOnlyUpInstances=true
eureka.client.registerInstanceEnabledByDefault=true
eureka.client.healthcheckEnabled=true
eureka.client.eurekaServiceUrlPollInterval=30000
eureka.client.eurekaServiceUrlPollTimeout=2000
eureka.client.eurekaServiceUrlPollMaxRetries=2
eureka.client.eurekaServiceUrlPollRetryInterval=2000
eureka.client.eurekaServiceUrlPollRetryCount=2
eureka.client.eurekaServiceUrlPollInitialInterval=2000
eureka.client.eurekaServiceUrlPollInitialIntervalJitter=1000
eureka.client.eurekaServiceUrlPollIntervalJitter=1000
eureka.client.eurekaServiceUrlPollIntervalJitterMax=5000
eureka.client.eurekaServiceUrlPollIntervalJitterMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterRange=5000
eureka.client.eurekaServiceUrlPollIntervalJitterSteps=10
eureka.client.eurekaServiceUrlPollIntervalJitterStepsMax=10
eureka.client.eurekaServiceUrlPollIntervalJitterStepsMin=1
eureka.client.eurekaServiceUrlPollIntervalJitterStepsRange=10
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepStepMax=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepStepMin=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepStepRange=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStepStepStepStepStepStep=1000
eureka.client.eurekaServiceUrlPollIntervalJitterStepsStepStepStepStepStepStep
Comments NOTHING