zoukankan      html  css  js  c++  java
  • Eureka Server+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)登录。登录成功之后就会调转到控制台

  • 相关阅读:
    preg_replace函数/e后门
    php7.0-7.3的bypass disable_function一把梭
    PHP反序列化逃逸
    day2filter_var函数漏洞
    基于 Elasticsearch 聚合搜索实现电商筛选查询功能
    基于SpringBoot + Redis + 轮询实现扫码登录
    教你理解Lambda表达式
    记录解决 Elasticseach 过滤与聚合问题
    基于 MyBatis-Plus 解决数据库逻辑删除与唯一索引问题
    Java8 Stream Lamdba sorted()排序遇到的小坑
  • 原文地址:https://www.cnblogs.com/smallfa/p/14073846.html
Copyright © 2011-2022 走看看