微服务:整合 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)登录。登录成功之后就会调转到控制台

 

posted @ 2020-03-26 14:08  颜士  阅读(2387)  评论(0编辑  收藏  举报