我们的Eureka Server注册中心需要安全保护,如果不保护的话,是很不安全的。Eureka Server注册中心常用的安全保护组件是Security!

二、上代码

1、项目结构

Eureka Server+Security!

2、pom.xml

Eureka Server+Security!
<?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>
Eureka Server+Security!

 

3、application.yml

Eureka Server+Security!
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/
Eureka Server+Security!

 

4、RegisterWebSecurityConfigure

Eureka Server+Security!
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);
    }
}
Eureka Server+Security!

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

Eureka Server+Security!
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/
Eureka Server+Security!

 

三、运行注册中心

1、打开url:http://localhost:8001/register 会自动调转到http://localhost:8001/register/login

Eureka Server+Security!

使用配置文件中的用户名(admin)及密码(123456)登录。登录成功之后就会调转到控制台

Eureka Server+Security!

 

相关文章:

  • 2021-06-25
  • 2021-10-20
  • 2021-11-05
  • 2021-08-08
  • 2021-10-16
  • 2021-05-30
  • 2021-07-07
  • 2021-06-06
猜你喜欢
  • 2021-12-11
  • 2021-05-02
  • 2021-05-28
  • 2021-06-11
  • 2022-12-23
  • 2021-08-10
  • 2019-06-13
相关资源
相似解决方案