微服務:整合 Spring Cloud Eureka - Security 安全保護


目錄

   微服務:整合 Spring Cloud Eureka - 注冊中心 Eureka Server 

   微服務:整合 Spring Cloud Eureka - 服務注冊 Eureka Client  

   微服務:整合 Spring Cloud Eureka - 服務發現 DiscoveryClient 

   微服務:整合 Spring Cloud Eureka - 服務消費以及Ribbon簡單使用 

   微服務:整合 Spring Cloud Eureka - 高可用集群  

   微服務:整合 Spring Cloud Eureka - .NET Core Mvc Api (C#) 

   微服務:整合 Spring Cloud Eureka - 服務治理機制  

   微服務:整合 Spring Cloud Eureka - 服務事件監聽  

   微服務:整合 Spring Cloud Eureka - 高級屬性Region、Zone

   微服務:整合 Spring Cloud Eureka - Rest接口文檔 

   微服務:整合 Spring Cloud Eureka - Security 安全保護

一、前言

  我們的Eureka Server注冊中心需要安全保護,如果不保護的話,是很不安全的。Eureka Server注冊中心常用的安全保護組件是Security!

二、上代碼

1、項目結構

2、pom.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>spring-cloud-register</artifactId>
        <groupId>com.demo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-register</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
        </dependency>

    </dependencies>

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

</project>

 

3、application.yml

server:
  port: 8001
  servlet:
    context-path: /register

spring:
  application:
    name: demo-register
  security:
    user:
      name: admin
      password: 123456

eureka:
  instance:
    hostname: peer1
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 30
    serviceUrl:
      defaultZone: http://admin:123456@peer1:8001/register/eureka/

 

4、RegisterWebSecurityConfigure

package com.demo.register.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;

@EnableWebSecurity
public class RegisterWebSecurityConfigure extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/register/eureka/**");
        super.configure(http);
    }
}

5、demo-service-provider -> pom.xml

server:
  port: 8102

spring:
  application:
    name: demo-service-provider

eureka:
  instance:
    lease-renewal-interval-in-seconds: 3
    lease-expiration-duration-in-seconds: 9
    hostname: peer1
    metadata-map:
      zone: zone-1
  client:
    register-with-eureka: true
    fetch-registry: true
    instance-info-replication-interval-seconds: 9
    registry-fetch-interval-seconds: 3
    serviceUrl:
      defaultZone: http://admin:123456@peer1:8001/register/eureka/

 

三、運行注冊中心

1、打開url:http://localhost:8001/register 會自動調轉到http://localhost:8001/register/login

使用配置文件中的用戶名(admin)及密碼(123456)登錄。登錄成功之后就會調轉到控制台

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM