Eureka的实操--中篇

Eureka的实操

2、安全连接方式

Eureka的安全连接可以通过以下步骤实现:

  1. 添加依赖:在项目的pom.xml文件中添加Eureka的依赖。
  2. 配置安全连接:在项目的application.yml或application.properties文件中添加Eureka的安全连接配置。具体包括设置安全基础URL、客户端ID、客户端密钥等。
  3. 关闭跨域攻击:在服务端增加配置类,关闭防止跨域攻击。如果需要跨域,可以在配置类中添加允许跨域的注解。
  4. 启用认证:在客户端项目中添加安全配置,设置账号密码,并启用Eureka的安全连接。

完成以上步骤后,即可实现Eureka的安全连接。注意,具体的配置方式可能因项目需求和环境而有所不同,需要结合实际情况进行调整。

架构图

1、Eureka-server

代码

java 复制代码
package com.ldzg.config;

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;

/**
 * 默认情况下SpringSecurity依赖的应用每个请求都需要添加CSRF token才能访问,
 * Eureka客户端注册时并不会添加,所以需要配置/eureka/**路径不需要CSRF token。
 * Created by macro on 2024/1/12.
 */
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}
java 复制代码
package com.ldzg;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

    public static void main(String[] args) throws UnknownHostException {
        ConfigurableApplicationContext application = SpringApplication.run(EurekaServerApplication.class, args);
        Environment env = application.getEnvironment();
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = env.getProperty("server.port");
        String path ="";// env.getProperty("server.servlet.context-path");
        log.info("\n----------------------------------------------------------\n\t" +
                "Application Eureka is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + path + "/\n\t" +
                "External: \thttp://" + ip + ":" + port + path + "/\n\t" +
                "swagger-ui: \thttp://" + ip + ":" + port + path + "/swagger-ui.html\n\t" +
                "Doc: \t\thttp://" + ip + ":" + port + path + "/doc.html\n" +
                "----------------------------------------------------------");
    }

}

pom.xml文件

xml 复制代码
<?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">
    <parent>
        <artifactId>springcloud-learning</artifactId>
        <groupId>com.ldzg</groupId>
        <version>1.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>eureka-server1</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

application.yml配置文件说明

yml 复制代码
server:
  port: 60004
spring:
  application:
    name: eureka-security-server
  security: #配置SpringSecurity登录用户名和密码
    basic:
      enabled: true
    user:
      name: ldzg
      password: 123456
eureka:
  # 关闭自我保护
  enable-self-preservation: false
  # 清理服务器
  eviction-interval-timer-in-ms: 5000
  instance:
    hostname: localhost
  client:
    healthcheck:
      enabled: true
    fetch-registry: false #指定是否要从注册中心获取服务(注册中心不需要开启)
    register-with-eureka: false #指定是否要注册到注册中心(注册中心不需要开启)
#    healthcheck:
#      #可以上报服务的真实健康状态
#      enabled: true
    service-url: #eureka的地址信息
        defaultZone: http://ldzg:123456@localhost:60004/eureka

运行结果:

访问eureka管理页面

2、springcloud-consumer

java代码

java 复制代码
package com.ldzg;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class UserServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceApplication.class, args);
    }

}

pom.xml文件

xml 复制代码
<?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">
    <parent>
        <artifactId>springcloud-learning</artifactId>
        <groupId>com.ldzg</groupId>
        <version>1.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-consumer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.yml配置文件说明

yml 复制代码
server:
  port: 61004
spring:
  application:
    name: eureka-consumer
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    #    healthcheck:
    #      #可以上报服务的真实健康状态
    #      enabled: true
    service-url:
      defaultZone: http://ldzg:123456@localhost:60004/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${spring.application.instance_id:${server.port}}
    appname: eureka-consumer

运行结果

eureka管理界面

相关推荐
Chan1632 分钟前
【 SpringCloud | 微服务 MQ基础 】
java·spring·spring cloud·微服务·云原生·rabbitmq
恰薯条的屑海鸥32 分钟前
零基础在实践中学习网络安全-皮卡丘靶场(第十五期-URL重定向模块)
学习·安全·web安全·渗透测试·网络安全学习
Tipray20061 小时前
让敏感数据在流转与存储中始终守护在安全范围
安全
movie__movie2 小时前
Eureka REST 相关接口
eureka·注册中心
前端页面仔2 小时前
易语言是什么?易语言能做什么?
开发语言·安全
2201_761199042 小时前
k8s4部署
云原生·容器·kubernetes
慌ZHANG2 小时前
云原生技术驱动 IT 架构现代化转型:企业实践与落地策略全解
云原生
moxiaoran57533 小时前
uni-app学习笔记三十--request网络请求传参
笔记·学习·uni-app
嘉陵妹妹3 小时前
深度优先算法学习
学习·算法·深度优先
西京刀客4 小时前
k8s热更新-subPath 不支持热更新
云原生·容器·kubernetes·configmap·subpath