Eureka的实操--中篇

Eureka的实操

2、安全连接方式

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

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

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

架构图
Eureka的实操--中篇_第1张图片

1、Eureka-server

代码

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);
    }
}

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文件


<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-learningartifactId>
        <groupId>com.ldzggroupId>
        <version>1.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>eureka-server1artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-serverartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-securityartifactId>
        dependency>
    dependencies>

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

project>

application.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张图片

访问eureka管理页面

Eureka的实操--中篇_第3张图片

Eureka的实操--中篇_第4张图片

2、springcloud-consumer

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文件


<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-learningartifactId>
        <groupId>com.ldzggroupId>
        <version>1.1-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>springcloud-consumerartifactId>

    <properties>
        <maven.compiler.source>8maven.compiler.source>
        <maven.compiler.target>8maven.compiler.target>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>


    dependencies>

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

application.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的实操--中篇_第5张图片

eureka管理界面
Eureka的实操--中篇_第6张图片

你可能感兴趣的:(eureka,云原生,中间件,安全,学习)